CREATE TABLE

The CREATE TABLE call creates a table with the specified data schemaand key columns (PRIMARY KEY). It lets you define secondary indexes on the created table.

  1. CREATE TABLE table_name (
  2. column1 type1,
  3. column2 type2,


columnN typeN,
INDEX index1_name GLOBAL ON ( column ),
INDEX index2_name GLOBAL ON ( column1, column2, … ),
PRIMARY KEY (column, …),
FAMILY column_family ()
)
WITH ( key = value, … )

Columns

For key columns and non-key columns, only primitive data types are allowed.

All columns allow writing NULL values, that is, they are optional.
It is mandatory to specify the PRIMARY KEY with a non-empty list of columns. Those columns become part of the key in the listed order.

Example

  1. CREATE TABLE my_table (
  2. a Uint64,
  3. b Bool,
  4. c Float,

PRIMARY KEY (b, a)
)

Secondary indexes

The INDEX construct is used to define a secondary index in a table:

  1. CREATE TABLE table_name (
  2. ...
  3. INDEX <Index_name> GLOBAL [SYNC|ASYNC] ON ( <Index_columns> ) COVER ( <Cover_columns> ),
  4. ...
  5. )

CREATE TABLE - 图1

where:

  • Index_name is the unique name of the index to be used to access data.
  • SYNC/ASYNC indicates synchronous/asynchronous data writes to the index. If not specified, synchronous.
  • Index_columns is a list of comma-separated names of columns in the created table to be used for a search in the index.
  • Cover_columns is a list of comma-separated names of columns in the created table, which will be stored in the index in addition to the search columns, making it possible to fetch additional data without accessing the table for it.

Example

  1. CREATE TABLE my_table (
  2. a Uint64,
  3. b Bool,
  4. c Uft8,
  5. d Date,
  6. INDEX idx_a GLOBAL ON (d),
  7. INDEX idx_ca GLOBAL ASYNC ON (b, a) COVER ( c ),
  8. PRIMARY KEY (a)
  9. )

CREATE TABLE - 图2

Additional parameters

You can also specify a number of YDB-specific parameters for the table. When creating a table using YQL, such parameters are listed in the WITH section:

  1. CREATE TABLE table_name (...)
  2. WITH (
  3. key1 = value1,
  4. key2 = value2,
  5. ...
  6. )

CREATE TABLE - 图3

Here, key is the name of the parameter and value is its value.

For a list of possible parameter names and their values, see YDB table description.

For example, this code will create a table with enabled automatic partitioning by partition size and the preferred size of each partition is 512 MB:

Listing 4

  1. CREATE TABLE my_table (
  2. id Uint64,
  3. title Utf8,
  4. PRIMARY KEY (id)
  5. )
  6. WITH (
  7. AUTO_PARTITIONING_BY_SIZE = ENABLED,
  8. AUTO_PARTITIONING_PARTITION_SIZE_MB = 512
  9. );

CREATE TABLE - 图4

Column groups

Columns of the same table can be grouped to set the following parameters:

  • DATA: A storage type for the data in this column group. Acceptable values: ssd, hdd.
  • COMPRESSION: A data compression codec. Acceptable values: off, lz4.

By default, all columns are in the same group named default. If necessary, the parameters of this group can also be redefined.

In the example below, for the created table, the family_large group of columns is added and set for the series_info column, and the parameters for the default group, which is set by default for all other columns, are also redefined.

  1. CREATE TABLE series_with_families (
  2. series_id Uint64,
  3. title Utf8,
  4. series_info Utf8 FAMILY family_large,
  5. release_date Uint64,
  6. PRIMARY KEY (series_id),
  7. FAMILY default (
  8. DATA = "ssd",
  9. COMPRESSION = "off"
  10. ),
  11. FAMILY family_large (
  12. DATA = "hdd",
  13. COMPRESSION = "lz4"
  14. )
  15. );

CREATE TABLE - 图5