CREATE

CREATE is used to create new databases or tables.

CREATE DATABASE

Syntax

Creates a new database:

sql

  1. CREATE DATABASE [IF NOT EXISTS] db_name

If the db_name database already exists, then GreptimeDB has the following behaviors:

  • Doesn’t create a new database.
  • Doesn’t return an error when the clause IF NOT EXISTS is presented.
  • Otherwise, returns an error.

Examples

Creates a test database:

sql

  1. CREATE DATABASE test;

sql

  1. Query OK, 1 row affected (0.05 sec)

Creates it again with IF NOT EXISTS:

sql

  1. CREATE DATABASE IF NOT EXISTS test;

CREATE TABLE

Syntax

Creates a new table in the db database or the current database in use:

sql

  1. CREATE TABLE [IF NOT EXISTS] [db.]table_name
  2. (
  3. name1 [type1] [NULL|NOT NULL] [DEFAULT expr1] [TIME INDEX] [PRIMARY KEY] COMMENT comment,
  4. name2 [type2] [NULL|NOT NULL] [DEFAULT expr2] [TIME INDEX] [PRIMARY KEY] COMMENT comment,
  5. ...,
  6. [TIME INDEX (name)],
  7. [PRIMARY KEY(name1, name2,...)]
  8. ) ENGINE = engine WITH([ttl | regions] = expr, ...)
  9. [
  10. PARTITION BY RANGE COLUMNS(name1, name2, ...) (
  11. PARTITION r0 VALUES LESS THAN (expr1),
  12. PARTITION r1 VALUES LESS THAN (expr2),
  13. ...
  14. )
  15. ]

The table schema is specified by the brackets before the ENGINE. The table schema is a list of column definitions and table constraints. A column definition includes the column name, type, and options such as nullable or default values, etc. Please see below.

Table constraints

The table constraints contain the following:

  • TIME INDEX specifies the time index column, which always has one and only one column. It indicates the Timestamp type in the data model of GreptimeDB.
  • PRIMARY KEY specifies the table’s primary key column, which indicates the Tag type in the data model of GreptimeDB. It cannot include the time index column, but it always implicitly adds the time index column to the end of keys.
  • The Other columns are Field columns in the data model of GreptimeDB.

NOTE

The PRIMARY KEY specified in the CREATE statement is not the primary key in traditional relational databases. Actually, The PRIMARY KEY in traditional relational databases is equivalent to the combination of PRIMARY KEY and TIME INDEX in GreptimeDB. In other words, the PRIMARY KEY and TIME INDEX together constitute the unique identifier of a row in GreptimeDB.

The statement won’t do anything if the table already exists and IF NOT EXISTS is presented; otherwise returns an error.

Table options

Users can add table options by using WITH. The valid options contain the following:

OptionDescriptionValue
ttlThe storage time of the table dataString value, such as ‘60m’, ‘1h’ for one hour, ‘14d’ for 14 days etc. Supported time units are: s / m / h / d
regionsThe region number of the tableInteger value, such as 1, 5, 10 etc.
write_buffer_sizeMemtable size of the tableString value representing a valid size, such as 32MB, 128MB, etc. The default value of this option is 32MB. Supported units are: MB / GB.

For example, to create a table with the storage data TTL(Time-To-Live) is seven days and region number is 10:

sql

  1. CREATE TABLE IF NOT EXISTS temperatures(
  2. ts TIMESTAMP TIME INDEX,
  3. temperature DOUBLE DEFAULT 10,
  4. ) engine=mito with(ttl='7d', regions=10);

Column options

GreptimeDB supports the following column options:

OptionDescription
NULLThe column value can be null.
NOT NULLThe column value can’t be null.
DEFAULT exprThe column’s default value is expr, which its result type must be the column’s type
COMMENT commentThe column comment. It must be a string value

The table constraints TIME INDEX and PRIMARY KEY can also be set by column option, but they can only be specified once in column definitions. So you can’t specify PRIMARY KEY for more than one column except by the table constraint PRIMARY KEY :

sql

  1. CREATE TABLE system_metrics (
  2. host STRING PRIMARY KEY,
  3. idc STRING PRIMARY KEY,
  4. cpu_util DOUBLE,
  5. memory_util DOUBLE,
  6. disk_util DOUBLE,
  7. ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  8. TIME INDEX(ts)
  9. );

Goes wrong:

sql

  1. Illegal primary keys definition: not allowed to inline multiple primary keys in columns options

sql

  1. CREATE TABLE system_metrics (
  2. host STRING,
  3. idc STRING,
  4. cpu_util DOUBLE,
  5. memory_util DOUBLE,
  6. disk_util DOUBLE,
  7. ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP TIME INDEX,
  8. PRIMARY KEY(host, idc),
  9. );

sql

  1. Query OK, 0 rows affected (0.01 sec)

Region partition rules

TODO by MichaelScofield

CREATE EXTERNAL TABLE

Syntax

Creates a new file external table in the db database or the current database in use:

sql

  1. CREATE EXTERNAL TABLE [IF NOT EXISTS] [<database>.]<table_name>
  2. [
  3. (
  4. <col_name> <col_type> [NULL | NOT NULL] [COMMENT "<comment>"]
  5. )
  6. ]
  7. [ WITH
  8. (
  9. LOCATION = 'url'
  10. [,FORMAT = { csv | json | parquet } ]
  11. [,PATTERN = '<regex_pattern>' ]
  12. [,ENDPOINT = '<uri>' ]
  13. [,ACCESS_KEY_ID = '<key_id>' ]
  14. [,SECRET_ACCESS_KEY = '<access_key>' ]
  15. [,SESSION_TOKEN = '<token>' ]
  16. [,REGION = '<region>' ]
  17. [,ENABLE_VIRTUAL_HOST_STYLE = '<boolean>']
  18. ..
  19. )
  20. ]

Table options

OptionDescriptionRequired
LOCATIONExternal files locations, e.g., s3://<bucket>[<path>], /<path>/[<filename>]Required
FORMATTarget file(s) format, e.g., JSON, CSV, ParquetRequired
PATTERNUse regex to match files. e.g., *_today.parquetOptional

S3

OptionDescriptionRequired
REGIONAWS region name. e.g., us-east-1.Required
ENDPOINTThe bucket endpointOptional
ACCESS_KEY_IDACCESS_KEY_ID Your access key ID for connecting the AWS S3 compatible object storage.Optional
SECRET_ACCESS_KEYYour secret access key for connecting the AWS S3 compatible object storage.Optional
ENABLE_VIRTUAL_HOST_STYLEIf you use virtual hosting to address the bucket, set it to “true”.Optional
SESSION_TOKENYour temporary credential for connecting the AWS S3 service.Optional

Examples

You can create an external table without any columns definitions:

sql

  1. CREATE EXTERNAL TABLE IF NOT EXISTS city WITH (location='/var/data/city.csv',format='csv');

Or

sql

  1. CREATE EXTERNAL TABLE city (
  2. host string,
  3. ts int64,
  4. cpu float64 default 0,
  5. memory float64,
  6. TIME INDEX (ts),
  7. PRIMARY KEY(ts, host)
  8. ) WITH (location='/var/data/city.csv', format='csv');