# Create Table

- ### Create Local Table

**Syntax:**

```sql
CREATE TABLE [IF NOT EXISTS] [db.]local_table_name ON CLUSTER ck_cluster
(
    name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1],
    name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2],
    ...
    INDEX index_name1 expr1 TYPE type1(...) GRANULARITY value1,
    INDEX index_name2 expr2 TYPE type2(...) GRANULARITY value2
) ENGINE = engine_name()
[PARTITION BY expr]
ORDER BY expr
[PRIMARY KEY expr]
[SAMPLE BY expr]
[SETTINGS name=value, ...];
```

**Parameter Description:**

<table>
    <thead>
        <tr>
            <th>Parameter</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
    <tr>
        <td><code>db</code></td>
        <td>The name of the database, default is the currently selected database. In this document, ck_test is used as an example.</td>
    </tr>
    <tr>
        <td><code>local_table_name</code></td>
        <td>Local table name.</td>
    </tr>
    <tr>
        <td><code>ON CLUSTER ck_test</code></td>
        <td>Create a local table on every node, fixed as
            <code>ON CLUSTER ck_test</code>.
        </td>
    </tr>
    <tr>
        <td><code>name1,name2</code></td>
        <td>Column names.</td>
    </tr>
    <tr>
        <td><code>type1,type2</code></td>
        <td>Column types. For types supported by UClickHouse, please refer to <a title="" href="/uclickhouse/developer/data_type">Data Types</a>.</td>
    </tr>
    <tr>
        <td>
            <code>ENGINE = engine_name()</code>
        </td>
        <td>Table engine type.
            <div>
                For dual-replica clusters, you should use the Replicated* engines of the MergeTree series that support data replication, otherwise data will not be replicated between replicas, resulting in inconsistent query results. When using this engine to create a table, the parameter should be provided as follows.
                <ul>
                    <li>
                          ReplicatedMergeTree('/clickhouse/tables/{database}/{table}/{shard}','{replica}'), characters in the parameter should not be modified.
                    </li>
                    <li>ReplicatedMergeTree(), which is equivalent to ReplicatedMergeTree('/clickhouse/tables/{database}/{table}/{shard}', '{replica}').
                    </li>
                </ul>
            </div>
            <div>
                For table engine types supported by ClickHouse, please refer to <a href="/uclickhouse/developer/table_engine">Table Engine</a>.
            </div>
    </td>
</tr>
<tr>
    <td><code>ORDER BY expr</code></td>
    <td>Sorting key, required, can be a tuple of columns or any expression.</td>
</tr>
<tr>
    <td><code>[DEFAULT|MATERIALIZED|ALIAS expr]</code></td>
    <td>Default expression.
        <ul>
            <li>DEFAULT: Common default expression. Generates and fills in default values when the field is omitted.</li>
            <li>MATERIALIZED: Materialized expression.</li>
            <li>ALIAS: Alias expression.</li>
        </ul>
    </td>
</tr>
<tr>
    <td><code>GRANULARITY</code></td>
    <td>Index granularity parameter.</td>
</tr>
<tr>
    <td>
        <code>[PARTITION BY expr]</code>
    </td>
    <td>Partition key. Usually partitioned by date, but can use other fields or expressions.</td>
</tr>
<tr>
    <td><code>[PRIMARY KEY expr]</code></td>
    <td>Primary key, by default the same as the sorting key. In most cases, you don't need to use the
        <code>PRIMARY KEY</code> clause to specify the primary key.
    </td>
</tr>
<tr>
    <td><code>[SAMPLE BY expr]</code></td>
    <td>Sampling expression. If you want to use a sampling expression, it must be included in the primary key.</td>
</tr>
<tr>
    <td><code>[SETTINGS name=value, ...]</code></td>
    <td>Additional parameters affecting performance.
        <strong>Tip</strong> 
        For parameters supported in <code>SETTINGS</code>, please refer to 
        <span><a href="https://clickhouse.com/docs/zh/engines/table-engines/mergetree-family/mergetree/"
                target="_blank">SETTINGS Configuration</a>
        </span>.
    </td>
</tr>
</tbody>
</table>

The parameters `ORDER BY`, `GRANULARITY`, `PARTITION BY`, `PRIMARY KEY`, `SAMPLE BY` and `[SETTINGS name=value, ...]` are only supported by the MergeTree family of table engines. For more parameter explanations, refer to [CREATE TABLE](https://clickhouse.com/docs/zh/sql-reference/statements/create/table/).

**Example:**

```sql
CREATE TABLE lineorder_local on cluster ck_cluster
(
    LO_ORDERKEY             UInt32,
    LO_LINENUMBER           UInt8,
    LO_CUSTKEY              UInt32,
    LO_PARTKEY              UInt32,
    LO_SUPPKEY              UInt32,
    LO_ORDERDATE            Date,
    LO_ORDERPRIORITY        LowCardinality(String),
    LO_SHIPPRIORITY         UInt8,
    LO_QUANTITY             UInt8,
    LO_EXTENDEDPRICE        UInt32,
    LO_ORDTOTALPRICE        UInt32,
    LO_DISCOUNT             UInt8,
    LO_REVENUE              UInt32,
    LO_SUPPLYCOST           UInt32,
    LO_TAX                  UInt8,
    LO_COMMITDATE           Date,
    LO_SHIPMODE             LowCardinality(String)
)
ENGINE = MergeTree PARTITION BY toYear(LO_ORDERDATE) ORDER BY (LO_ORDERDATE, LO_ORDERKEY);
```

- ### Create Distributed Table

A distributed table is a collection of local tables that abstracts multiple local tables into a unified table, providing write and query capabilities. When data is written to a distributed table, it is automatically distributed to multiple local tables in the collection. When querying the distributed table, each local table in the collection is queried separately, and the final result is aggregated and returned. You need to create a local table first and then create a distributed table.

**Basic Syntax:**

```sql
CREATE TABLE [db.]distributed_table_name ON CLUSTER ck_cluster
 AS db.local_table_name ENGINE = Distributed(cluster, db, local_table_name [, sharding_key])
```

**Parameter Description:**

<table>
    <thead>
        <tr>
            <th>Parameter</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
    <tr>
        <td><code>db</code></td>
        <td>The name of the database, default is the currently selected database. In this document, ck_cluster is used as an example.</td>
    </tr>
    <tr>
        <td><code>distributed_table_name</code></td>
        <td>Distributed table name.</td>
    </tr>
    <tr>
        <td><code>ON CLUSTER ck_cluster</code></td>
        <td>Create a table on every node, fixed as <code>ON CLUSTER ck_cluster</code>.
        </td>
    </tr>
    <tr>
        <td><code>local_table_name</code></td>
        <td>Already created local table name.</td>
    </tr>
    <tr>
        <td><code>sharding_key</code></td>
        <td>Sharding expression, used to decide into which shard the data will be written.
            <p>
                <code>sharding_key</code>
                can be an expression, such as the function <code>rand()</code>, or a column, such as
                <code>user_id</code> (Integer type).
            </p>
        </td>
    </tr>
    </tbody>
</table>

**Example:**

```
CREATE TABLE lineorder_distribute  AS lineorder on cluster ck_cluster
ENGINE = Distributed(
    ck_cluster,
    ch_test,
    lineorder_local,
    rand()
  );
```

- ### Create Table by Copying Table Structure

**Basic Syntax:**

```sql
CREATE TABLE [IF NOT EXISTS] [db.]table_name2 ON CLUSTER ck_cluster AS [db.]table_name1 [ENGINE = engine_name];
```

**Parameter Description:**

<table>
    <thead>
        <tr>
            <th>Parameter</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
    <tr>
        <td><code>db</code></td>
        <td>The name of the database, default is the currently selected database. In this document, ck_cluster is used as an example.</td>
    </tr>
    <tr>
        <td><code>table_name1</code></td>
        <td>Source table whose structure is copied. In this document, an already created local table named local_table is used as an example.</td>
    </tr>
    <tr>
        <td><code>table_name2</code></td>
        <td>Newly created table.</td>
    </tr>
    <tr>
        <td><code>ON CLUSTER ck_cluster</code></td>
        <td>Create a table on every node, fixed as <code>ON CLUSTER ck_cluster</code>.
        </td>
    </tr>
    <tr>
        <td><code>[ENGINE = engine_name]</code></td>
        <td>Table engine type. If not specified, the engine is the same as the copied table.
            <strong>Note</strong> <span>For table engine types supported by UClickHouse</span>, please refer to <span>
                <a href="/uclickhouse/developer/table_engine">Table Engine</a>
            </span>.
        </td>
    </tr>
    </tbody>
</table>

**Example:**

```sql
create table lineorder ON CLUSTER ck_cluster as ck_test.lineorder_local;
```

- ### Create Table Using SELECT Statement

**Basic Syntax:**

```
CREATE TABLE [IF NOT EXISTS] [db.]table_name ON CLUSTER ck_cluster ENGINE = engine_name() AS SELECT ...
```

**Parameter Description:**

<table>
    <thead>
        <tr>
            <th>Parameter</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
    <tr>
        <td><code>db</code></td>
        <td>The name of the database, default is the currently selected database. In this document, ck_cluster is used as an example.</td>
    </tr>
    <tr>
        <td><code>table_name</code></td>
        <td>Table created using SELECT statement.</td>
    </tr>
    <tr>
        <td><code>ON CLUSTER ck_cluster</code></td>
        <td>
            Create a table on every node, fixed as <code>ON CLUSTER ck_cluster</code>.
        </td>
    </tr>
    <tr>
        <td><code>ENGINE = engine_name()</code></td>
        <td>Table engine type. For table engine types supported by UClickHouse, please refer to <a href="/uclickhouse/developer/table_engine">Table Engine</a>
        </td>
    </tr>
    <tr>
        <td><code>SELECT ...</code></td>
        <td><code>SELECT</code> clause.
        </td>
    </tr>
    </tbody>
</table>

**Example:**

```
create table lineorder ON CLUSTER ck_cluster ENGINE =MergeTree() order by Year as select * from ck_cluster.local_table;
```

### Official Documentation Reference

- For more information on creating tables, please see [CREATE TABLE](https://clickhouse.com/docs/zh/sql-reference/statements/create/table/).
- For more information on creating tables by copying table structure, please see [With a Schema Similar to Other Table](https://clickhouse.com/docs/zh/sql-reference/statements/create/table/#with-a-schema-similar-to-other-table).
- For more information on creating tables using the SELECT statement, please see [From SELECT query](https://clickhouse.com/docs/zh/sql-reference/statements/create/table/#from-select-query).