CREATE TABLE
This topic describes the syntax for creating a spatio-temporal data table.
Syntax
create_table_statement ::= CREATE TABLE [ IF NOT EXISTS ] table_name
'('
column_definition
( ',' column_definition )*
',' [constraint pk] PRIMARY KEY '(' primary_key ')'
')' WITH [ table_options ]
column_definition ::= column_name lql_type
primary_key ::= primary_item [ ',' primary_item ]
primary_item ::= column_name (ASC|DESC)
| [z-order] '(' [ column_name ( ',' column_name )* ] ')'The CREATE TABLE statement supports the IF NOT EXISTS option. A primary key consists of one or more columns.
Usage notes
Table name (table_identifier)
Note the following when you set a table name:
The name can contain digits, uppercase letters, lowercase letters, periods (.), 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)
Syntax element | Required | Usage notes |
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 if you use the TIMESTAMP data type:
|
NULL constraint | No | Specifies whether the column value can be NULL. Important Currently, Lindorm SQL does not validate NULL constraints. The storage engine performs this validation. The validation method for NULL constraints depends on the rules of the storage engine. Different storage engines have different rules. Therefore, you may be able to write NULL values even if you have specified NOT NULL. To ensure that data can be written correctly, follow these requirements:
|
Primary key (primary_key)
A 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.
You cannot modify a primary key 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 |
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 in LindormTable 2.7.7 or later and Lindorm SQL 2.8.6.0 or later.
To view your LindormTable and Lindorm SQL versions, see LindormTable version guide and SQL version guide. If you cannot upgrade to these versions in the console, 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
table_name_idx_${auto_increment_id}.Currently, you can create only secondary indexes and search indexes using index expressions.
Secondary index redundancy:
Lindorm SQL 2.9.3.10 and later
If you do not specify the
INCLUDEclause when you create a secondary index, all columns are not redundantly stored by default.Lindorm SQL versions earlier than 2.9.3.10
If you do not specify the
INCLUDEclause when you create a secondary index, all columns are redundantly stored by default.NoteThe redundancy effect is the same as setting the index property INDEX_COVERED_TYPE to
COVERED_ALL_COLUMNS_IN_SCHEMA. If the table created here is a dynamic table, the effect is the same as setting the property toCOVERED_DYNAMIC_COLUMNS.If you specify the
KEYorINDEXclause when you create a table and do not explicitly specify the MUTABILITY and CONSISTENCY properties, the table has the following properties by default:CONSISTENCY = 'strong',MUTABILITY='MUTABLE_LATEST'.If a
CREATE TABLEstatement includes an index expression, the system first creates the table and then creates the index. If an exception occurs during index creation, the table and index may remain in a residual state and are not automatically cleared or deleted. You can view them using statements such asSHOWandDESCRIBE, but you may be unable to write data or perform queries. We recommend that you clear the residual table and index and then recreate them.
Partition (partition_definition)
Partitions are supported only by LindormTSDB. In a time series table, the columns used for PARTITION BY must be VARCHAR columns 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 to hash-partition the table. For example: PARTITION BY HASH(c1, p1).
Table properties (table_options)
Only the wide table engine supports table properties (table_options). You can use the WITH keyword to add the following table properties:
Option (option_identifer) | Type | Description |
COMPRESSION | STRING | The compression algorithm for the table. The optional compression algorithms are:
Note For LindormTable versions earlier than 2.3.4, no compression algorithm is specified by default. For LindormTable 2.3.4 and later, the default compression algorithm is ZSTD. |
TTL | INT | The time-to-live (TTL) of data, in seconds (s). Note
|
COMPACTION_MAJOR_PERIOD | LONG | The period for the system to execute Note Default value: Math.Min(TTL, 1728000000 ms). If you do not set TTL, the default value of this parameter is 20 days (20 × 24 × 60 × 60 × 1000 ms = 1728000000 ms). |
MUTABILITY | STRING | An attribute related to indexes that specifies the mutability of the base table. The default value is All valid values are listed below:
For more information about the values, see High-performance native secondary indexes. Important After you create an index table, you cannot modify the value of the MUTABILITY parameter. To adjust this parameter, you must first delete all index tables. Back up your data before the operation to prevent data loss. |
CONSISTENCY | STRING | The consistency property of the table. For multi-zone instances, this parameter indicates the data consistency between the primary and standby data. It includes the following two levels:
Important For multi-zone instances, if your data involves read-after-write operations, such as increase, append, or index updates, 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 as the data type of the first column in the PRIMARY KEY | The start key and end key for pre-creating region partitions for the table. Note
|
SPLITKEYS | Same as the data type of the first column in the PRIMARY KEY | The start keys for all pre-created partitions of the table. Note
|
SPLITALGO | STRING | Defines the splitting algorithm for pre-created partitions. 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. For more information about dynamic columns, see Dynamic columns. |
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 column values. 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. 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
|
In LindormTable versions earlier than 2.2.16, the WITH keyword is not supported when you set table properties. You must enclose the table property keyword in single quotation marks ('). You can set property values based on their data types. If the property value is a string (STRING), you must also enclose the string 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 that supports the Geometry data type. This lets you write data of any spatial type to the table.
CREATE TABLE geoms(gid INT, g GEOMETRY, PRIMARY KEY(gid));Write data of any spatial data type.
UPSERT INTO geoms(gid, g) VALUES(0,ST_GeomFromText('POINT(-10.1 3.3)')),(1,ST_GeomFromText('LINESTRING(-12.2 4.3, -10.2 4.3)')),(2,ST_GeomFromText('POLYGON((2 2, 2 8, 8 8, 8 2, 2 2))'));Create a table that supports the Point data type. This lets you write only point data to the table.
CREATE TABLE pts(gid INT, g GEOMETRY(POINT), PRIMARY KEY(gid));Write point data.
UPSERT INTO pts(gid, g) VALUES(0, ST_MakePoint(0,0)),(1, ST_MakePoint(1,1));Create a table that supports the Polygon data type. This lets you write only polygon data to the table.
CREATE TABLE polys(gid INT, g GEOMETRY(POLYGON), PRIMARY KEY(gid));Write polygon data.
UPSERT INTO polys(gid,g) VALUES(0,ST_GeomFromText('POLYGON((2 2, 2 8, 8 8, 8 2, 2 2))'));Create a table that supports the LineString data type. This lets you write only line data to this table.
CREATE TABLE lines(gid INT, g GEOMETRY(LINESTRING), PRIMARY KEY(gid));Write line data.
UPSERT INTO lines(gid,g) VALUES(0, ST_GeomFromText('LINESTRING(-12.2 4.3, -10.2 4.3)'));Create a table that supports the MULTIPOINT data type. This lets you write only multipoint data to the table.
CREATE TABLE multipoints(gid INT, g GEOMETRY(MULTIPOINT), PRIMARY KEY(gid));Write multipoint data.
UPSERT INTO multipoints(gid,g) VALUES(0, ST_GeomFromText('MULTIPOINT (10 40, 40 30, 20 20, 30 10)'));Create a table that supports the MULTILINESTRING data type. This lets you write only multilinestring data to the table.
CREATE TABLE multilines(gid INT, g GEOMETRY(MULTILINESTRING), PRIMARY KEY(gid));Write multilinestring data.
UPSERT INTO multilines(gid,g) VALUES(0, ST_GeomFromText('MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))'));Create a table that supports the MULTIPOLYGON data type. This lets you write only multipolygon data to the table.
CREATE TABLE multipolys(gid INT, g GEOMETRY(MULTIPOLYGON), PRIMARY KEY(gid));Write multipolygon data.
UPSERT INTO multipolys(gid,g) VALUES(0, ST_GeomFromText('MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))'));Create a table that supports the GEOMETRYCOLLECTION data type. This lets you write only geometry collection data to the table.
CREATE TABLE collections(gid INT, g GEOMETRY(GEOMETRYCOLLECTION), PRIMARY KEY(gid));Write geometry collection data.
UPSERT INTO collections(gid,g) VALUES(0, ST_GeomFromText('GEOMETRYCOLLECTION (POINT (40 10), LINESTRING (10 10, 20 20, 10 40), POLYGON ((40 40, 20 45, 45 30, 40 40)))'));Create a table that contains multiple columns of the Geometry data type.
CREATE TABLE mix(gid INT, pt GEOMETRY(POINT), ply GEOMETRY(POLYGON), PRIMARY KEY(gid));