The CREATE TABLE statement creates a table in LindormTable or LindormTSDB. It supports multiple data types, such as VARCHAR, TIMESTAMP, and BIGINT. It also supports multiple table attributes, such as data Time to Live (TTL) and the COMPRESSION algorithm. You can combine attributes and data types to design tables that best suit your business scenarios.
Engines and versions
CREATE TABLE applies to LindormTable and LindormTSDB.
LindormTSDB supports the standard CREATE TABLE syntax starting from version 3.4.32. To view or upgrade the LindormTSDB version, see LindormTSDB version history and Upgrade a minor version.
Syntax
create_table_statement ::= CREATE TABLE [ IF NOT EXISTS ] table_identifier
'('
column_definition
( ',' column_definition )*
',' PRIMARY KEY '(' primary_key ')'
( ',' {KEY|INDEX} [index_identifier]
[ USING index_method_definition ]
[ INCLUDE column_identifier ( ',' column_identifier )* ]
[ WITH index_options ]
)*
')'
[ PARTITION BY partition_definition ]
[ WITH table_options ]
column_definition ::= column_identifier data_type [ NOT NULL ]
primary_key ::= column_identifier [ ',' column_identifier (ASC|DESC)]
index_method_definition ::= { KV | SEARCH }
index_options ::= '('
option_definition (',' option_definition )*
')'
partition_definition ::= HASH '(' column_identifier (',' column_identifier )* ')'
table_options ::= '(' option_definition (',' option_definition )* ')'
option_definition ::= option_identifer '=' string_literal Differences
The CREATE TABLE syntax differs significantly between LindormTable and LindormTSDB. The following table compares the differences.
Syntax element | LindormTable | LindormTSDB |
〇 | 〇 | |
〇 | 〇 | |
〇 | 〇 | |
〇 | ✖️ | |
✖️ | 〇 | |
〇 | ✖️ |
Usage notes
Table name (table_identifier)
Note the following requirements for table names:
Can contain digits, uppercase letters, lowercase letters, periods (.), hyphens (-), and underscores (_).
Cannot start with a period (.) or a hyphen (-).
Must be 1 to 255 characters in length.
Column definition (column_definition)
Syntax element | Required | Description |
Column name (column_identifier) | Yes |
|
Data type (data_type) | Yes | For information about supported data types, see Data types. Important When you create a time series table, note the following points if you use the TIMESTAMP data type:
|
NULL constraint | No | Specifies whether the column value can be NULL. Important Lindorm SQL does not check for NULL constraints. This check is performed by the storage engine. The method for checking NULL constraints depends on the validation rules of the storage engine. The rules vary for different storage engines. Therefore, you may be able to write NULL values even if you specify NOT NULL. To ensure that data is written correctly, follow these requirements:
|
Primary key (primary_key)
The primary key is the unique identifier for data in a table. It consists of one or more columns. You must specify a primary key (PRIMARY KEY) when you create a table.
The primary key cannot be modified after it is created. Design your primary key carefully.
When you use the CREATE TABLE statement to create a table, note the following:
Engine type | Primary key usage notes |
LindormTable |
|
LindormTSDB |
|
Index expression (KEY|INDEX)
When you use the CREATE TABLE statement to create an index table, you can use the KEY or INDEX keyword to specify the index to create.
The index expression (KEY|INDEX) is supported only by LindormTable 2.7.7 and later and Lindorm SQL 2.8.6.0 and later.
For information about the versions of LindormTable and Lindorm SQL, see LindormTable version history and SQL version history. If you cannot upgrade to the required versions in the console, you can contact Lindorm technical support on DingTalk at s0s3eg3.
Usage notes
If you do not explicitly specify an index name, the system generates one by default. The format of the generated index name is
TableName_idx_${Auto-incremented_Number}.Currently, you can only create secondary indexes and search indexes using index expressions.
Redundancy of secondary indexes:
Lindorm SQL 2.9.3.10 and later
If you create a secondary index without specifying
INCLUDE, all columns are not included by default.Lindorm SQL versions earlier than 2.9.3.10
If you create a secondary index without specifying
INCLUDE, all columns are included by default.NoteThis is equivalent to setting the INDEX_COVERED_TYPE index attribute to
COVERED_ALL_COLUMNS_IN_SCHEMA. If the table is a dynamic table, this is equivalent toCOVERED_DYNAMIC_COLUMNS.If you specify the
KEYorINDEXclause when you create a table but do not explicitly specify the MUTABILITY and CONSISTENCY attributes, the table has two default attributes:CONSISTENCY = 'strong'andMUTABILITY='MUTABLE_LATEST'.If the CREATE TABLE statement includes an index expression, the system first creates the table and then creates the index. If an exception occurs during index creation, the created table and index might remain. They are not automatically cleared or deleted. You can view them using statements such as
SHOWandDESCRIBE. However, you might not be able to write data or run queries. We recommend that you clear the remaining table and index and then create them again.
Partition (partition_definition)
Partitioning is supported only by LindormTSDB. In a time series table, the column used for PARTITION BY must be a VARCHAR column in the primary key definition.
When you create a table, you can use the PARTITION BY HASH(column1, column2, ..., columnN) statement to explicitly specify one or more columns for hash partitioning. For example: PARTITION BY HASH(c1, p1).
Table attributes (table_options)
Table attributes (table_options) are supported only by LindormTable. You can use the WITH keyword to add the following table attributes:
Option (option_identifer) | Type | Description |
COMPRESSION | STRING | The compression algorithm for the table. Valid values are:
Note In LindormTable versions earlier than 2.3.4, no compression algorithm is specified by default. In LindormTable 2.3.4 and later, the ZSTD algorithm is used by default. |
TTL | INT | The time-to-live (TTL) of data, in seconds (s). Note
|
COMPACTION_MAJOR_PERIOD | LONG | The period for the system to perform a Note Default value: Math.Min(TTL, 1728000000 ms). If you do not set a TTL, the default value of this parameter is 20 days (20 × 24 × 60 × 60 × 1000 ms = 1728000000 ms). |
MUTABILITY | STRING | Related to indexes. It classifies the write patterns for the primary table. The default value is All valid values are listed below:
For more information about the values, see High-performance native secondary indexes. Important The value of the MUTABILITY parameter cannot be modified after an index table is created. To adjust this parameter, you must first delete all index tables. We recommend that you back up your data before the operation to prevent data loss. |
CONSISTENCY | STRING | The consistency attribute of the table. For a multi-zone instance, this parameter specifies the data consistency between the primary and replica data. It includes the following two levels:
Important For a multi-zone instance, if data is read before it is written, such as in increase, append, or index update operations, you must set the CONSISTENCY parameter of the primary table to |
NUMREGIONS | INT | The number of regions to pre-create for the table. |
CHS | INT | The boundary for hot and cold data separation, in seconds. Note
|
STARTKEY and ENDKEY | Same data type as the first column in the PRIMARY KEY | The start and end keys for pre-created region partitions. Note
|
SPLITKEYS | Same data type as the first column in the PRIMARY KEY | The start keys for all pre-split partitions of the table. Note
|
SPLITALGO | STRING | Defines the splitting algorithm for pre-partitioning. Currently, only the following splitting algorithms are supported:
|
DYNAMIC_COLUMNS | STRING | Specifies whether to enable dynamic columns. Valid values:
Note Dynamic columns support only the Varbinary type. |
VERSIONS | STRING | The number of versions to retain for a column value. The value must be an integer greater than or equal to 1. The default value is 1, which means one version is retained. Lindorm supports retaining multiple versions of a column value. For more information about multi-version data management, see Multi-version data management. Important A large value for the VERSIONS parameter may affect data query and storage performance. Avoid setting an excessively large value. We recommend that you set VERSIONS to 1. |
BLOB_BUCKET_NAME | STRING | Creates a bucket for a table that contains BLOB columns. The value is a custom bucket name. The bucket name must follow these rules:
Note
|
LindormTable versions earlier than 2.2.16 do not support the WITH keyword for setting table attributes. You must enclose the table attribute keyword in single quotation marks ('). The attribute value is set based on its type. If the attribute value is a string, enclose it in single quotation marks ('). For example, CREATE TABLE IF NOT EXISTS t1(c1 varchar, c2 bigint, c3 int, c4 int, PRIMARY KEY(c1,c2)) 'CONSISTENCY'='strong';.
Examples
Create a table
CREATE TABLE sensor (
device_id VARCHAR NOT NULL,
region VARCHAR NOT NULL,
time TIMESTAMP NOT NULL,
temperature DOUBLE,
humidity BIGINT,
PRIMARY KEY(device_id, region, time)
);Verify the result
Execute the DESCRIBE table sensor; statement to check whether the table is created.
Create an index when you create a table
Create a secondary index when you create a table.
CREATE TABLE IF NOT EXISTS sensor (
device_id VARCHAR NOT NULL,
region VARCHAR NOT NULL,
time TIMESTAMP NOT NULL,
temperature DOUBLE,
humidity BIGINT,
PRIMARY KEY(device_id, region, time),
KEY (temperature, time)
);Verify the result
Execute the DESCRIBE table sensor; statement to check whether the table is created, and then execute SHOW INDEX FROM sensor; to check whether the secondary index is created.
Specify the data TTL and compression algorithm
Create a wide table, set the data TTL to 30 days (2,592,000 seconds), and set the compression algorithm to ZSTD.
CREATE TABLE sensor (
device_id VARCHAR NOT NULL,
region VARCHAR NOT NULL,
time TIMESTAMP NOT NULL,
temperature DOUBLE,
humidity BIGINT,
PRIMARY KEY(device_id, region, time)
) WITH (COMPRESSION='ZSTD', TTL='2592000');Verify the result
Execute the
DESCRIBE table sensor;statement to check whether the table is created.On the Overview page of the cluster management system, click the name of the destination table in the destination database. In the Current Table Details area, click View Table Properties to view the values of the COMPRESSION and TTL parameters.
Specify the major compaction period
Create a wide table and set the major compaction period to 10 days (864,000,000 milliseconds).
CREATE TABLE sensor (
device_id VARCHAR NOT NULL,
region VARCHAR NOT NULL,
time TIMESTAMP NOT NULL,
temperature DOUBLE,
humidity BIGINT,
PRIMARY KEY(device_id, region, time)
) WITH (COMPACTION_MAJOR_PERIOD='864000000');Verify the result
Execute the
DESCRIBE table sensor;statement to check whether the table is created.On the Overview page of the cluster management system, click the name of the destination table in the destination database. In the Current Table Details area, click View Table Properties to view the value of the COMPACTION_MAJOR_PERIOD parameter.
Enable dynamic columns
Create a wide table and set the DYNAMIC_COLUMNS parameter to TRUE to support writing dynamic column data to the table.
CREATE TABLE sensor (
device_id VARCHAR NOT NULL,
region VARCHAR NOT NULL,
time TIMESTAMP NOT NULL,
temperature DOUBLE,
humidity BIGINT,
PRIMARY KEY(device_id, region, time)
) WITH (DYNAMIC_COLUMNS='TRUE');Verify the result
Execute the
DESCRIBE table sensor;statement to check whether the table is created.On the Overview page of the cluster management system, click the name of the destination table in the destination database. In the Current Table Details area, click View Table Properties to view the value of the DYNAMIC_COLUMNS parameter.
Specify the boundary for hot and cold data separation
Create a wide table and set the boundary for hot and cold data separation. The system archives data based on this boundary.
CREATE TABLE sensor (
device_id VARCHAR NOT NULL,
region VARCHAR NOT NULL,
time TIMESTAMP NOT NULL,
temperature DOUBLE,
humidity BIGINT,
PRIMARY KEY(device_id, region, time)
) WITH (CHS = '86400', CHS_L2 = 'storagetype=COLD');Verify the result
Execute the
DESCRIBE table sensor;statement to check whether the table is created.On the Overview page of the cluster management system, click the name of the destination table in the destination database. In the Current Table Details area, click View Table Properties to view the values of the CHS and CHS_L2 parameters.
Set multiple attributes at the same time
Create a wide table and specify the compression algorithm, data TTL, and the boundary for hot and cold data separation.
CREATE TABLE sensor (
device_id VARCHAR NOT NULL,
region VARCHAR NOT NULL,
time TIMESTAMP NOT NULL,
temperature DOUBLE,
humidity BIGINT,
PRIMARY KEY(device_id, region, time)
) WITH (
COMPRESSION='ZSTD',
CHS = '86400',
CHS_L2 = 'storagetype=COLD',
TTL = '2592000');Verify the result
Execute the
DESCRIBE table sensor;statement to check whether the table is created.On the Overview page of the cluster management system, click the name of the destination table in the destination database. In the Current Table Details area, click View Table Properties to view the values of the COMPRESSION, CHS, CHS_L2, and TTL parameters.
Set partitions
Create a wide table with five predefined partitions. The start key of the partitions is 1000 and the end key is 9000.
CREATE TABLE sensor (
p1 INTEGER NOT NULL,
c1 INTEGER,
c2 VARCHAR,
c3 VARCHAR,
PRIMARY KEY(p1)
) WITH (NUMREGIONS='5', STARTKEY='1000', ENDKEY='9000');Verify the result
Execute the
DESCRIBE table sensor;statement to check whether the table is created.On the Overview page of the cluster management system, click the name of the destination table in the destination database. In the Shard Details area, you can view the values of the startKey and endKey parameters for each shard.
Set the start keys of multiple partitions
Create a wide table and predefine the start keys for five partitions. After the table is created, it contains six pre-split partitions by default.
CREATE TABLE sensor (
p1 INT NOT NULL,
p2 INT NOT NULL,
c1 VARCHAR,
c2 BIGINT,
PRIMARY KEY(p1, p2)
) WITH (SPLITKEYS = '100000,300000,500000,700000,900000');Verify the result
Execute the
DESCRIBE table sensor;statement to check whether the table is created.On the Overview page of the cluster management system, click the name of the destination table in the destination database. In the Shard Details area, you can view the values of the startKey and endKey parameters for each shard.
Explicitly specify a partition key column
Most query scenarios involve querying the instantaneous data of a single device. Therefore, you can explicitly specify device_id as the partition key column for data partitioning.
CREATE TABLE sensor (
device_id VARCHAR NOT NULL,
region VARCHAR NOT NULL,
time TIMESTAMP NOT NULL,
temperature DOUBLE,
humidity BIGINT,
PRIMARY KEY(device_id, region, time)
) PARTITION BY HASH(device_id);Verify the result
Execute the DESCRIBE TABLE sensor; statement to verify the result.