You can use the CREATE TABLE syntax to create tables in LindormTable and LindormTSDB. This syntax supports various data types such as VARCHAR, TIMESTAMP, and BIGINT and can configure multiple attributes for tables, such as the time-to-live (TTL) of data in the table and the algorithm used to compress data in the table. You can flexibly configure the data types and table attributes to create tables that meet your requirements.
Applicable engines and versions
The CREATE TABLE syntax is applicable to LindormTable and LindormTSDB.
The CREATE TABLE syntax is supported by LindormTSDB 3.4.32 and later. For information about how to view and upgrade the LindormTSDB version of a Lindorm instance, see Release notes of LindormTSDB and Upgrade the minor engine version of a Lindorm instance.
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
Supported parameters
The supported parameters of the CREATE TABLE syntax are different in LindormTable and LindormTSDB. The following table describes the parameters supported by LindormTable and LindormTSDB.
Parameter | LindormTable | LindormTSDB |
〇 | 〇 | |
〇 | 〇 | |
〇 | 〇 | |
〇 | ✖️ | |
️️️️️️✖️ | 〇 | |
〇 | ️️️️️️✖️ |
Parameters
Table name (table_identifier)
The table name must meet the following requirements:
The name can contain digits, letters, commas (,), hyphens (-), and underscores (_).
The name cannot start with a period (.) or a hyphen (-).
The name must be 1 to 255 characters in length.
Column definition (column_definition)
Option | Required | Description |
Column name (column_identifier) | Yes |
|
Data types (data type) | Yes | For more information about data types that are supported by this syntax, see Data types. Important If you want to specify the TIMESTAMP type when you create a time series table, take note of the following items:
|
NULL constraint | No | Specifies whether the value of this option can be NULL. Important Lindorm SQL does not verify the NULL values of options. These values are verified by the engine. The verification method of NULL values depends on the verification rules of the engine. The verification rules vary with the engine. Therefore, NULL values may be written to the table even if you set this option to NOT NULL. To ensure that data can be written to the table, take note of the following requirements:
|
Primary key (primary_key)
The primary key of a table uniquely identifies the data in the table. A primary key consists of one or more columns. You must specify a primary key when you create a table.
You cannot modify the primary key. Make sure that you specify an appropriate primary key when you create the table.
When you use the CREATE TABLE
syntax to create a table, take note of the items described in the following table.
Engine | Primary key usage |
LindormTable |
|
LindormTSDB |
|
Index expression (KEY|INDEX)
To create an index table by using the CREATE TABLE
statement, you can use the KEY
or INDEX
keyword to specify the index that you want 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 more information about how to view the version of LindormTable and Lindorm SQL, see Release notes of LindormTable and SQL versions. If you cannot update your engine to the required version in the console, contact Lindorm technical support (DingTalk ID: s0s3eg3).
When you use the index expression, take note of the following items:
If you do not explicitly specify the index name, the system generates an index name. Format: <table_name>_idx_${auto-increment number}
.
Secondary indexes and search indexes can be created only by using the index expression. For more information about index types, see the CREATE INDEX section of the "CREATE INDEX" topic.
If you create a secondary index and do not specify the INCLUDE
expression, the created index contains all columns. This is equivalent to setting the index attribute INDEX_COVERED_TYPE
to COVERED_ALL_COLUMNS_IN_SCHEMA
. If the created table is a dynamic table, it is equivalent to setting the index attribute INDEX_COVERED_TYPE
to COVERED_DYNAMIC_COLUMNS
.
For more information about the INDEX_COVERED_TYPE
attribute, see the Index attributes (index_options) section of the "CREATE INDEX" topic.
If you specify the KEY
or INDEX
clause but do not explicitly specify the MUTABILITY
and CONSISTENCY
attributes, the table has two table attributes: CONSISTENCY ='strong'
and MUTABILITY='MUTABLE_LATEST'
.
For more information about the CONSISTENCY
and MUTABILITY
attributes, see the Table attributes (table_options) section of the "CREATE TABLE" topic.
If the index expression is included in the CREATE TABLE
statement, the system first creates the table and then creates the index. If an exception occurs while creating the index, the created table and index may remain in the database and will not be automatically cleared or deleted. You can view the table and index by executing a statement such as SHOW
or DESCRIBE
, but you may not be able to write data to or query the table or index. We recommend that you clear and then recreate the table and index.
Partitioning (partition_definition)
Partitioning is supported only by LindormTSDB. In a time series table, the partition key column specified by the PARTITION BY clause must be the VARCHAR column defined in the primary key.
When you create a table, you can use the PARTITION BY HASH(column1, column2, ..., columnN)
clause to specify the columns based on which hash partitioning is performed on the table. Example: PARTITION BY HASH(c1, p1)
.
Table attributes (table_options)
This parameter is supported only by LindormTable. You can use the WITH
keyword to add the attributes described in the following table to the table that you want to create.
Attribute (option_identifer) | Type | Description |
COMPRESSION | STRING | The algorithm used to compress the data in the table. Valid values:
Note In LindormTable whose version is earlier than 2.3.4, no compression algorithm is used by default. In LindormTable 2.3.4 and later, the ZSTD algorithm is used by default. |
TTL | INT | The TTL value of data in the table. Unit: seconds. Note
|
COMPACTION_MAJOR_PERIOD | LONG | The interval at which the Note The default interval is calculated by using the following formula: Math.Min(TTL,1728000000ms). If you do not specify a TTL, the value of this attribute is set to 1728000000 ms (equivalent to 20 days) by default. |
MUTABILITY | STRING | An attribute related to indexes. This option specifies the mutability of the base table. Default value: Valid values:
For more information about the values, see Secondary indexes. Important The value of the MUTABILITY attribute cannot be modified after the index table is created. To adjust this attribute, you must first delete all index tables. We recommend that you back up your data before adjustment to avoid data loss. |
CONSISTENCY | STRING | The consistency level of the table. Default value: eventual. For a cross-zone instance, this parameter specifies the data consistency between the primary table and secondary table. Valid values:
Important For a cross-zone instance, if data is read and then written in increase, append, and index update operations, you must set the CONSISTENCY parameter of the primary table to |
NUMREGIONS | INT | The number of data regions in the table that you want to create. |
CHS | INT | The time period after which data is stored by using cold storage. Unit: seconds. Note
|
STARTKEY and ENDKEY | Same data type as that of the first column in the primary key specified by PRIMARY KEY | The start key and end key of each region. Note
|
SPLITKEYS | Same data type as that of the first column in the primary key specified by PRIMARY KEY | The start keys of all pre-split regions. Note
|
SPLITALGO | STRING | The splitting algorithm used for pre-partitioning. The following two algorithms are supported:
|
DYNAMIC_COLUMNS | STRING | Specifies whether to enable dynamic columns. Default value: False. Valid values:
Note Dynamic columns support only the VARBINARY data type. For more information about dynamic columns, see Dynamic columns. |
VERSIONS | STRING | The number of versions that are retained for column data. The value of this parameter is an integer greater than or equal to 1. The default value of this parameter is 1, which indicates that only one version is retained for column data. Lindorm allows you to retain multiple versions of column data. For more information, see Use hints to implement data versioning. Important If you set VERSIONS to a large value, the query and storage performance of the table may be degraded. We recommend that you do not set this attribute to a large value. Recommended value: 1. |
BLOB_BUCKET_NAME | STRING | The name of the OSS bucket that is created for tables that contain BLOB columns. The bucket name must meet the following requirements:
Note
|
LindormTable whose version is earlier than 2.2.16 does not support the WITH
keyword. In this case, you must enclose the attribute name with a pair of quotation marks ('). The attribute value can be configured based on the data type of the attribute. If the attribute value is a string, you must enclose the string with a pair of quotation marks ('). 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
You can 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
You can execute the DESCRIBE table sensor;
statement to check whether the table is created and execute the SHOW INDEX FROM sensor;
statement to check whether the secondary index is created.
Specify the TTL and compression algorithm
Create a wide table and set the data TTL of the table to 2,592,000 seconds (equivalent to 30 days) and 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
You can execute the
DESCRIBE table sensor;
statement to check whether the table is created.Log on to the cluster management system. On the Overview page, click the name of the table that you want to check. In the Current table details section, click View table properties. On the page that appears, check the values of COMPRESSION and TTL. For more information about how to log on to the cluster management system, see Log on to the cluster management system.
Specify the interval at which the major compaction operation is performed
Create a wide table and set the interval at which the major compaction operation is performed to 864,000,000 ms (equivalent to 10 days).
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
You can execute the
DESCRIBE table sensor;
statement to check whether the table is created.Log on to the cluster management system. On the Overview page, click the name of the table that you want to check. In the Current table details section, click View table properties. On the page that appears, check the value of COMPACTION_MAJOR_PERIOD.
Enable dynamic columns
Create a wide table and set DYNAMIC_COLUMNS to TRUE
.
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
You can execute the
DESCRIBE table sensor;
statement to check whether the table is created.Log on to the cluster management system. On the Overview page, click the name of the table that you want to check. In the Current table details section, click View table properties. On the page that appears, check the value of DYNAMIC_COLUMNS.
Specify the boundary based on which hot and cold data separation is performed
Create a wide table and specify the boundary based on which hot and cold data separation is performed.
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
You can execute the
DESCRIBE table sensor;
statement to check whether the table is created.Log on to the cluster management system. On the Overview page, click the name of the table that you want to check. In the Current table details section, click View table properties. On the page that appears, check the values of CHS and CHS_L2.
Configure multiple attributes
Create a wide table and specify the compression algorithm, TTL, and the boundary based on which hot and cold data separation is performed.
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
You can execute the
DESCRIBE table sensor;
statement to check whether the table is created.Log on to the cluster management system. On the Overview page, click the name of the table that you want to check. In the Current table details section, click View table properties. On the page that appears, check the values of COMPRESSION, CHS, CHS_L2, and TTL.
Configure partitioning
Create a wide table and pre-split the table into five regions whose start key is 1000 and 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
You can execute the
DESCRIBE table sensor;
statement to check whether the table is created.Log on to the cluster management system. On the Overview page, click the name of the table that you want to check. In the Shard information section, view the startKey and endKey values of each region.
Specify the start keys of multiple regions
Create a wide table and pre-define the start keys of five regions. In this case, the table contains six pre-split regions.
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
You can execute the
DESCRIBE table sensor;
statement to check whether the table is created.Log on to the cluster management system. On the Overview page, click the name of the table that you want to check. In the Shard information section, check the startKey and endKey values of each region.
Explicitly specify a partition key column
In most queries, the instantaneous data of a single device is queried. Therefore, you can explicitly specify device_id as the partition key column.
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
You can execute the DESCRIBE TABLE sensor;
statement to check whether the table is created.