Skip to content

YDB DDL Examples

Anton Kovalenko edited this page Apr 25, 2024 · 4 revisions

Create ColumnTable

CREATE TABLE `my_column_table` (
  Key Uint64 not null,
  Value String,
  PRIMARY KEY (Key)
)
PARTITION BY HASH(Key)
WITH (
  STORE = COLUMN,
  AUTO_PARTITIONING_MIN_PARTITIONS_COUNT = 10
);

Add Secondary Index

docs

ALTER TABLE `my_table` ADD INDEX `name_index` GLOBAL ON (`name`);

Create Changefeed (CDC Stream)

docs

ALTER TABLE `my-table` ADD CHANGEFEED `my_cdc` WITH (
    FORMAT = 'JSON',
    MODE = 'UPDATES'
);

Table with options

docs

CREATE TABLE `table_with_config`
(
    `id` Uint64,
    `expire_at` Datetime,
    `name` String,
    PRIMARY KEY (`id`)
) 
WITH (
    AUTO_PARTITIONING_BY_SIZE = ENABLED,
    AUTO_PARTITIONING_PARTITION_SIZE_MB = 512,
    AUTO_PARTITIONING_BY_LOAD = ENABLED,
    AUTO_PARTITIONING_MIN_PARTITIONS_COUNT = 2,
    AUTO_PARTITIONING_MAX_PARTITIONS_COUNT = 8,
    UNIFORM_PARTITIONS = 4,
    READ_REPLICAS_SETTINGS = 'PER_AZ: 2',
    TTL = Interval("PT1H") ON expire_at,
    KEY_BLOOM_FILTER = ENABLED
)

External Data Source

docs

CREATE EXTERNAL DATA SOURCE `my_external_data_source` WITH (
  SOURCE_TYPE="ObjectStorage",
  LOCATION="my_data_source_location",
  AUTH_METHOD="NONE"
);

External Table

docs

CREATE EXTERNAL TABLE my_external_table (
  data json
) WITH (
  DATA_SOURCE="my_external_data_source",
  LOCATION="table_location",
  FORMAT="json_as_string"
);