Skip to main content
Creates a new table. By default, tables are created only on the current server. Distributed DDL queries are implemented as ON CLUSTER clause, which is described separately.

Syntax forms

This query can have various syntax forms depending on the use case.

Create a table with an explicit schema

Creates a table named table_name in the db database or the current database if db is not set, with the structure specified in brackets and the engine engine. The structure of the table is a list of column descriptions, secondary indexes, projections and constraints . If primary key is supported by the engine, it will be indicated as parameter for the table engine. A column description is name type in the simplest case. Example: RegionID UInt32. Expressions can also be defined for default values (see below). If necessary, primary key can be specified, with one or more key expressions. Comments can be added for columns and for the table.

Create a table with an existing tables schema

ClickHouse supports the ability to copy the schema and data of an existing table. For replicating the schema of an existing table: This creates a table with the same structure as another table.

Create a table with an existing tables schema and data

For replicating the schema and data of an existing table:
This creates a table with the same schema and data as an existing table. After the new table is created, all partitions from db.table are attached to it. In other words, the data of db.table is cloned into db2.table_clone upon creation. This query is equivalent to the following:
For both features, you can specify a different engine for the table. If the engine is not specified, the same engine will be used as for the original table (db.table).

Create a table with a table function

Creates a table with the same result as that of the table function specified. The created table will also work in the same way as the corresponding table function that was specified.

Create a table with a SELECT query

Creates a table with a structure like the result of the SELECT query, with the engine engine, and fills it with data from SELECT. Also you can explicitly specify columns description. If the table already exists and IF NOT EXISTS is specified, the query won’t do anything. There can be other clauses after the ENGINE clause in the query. See detailed documentation on how to create tables in the descriptions of table engines. Example
Query
Response

Specify column default values

The column description can specify a default value expression in the form of DEFAULT expr, MATERIALIZED expr, or ALIAS expr. Example: URLDomain String DEFAULT domain(URL). The expression expr is optional. If it is omitted, the column type must be specified explicitly and the default value will be 0 for numeric columns, '' (the empty string) for string columns, [] (the empty array) for array columns, 1970-01-01 for date columns, or NULL for nullable columns. The column type of a default value column can be omitted in which case it is inferred from expr’s type. For example the type of column EventDate DEFAULT toDate(EventTime) will be date. If both a data type and a default value expression are specified, an implicit type casting function inserted which converts the expression to the specified type. Example: Hits UInt32 DEFAULT 0 is internally represented as Hits UInt32 DEFAULT toUInt32(0). A default value expression expr may reference arbitrary table columns and constants. ClickHouse checks that changes of the table structure do not introduce loops in the expression calculation. For INSERT, it checks that expressions are resolvable – that all columns they can be calculated from have been passed.

DEFAULT

DEFAULT expr Normal default value. If the value of such a column is not specified in an INSERT query, it is computed from expr. Example:

MATERIALIZED

MATERIALIZED expr Materialized expression. Values of such columns are automatically calculated according to the specified materialized expression when rows are inserted. Values cannot be explicitly specified during INSERTs. Also, default value columns of this type are not included in the result of SELECT *. This is to preserve the invariant that the result of a SELECT * can always be inserted back into the table using INSERT. This behavior can be disabled with setting asterisk_include_materialized_columns. Example:

EPHEMERAL

EPHEMERAL [expr] Ephemeral column. Columns of this type are not stored in the table and it is not possible to SELECT from them. The only purpose of ephemeral columns is to build default value expressions of other columns from them. An insert without explicitly specified columns will skip columns of this type. This is to preserve the invariant that the result of a SELECT * can always be inserted back into the table using INSERT. Example:

ALIAS

ALIAS expr Calculated columns (synonym). Column of this type are not stored in the table and it is not possible to INSERT values into them. When SELECT queries explicitly reference columns of this type, the value is computed at query time from expr. By default, SELECT * excludes ALIAS columns. This behavior can be disabled with setting asterisk_include_alias_columns. When using the ALTER query to add new columns, old data for these columns is not written. Instead, when reading old data that does not have values for the new columns, expressions are computed on the fly by default. However, if running the expressions requires different columns that are not indicated in the query, these columns will additionally be read, but only for the blocks of data that need it. If you add a new column to a table but later change its default expression, the values used for old data will change (for data where values were not stored on the disk). Note that when running background merges, data for columns that are missing in one of the merging parts is written to the merged part. It is not possible to set default values for elements in nested data structures.

Use NULL or NOT NULL modifiers

NULL and NOT NULL modifiers after data type in column definition allow or do not allow it to be Nullable. If the type is not Nullable and if NULL is specified, it will be treated as Nullable; if NOT NULL is specified, then no. For example, INT NULL is the same as Nullable(INT). If the type is Nullable and NULL or NOT NULL modifiers are specified, the exception will be thrown. See also data_type_default_nullable setting.

Primary key

You can define a primary key when creating a table. A primary key can be specified in two ways:
Inside the column list
Outside the column list
You can’t combine both ways in one query.

Specify table constraints

Along with columns descriptions, constraints could be defined:

CONSTRAINT

boolean_expr_1 could by any boolean expression. If constraints are defined for the table, each of them will be checked for every row in INSERT query. If any constraint is not satisfied — server will raise an exception with constraint name and checking expression. Adding large amount of constraints can negatively affect performance of big INSERT queries. Existing constraints across all tables can be inspected via the system.constraints table.

ASSUME

The ASSUME clause is used to define a CONSTRAINT on a table that is assumed to be true. This constraint can then be used by the optimizer to enhance the performance of SQL queries. Take this example where ASSUME CONSTRAINT is used in the creation of the users_a table:
Here, ASSUME CONSTRAINT is used to assert that the length(name) function always equals the value of the name_len column. This means that whenever length(name) is called in a query, ClickHouse can replace it with name_len, which should be faster because it avoids calling the length() function. Then, when executing the query SELECT name FROM users_a WHERE length(name) < 5;, ClickHouse can optimize it to SELECT name FROM users_a WHERE name_len < 5; because of the ASSUME CONSTRAINT. This can make the query run faster because it avoids calculating the length of name for each row. ASSUME CONSTRAINT does not enforce the constraint, it merely informs the optimizer that the constraint holds true. If the constraint is not actually true, the results of the queries may be incorrect. Therefore, you should only use ASSUME CONSTRAINT if you are sure that the constraint is true.

Define storage time with TTL

Defines storage time for values. Can be specified only for MergeTree-family tables. For the detailed description, see TTL for columns and tables.

Select column compression codecs

By default, ClickHouse applies lz4 compression in the self-managed version, and zstd in ClickHouse Cloud. You can also define the compression method for each individual column in the CREATE TABLE query:
For the available general purpose, specialized and encryption codecs, see Column compression codecs.

Create temporary tables

ClickHouse supports temporary tables, which disappear when the session ends. For details, see CREATE TEMPORARY TABLE.

Update a table atomically with REPLACE TABLE

The REPLACE statement allows you to update a table atomically. For details, see REPLACE TABLE.

Add a table comment

You can add a comment to the table when creating it. Syntax
The COMMENT clause must be specified after any storage-specific clauses such as PARTITION BY, ORDER BY, and storage-specific SETTINGS.After the COMMENT clause, only query-specific SETTINGS (like max_threads, etc.) will be parsed, not storage-related settings.This means the correct clause order is:
  • ENGINE
  • storage clauses
  • COMMENT
  • query settings (if any)
Example
Query
Response
Last modified on August 7, 2026