Description
You can use this statement to create a table in the database.
Syntax
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] table_name
(table_definition_list) [table_option_list] [partition_option] [AS] select;
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] table_name
LIKE table_name;
table_definition_list:
table_definition [, table_definition ...]
table_definition:
column_definition
| [CONSTRAINT [constraint_name]] PRIMARY KEY index_desc
| [CONSTRAINT [constraint_name]] UNIQUE {INDEX | KEY} [index_name] index_desc
| {INDEX | KEY} [index_name] index_desc
column_definition_list:
column_definition [, column_definition ...]
column_definition:
column_name data_type
[DEFAULT const_value] [AUTO_INCREMENT]
[NULL | NOT NULL] [[PRIMARY] KEY] [UNIQUE [KEY]] comment
index_desc:
(column_desc_list) [index_type] [index_option_list]
column_desc_list:
column_desc [, column_desc ...]
column_desc:
column_name [(length)] [ASC | DESC]
index_type:
USING BTREE
index_option_list:
index_option [ index_option ...]
index_option:
[GLOBAL | LOCAL]
| block_size
| compression
| STORING(column_name_list)
| comment
table_option_list:
table_option [ table_option ...]
table_option:
[DEFAULT] {CHARSET | CHARACTER SET} [=] charset_name
| [DEFAULT] COLLATE [=] collation_name
| primary_zone
| replica_num
| table_tablegroup
| block_size
| compression
| AUTO_INCREMENT [=] INT_VALUE
| comment
| DUPLICATE_SCOPE [=] "none|zone|region|cluster"
| LOCALITY [=] "locality description"
| ROW_FORMAT [=] REDUNDANT|COMPACT|DYNAMIC|COMPRESSED|DEFAULT
| PCTFREE [=] num
partition_option:
PARTITION BY HASH(expression)
[subpartition_option] PARTITIONS partition_count
| PARTITION BY KEY([column_name_list])
[subpartition_option] PARTITIONS partition_count
| PARTITION BY RANGE {(expression) | COLUMNS (column_name_list)}
[subpartition_option] (range_partition_list)
| PARTITION BY LIST {(expression) | COLUMNS (column_name_list)}
[subpartition_option] PARTITIONS partition_count
subpartition_option:
SUBPARTITION BY HASH(expression)
SUBPARTITIONS subpartition_count
| SUBPARTITION BY KEY(column_name_list)
SUBPARTITIONS subpartition_count
| SUBPARTITION BY RANGE {(expression) | COLUMNS (column_name_list)}
(range_subpartition_list)
| SUBPARTITION BY LIST(expression)
range_partition_list:
range_partition [, range_partition ...]
range_partition:
PARTITION partition_name
VALUES LESS THAN {(expression_list) | MAXVALUE}
range_subpartition_list:
range_subpartition [, range_subpartition ...]
range_subpartition:
SUBPARTITION subpartition_name
VALUES LESS THAN {(expression_list) | MAXVALUE}
expression_list:
expression [, expression ...]
column_name_list:
column_name [, column_name ...]
partition_name_list:
partition_name [, partition_name ...]
partition_count | subpartition_count:
INT_VALUE
Parameters
| Parameter | Description |
|---|---|
| DUPLICATE_SCOPE | Specifies the nature of a replica table. Valid values: none, zone, region, and cluster. * none: The table is a normal table. * zone: The table is a replica table. The leader must copy transactions to all F and R replicas in this zone. * region: The table is a replica table. The leader must copy transactions to all F and R replicas in the current region. * cluster: The table is a replica table. The leader must copy transactions to all F and R replicas in this cluster. If DUPLICATE_SCOPE is not specified, the default value none is used. |
| ROW_FORMAT | Specifies whether to enable the encoding storage format. * redundant * compact * dynamic * compressed * default |
| BLOCK_SIZE | Specifies the microblock size of the table. |
| COMPRESSION | Specifies the compression algorithm of the table. Valid values: 1. none: No compression algorithm is used. 2. lz4_1.0: The lz4 compression algorithm is used. 3. zstd_1.0: The zstd Compression algorithm is used. 4. snappy_1.0: The snappy compression algorithm is used. |
| CHARSET | CHARACTER SET | Specifies the default character set for columns in the table. Valid values: UTF8, UTF8MB4, GBK, UTF16, and GB18030. |
| COLLATE | Specifies the default comparison rule for columns in the table. Valid values: utf8_bin, utf8_general_ci, utf8_unicode_ci, gbk_bin, gbk_chinese_ci, utf8mb4_general_ci, utf8mb4__general_cs, utf8mb4_bin, utf8mb4_unicode_ci, utf16_general_ci, utf16_bin, utf16_unicode_ci, gb18030_chinese_ci, gb18030_bin |
| primary_zone | Specifies the primary zone where the leader replica is located. |
| replica_num | Specifies the number of replicas. |
| table_tablegroup | Specifies the table group to which the table belongs. |
| AUTO_INCREMENT | Specifies the initial value of auto-increment columns in the table. |
| comment | The comment. |
| LOCALITY | Specifies the distribution of replicas across the zones. For example, F@z1, F@z2, F@z3, and R@z4 indicate that z1, z2, and Z3 are full-featured replicas, and z4 is a read-only replica. |
| PCTFREE | Specifies the percentage of space reserved for macroblocks. |
Examples
- Creating a database table
CREATE TABLE test (c1 int primary key, c2 VARCHAR(50)) REPLICA_NUM = 3, PRIMARY_ZONE = 'zone1';
- Creating a replica table
CREATE TABLE item(id int, name varchar(10)) DUPLICATE_SCOPE="cluster";
- Creating an indexed table
CREATE TABLE t1 (c1 int primary key, c2 int, c3 int, index i1 (c2));
- Creating a table with eight hash partitions
CREATE TABLE t1 (c1 int primary key, c2 int) partition by hash(c1) partitions 8;
- Creating a table with range partitions and key subpartitions
CREATE TABLE t1 (c1 int, c2 int, c3 int)
partition by range(c1) subpartition by key(c2, c3) subpartitions 5
(partition p0 values less than(0), partition p1 values less than(100));
- Create a table with a column named gbk and a column named utf8.
CREATE TABLE t1 (c1 varchar(10),
c2 varchar(10) charset gbk collate gbk_bin)
default charset utf8 collate utf8mb4_general_ci;
- Enable encoding and zstd compression for the table and reserve 5% space for macroblocks.
CREATE TABLE t1 (c1 int, c2 int, c3 varchar(64))
compression 'zstd_1.0'
ROW_FORMAT dynamic
pctfree 5;