DDL
Used to define the structure and organization of the database.
CREATE DATABASE
CREATE DATABASE [IF NOT EXISTS] db_name [WITH db_options];
db_options:
db_option ...
db_option: {
TTL value
| SHARD value
| VNODE_DURATION value
| REPLICA value
| PRECISION {'ms' | 'us' | 'ns'}
}
| Options | Description |
|---|---|
TTL | Data expiration time, default unlimited storage, supported units: d, h, m.Example: TTL 10d, TTL 180m. |
SHARD | Indicates the number of shards, default: 1. |
VNODE_DURATION | The time window length of the data in SHARD, default 365 days, supported units: d, h, m.Example: TTL 10d, TTL 180m. |
REPLICA | The number of replicas of the data in the cluster, default is 1.(Note: The number of copies must be less than or equal to the data nodes of tskv in the cluster). |
PRECISION | The timestamp precision of the database, default is ns.Supported units: ms, us, ns |
View the CREATE DATABASE example
CREATE TABLE
CREATE TABLE uses CnosDB TSKV to store data, supporting storage of some SQL data types as follows:
BIGINTBIGINT UNSIGNEDBOOLEANTIMESTAMPSTRINGDOUBLE
In addition, the structure of the CnosDB table needs to follow certain specifications, please refer to Basic Concepts.
CREATE TABLE [IF NOT EXISTS] tb_name
(field_definition [, field_definition ] ... [, TAGS(tg_name [, tg_name] ...)]);
field_definition:
column_name data_type [field_codec_type]
field_codec_type:
CODEC(code_type)
View the CREATE TABLE example
CREATE EXTERNAL TABLE
The external table is read-only and cannot perform DML operations.
-- Column definitions can not be specified for PARQUET files
CREATE EXTERNAL TABLE [ IF NOT EXISTS ] tb_name
( field_definition [, field_definition] ... ) tb_option;
field_definition:
column_name data_type [ NULL ]
tb_option: {
STORED AS { PARQUET | NDJSON | CSV | AVRO }
| [ WITH HEADER ROW ]
| [ DELIMITER 'a_single_char' ]
| [ PARTITIONED BY ( column_name, [, ... ] ) ]
| LOCATION '<file>'
}
| Options | Description |
|---|---|
STORED AS | Specify the format of the file, supporting PARQUET, NDJSON, CSV, AVRO. |
WITH HEADER ROW | Only effective when STORED AS CSV, used to specify the Header in the CSV file. |
DELIMITER | Only effective when STORED AS CSV, used to specify the delimiter in the CSV file. |
PARTITIONED BY | Partitioning by the columns specified when creating the table. |
LOCATION | Location of the associated file, supports directories. |
View the CREATE EXTERNAL TABLE example
CREATE STREAM TABLE
Need a source table, STREAM table does not support ALTER
CREATE STREAM TABLE [IF NOT EXISTS] table_name(field_definition [, field_definition] ...)
WITH (db = 'db_name', table = 'table_name', event_time_column = 'time_column')
engine = tskv;
field_definition:
column_name data_type
Syntax:DROP TABLE
ALTER DATABASE
ALTER DATABASEcan modify all parameters set in the database (excludingPRECISION), you can query the current database parameter settings using the DESCRIBE DATABASE command.
ALTER DATABASE db_name [alter_db_options]
alter_db_options:
SET db_option
db_option: {
TTL value
| SHARD value
| VNODE_DURATION value
| REPLICA value
}
View the ALTER DATABASE example
ALTER TABLE
Support modifying/deleting columns of FIELD types, column types can be queried using the DESCRIBE TABLE syntax.
Do not support modifying the column name time.
ALTER TABLE tb_name alter_table_option;
alter_table_option: {
ADD FIELD col_name [data_type] [CODEC(code_type)]
| ALTER col_name SET CODEC(code_type)
| DROP col_name
| RENAME COLUMN col_name TO new_col_name
}
View the ALTER TABLE example
DROP DATABASE
DROP DATABASE [IF EXISTS] db_name;
View the DROP DATABASE example
DROP TABLE
DROP TABLE [ IF EXISTS ] tb_name;