Yandex.Cloud
  • Services
  • Why Yandex.Cloud
  • Pricing
  • Documentation
  • Contact us
Get started
Yandex Database
  • Getting started
    • Overview
    • Create databases
    • Examples of YQL queries
    • Examples of operations in the YDB CLI
    • Launch a test app
    • Document API
    • Developing in NodeJS through the Document API
  • Step-by-step instructions
    • Database management
    • How to connect to a database
    • Table management
    • Reading and writing data
    • Working with secondary indexes
  • Working with the SDK
  • Concepts
    • Overview
    • Data model and schema
    • Serverless and Dedicated operation modes
    • Data types
    • Transactions
    • Secondary indexes
    • Time to Live (TTL)
    • Terms and definitions
    • Quotas and limits
  • Access management
  • Pricing policy
    • Overview
    • Serverless mode
    • Dedicated mode
  • Recommendations
    • Schema design
    • Partitioning tables
    • Secondary indexes
    • Paginated output
    • Loading large data volumes
    • Using timeouts
  • YDB API and API reference
    • Database limits
    • Handling errors in the API
  • Amazon DynamoDB-compatible HTTP API
    • API reference
      • All methods
      • Actions
        • BatchGetItem
        • BatchWriteItem
        • CreateTable
        • DeleteItem
        • DeleteTable
        • DescribeTable
        • GetItem
        • ListTables
        • PutItem
        • Query
        • Scan
        • TransactGetItems
        • TransactWriteItems
        • UpdateItem
      • Common errors
  • YQL reference guide
    • Overview
    • Data types
      • Simple
      • Optional
      • Containers
      • Special
    • Syntax
      • Unsupported statements
      • For text representation of data types
      • Expressions
      • CREATE TABLE
      • DROP TABLE
      • INSERT INTO
      • UPSERT INTO
      • REPLACE INTO
      • UPDATE
      • DELETE
      • SELECT
      • GROUP BY
      • JOIN
      • FLATTEN
      • ACTION
      • DISCARD
      • PRAGMA
      • DECLARE
      • OVER, PARTITION BY, and WINDOW
    • Built-in functions
      • Basic
      • Aggregate
      • Window
      • For lists
      • For dictionaries
      • For JSON
      • For structures
      • For types
    • Preset user-defined functions
      • HyperScan
      • Pcre
      • Pire
      • Re2
      • String
      • Unicode
      • Datetime
      • Url
      • Ip
      • Digest
      • Math
      • Histogram
    • For text representation of data types
  • YQL tutorial
    • Overview
    • Creating a table
    • Adding data to a table
    • Selecting data from all columns
    • Selecting data from specific columns
    • Sorting and filtering
    • Data aggregation
    • Additional selection criteria
    • Joining tables by JOIN
    • Data insert and update by REPLACE
    • Data insert and update by UPSERT
    • Data insert by INSERT
    • Data update by UPDATE
    • Deleting data
    • Adding and deleting columns
    • Deleting a table
  • Maintenance
    • Backups
  • Diagnostics
    • System views
  • Questions and answers
    • General questions
    • Errors
    • YQL
    • All questions on the same page
  • Public materials
  1. Step-by-step instructions
  2. Table management

Table management

  • Create a table
  • Change the table structure
  • Delete table
  • Create and delete directories

Learn how to create, edit, or delete a table. To create a table, you need a database.

Create a table

Management console
SQL

You can create two types of tables:

  • A YDB table is a relational table with a primary key index. It is always ordered by primary key.

  • A document table is a table type compatible with Amazon DynamoDB. This type of table is only available in Yandex Database serverless mode.

    A document table contains data represented as a set of items. Each item is a set of attributes. An attribute is a data unit represented in a document table as a key-value pair. Attributes are much like columns in relational tables. When creating a document table, you must specify a primary key that serves as a unique identifier for table items. A primary key is a set of attributes. All table items must contain attributes that are part of the table's primary key. Except for the primary key, items may contain arbitrary attributes of arbitrary types.

Create a YDB table

  1. In the management console, go to the folder page and select Yandex Database.
  2. Select the database to create a table in.
  3. Choose Create → Table on the right of the page.
  4. Configure the table settings:
    • Name of the table. Must be unique within the database.
    • Type of the table: YDB table.
  5. Add columns:
    • Column name. Must be unique within the table.
    • Column data type. Regardless of the data type, each column may contain a NULL value.
    • Primary key. Specify whether the column is part of the primary key. Primary indexes are automatically created based on the primary key.
  6. If necessary, set up secondary indexes:
    • Secondary index name. Must be unique within the table.
    • Key: One or more columns that make up a key for creating a secondary index.
  7. Set up the partitioning policy:
    • No: The table is not partitioned.
    • Uniform: The entire range of values of Uint32 or Uint64 key columns (from 0 to the maximum value) is split into same-length intervals. When using this policy, set the number of intervals in the Quantity field.
    • Explicit: Lets you explicitly specify values for keys that will act as boundaries for the initial partitioning of the table. To add another boundary value, click Add split point.
  8. Configure automatic partitioning:
    • By size: If enabled, a partition is split into two when a certain data size is reached.
    • By load: If enabled, a partition is split into two if it is under high loads for a certain period of time (uses a lot of CPU time).
  9. Configure advanced table settings:
    • Autopartition by size MBs: Threshold data size at which auto partitioning by size is triggered.
    • Min partitions: The number of partitions in the table below which no partition merge by size or load is performed.
    • Max partitions: The number of partitions in the table above which no splitting by size or load is performed.
    • Key bloom filter: If enabled, YDB uses a Bloom filter to search data by key. In some cases, it can speed up key reads.
  10. Click Create table.

Create a document table

Note

Document tables are only available in Yandex Database serverless mode.

  1. In the management console, go to the folder page and select Yandex Database.
  2. Select the database to create a table in.
  3. Choose Create → Table on the right of the page.
  4. Configure the table settings:
    • Name of the table. Must be unique within the database.
    • Type of table: Document table.
  5. Add columns:
    • Column name. Must be unique within the table.
    • Column data type. Regardless of the data type, each column may contain a NULL value.
    • Partition key: A simple primary key that consists of a single attribute. YDB uses the partition key value as input for the internal hashing function. The result of calculating the hash function determines the partition where the item will be stored.
    • Sort key. A primary key can be composite and consist of a partition key and a sort key. All items with the same partition key will be stored together, in sorted order by the sort key value. If a partition key and a sort key are specified in a document table, two elements may contain the same value for the partition key, but must contain different values for the sort key.
  6. Click Create table.

To create a table, specify:

  1. Table name.
  2. Names and data types for each column.
  3. Columns that make up a primary key. YDB tables require a primary key.

YDB tables have the following characteristics:

  • Each column may contain a NULL value.
  • If you try to create a table with an existing name, no action is taken.
  • Primary indexes are based on the primary key. They are unique and created automatically.

For more information about the YDB data model and schema, see Data model and schema. Read more about supported data types in Data types.

The CREATE TABLE statement creates a table with the specified parameters:

CREATE TABLE series
(
    series_id Uint64,
    title Utf8,
    series_info Utf8,
    release_date Uint64,
    PRIMARY KEY (series_id)
);

CREATE TABLE seasons
(
    series_id Uint64,
    season_id Uint64,
    title Utf8,
    first_aired Uint64,
    last_aired Uint64,
    PRIMARY KEY (series_id, season_id)
);

CREATE TABLE episodes
(
    series_id Uint64,
    season_id Uint64,
    episode_id Uint64,
    title Utf8,
    air_date Uint64,
    PRIMARY KEY (series_id, season_id, episode_id)
);

As a result, a simple series table is created with the series_id, title, series_info, and release_date columns. The series_id column is the primary key of the table. The seasons and episodes tables are described the same way.

Note

For more information about how to add secondary indexes when creating a table, see Adding secondary indexes.

Change the table structure

In YDB, you can add non-key columns to a table and change its automatic partitioning settings.

Management console
SQL
  1. In the management console, go to the folder page and select Yandex Database.
  2. Select the database to update a table in.
  3. Find the table in the list and select → Change.
  4. Add new columns to the table and specify their parameters:
    • Column name. Must be unique within the table.
    • Column data type. Regardless of the data type, each column may contain a NULL value.
  5. To delete non-key columns from the table, click in the appropriate line. You can't delete columns that make up a primary key.
  6. Configure automatic partitioning:
    • By size: If enabled, a partition is split into two when a certain data size is reached.
    • By load: If enabled, a partition is split into two if it is under high loads for a certain period of time (uses a lot of CPU time).
  7. Configure advanced table settings:
    • Autopartition by size MBs: Threshold data size at which auto partitioning by size is triggered.
    • Min partitions: The number of partitions in the table below which no partition merge by size or load is performed.
    • Max partitions: The number of partitions in the table above which no splitting by size or load is performed.
    • Key bloom filter: If enabled, YDB uses a Bloom filter to search data by key. In some cases, it can speed up key reads.
  8. Click Edit table.

ADD COLUMN adds a column with the specified name and type. The code below adds the is_deleted column with the Bool data type to the episodes table.

ALTER TABLE episodes ADD COLUMN is_deleted Bool;

DROP COLUMN deletes the column with the specified name. The code below removes the is_deleted column from the episodes table.

ALTER TABLE episodes DROP COLUMN is_deleted;

Delete table

Management console
SQL
  1. In the management console, go to the folder page and select Yandex Database.
  2. Select the database to delete a table from.
  3. Find the table in the list and select → Delete.
  4. Confirm the deletion.

DROP TABLE deletes the specified table. If there is no such table, an error is returned. The code below returns the Table not found error, since we didn't create an actors table yet.

DROP TABLE actors;

Create and delete directories

Management console
SDK
  1. In the management console, go to the folder page and select Yandex Database.
  2. Select the database to create a directory in.
  3. Choose Create → Directory on the right of the page.
  4. Enter the directory name and click Create directory.

The SDK provides methods for creating, deleting, listing, and viewing directory information.

The table below contains the names of SDK methods for Java, Python, and Go.

Method Java Python Go
Creating a directory SchemeClient.makeDirectory scheme_client.make_directory Client.MakeDirectory
Deleting a directory SchemeClient.removeDirectory scheme_client.remove_directory Client.RemoveDirectory
Viewing information about a directory SchemeClient.describePath SchemeClient.describeDirectory Client.DescribePath
Listing a directory scheme_client.describe_path SchemeClient.list_directory Client.ListDirectory
In this article:
  • Create a table
  • Change the table structure
  • Delete table
  • Create and delete directories
Language
Careers
Privacy policy
Terms of use
© 2021 Yandex.Cloud LLC