Table management
Learn how to create, edit, or delete a table. To create a table, you need a database.
Create a table
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
- In the management console, go to the folder page and select Yandex Database.
- Select the database to create a table in.
- Choose Create → Table on the right of the page.
- Configure the table settings:
- Name of the table. Must be unique within the database.
- Type of the table: YDB table.
- 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.
- 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.
- 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.
- 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).
- 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.
- Click Create table.
Create a document table
Note
Document tables are only available in Yandex Database serverless mode.
- In the management console, go to the folder page and select Yandex Database.
- Select the database to create a table in.
- Choose Create → Table on the right of the page.
- Configure the table settings:
- Name of the table. Must be unique within the database.
- Type of table: Document table.
- 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.
- Click Create table.
To create a table, specify:
- Table name.
- Names and data types for each column.
- 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.
- In the management console, go to the folder page and select Yandex Database.
- Select the database to update a table in.
- Find the table in the list and select → Change.
- 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.
- To delete non-key columns from the table, click in the appropriate line. You can't delete columns that make up a primary key.
- 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).
- 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.
- 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
- In the management console, go to the folder page and select Yandex Database.
- Select the database to delete a table from.
- Find the table in the list and select → Delete.
- 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
- In the management console, go to the folder page and select Yandex Database.
- Select the database to create a directory in.
- Choose Create → Directory on the right of the page.
- 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 |