ALTER TABLE

Using the ALTER TABLE command, you can change the composition of columns and additional table parameters. You can specify several actions in one command. In general, the ALTER TABLE command looks like this:

  1. ALTER TABLE table_name action1, action2, ..., actionN;

ALTER TABLE - 图1

action: Any action to change the table described below.

Changing the composition of columns

YDB lets you add columns to a table and delete non-key columns from it.

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.

  1. ALTER TABLE episodes ADD COLUMN is_deleted Bool;

ALTER TABLE - 图2

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

  1. ALTER TABLE episodes DROP column is_deleted;

ALTER TABLE - 图3

Adding or removing a secondary index

ADD INDEX: Adds an index with the specified name and type for a given set of columns. The code below adds a global index named title_index for the title column.

  1. ALTER TABLE `series` ADD INDEX `title_index` GLOBAL ON (`title`);

ALTER TABLE - 图4

You can specify any index parameters from the CREATE TABLE command.

Deleting an index:

DROP INDEX: Deletes the index with the specified name. The code below deletes the index named title_index.

  1. ALTER TABLE `series` DROP INDEX `title_index`;

ALTER TABLE - 图5

Renaming a table

  1. ALTER TABLE old_table_name RENAME TO new_table_name;

ALTER TABLE - 图6

If a table with a new name exists, an error is returned. The possibility of transactional table substitution under load is supported by ad-hoc CLI and SDK methods.

If a YQL query contains multiple ALTER TABLE ... RENAME TO ... commands, each of them will be executed in autocommit mode as a separate transaction. From the external process viewpoint, the tables will be renamed sequentially one by one. To rename multiple tables within a single transaction, use ad-hoc methods available in the CLI and SDK.

Renaming can be used to move a table from one directory inside the database to another, for example:

  1. ALTER TABLE `table1` RENAME TO `/backup/table1`;

ALTER TABLE - 图7

Changing column groups

ADD FAMILY: Creates a new group of columns in the table. The code below creates the family_small column group in the series_with_families table.

  1. ALTER TABLE series_with_families ADD FAMILY family_small (
  2. DATA = "ssd",
  3. COMPRESSION = "off"
  4. );

ALTER TABLE - 图8

Using the ALTER COLUMN command, you can change a column group for the specified column. The code below for the release_date column in the series_with_families table changes the column group to family_small.

  1. ALTER TABLE series_with_families ALTER COLUMN release_date SET FAMILY family_small;

ALTER TABLE - 图9

The two previous commands from listings 8 and 9 can be combined into one ALTER TABLE call. The code below creates the family_small column group and sets it for the release_date column in the series_with_families table.

  1. ALTER TABLE series_with_families
  2. ADD FAMILY family_small (
  3. DATA = "ssd",
  4. COMPRESSION = "off"
  5. ),
  6. ALTER COLUMN release_date SET FAMILY family_small;

ALTER TABLE - 图10

Using the ALTER FAMILY command, you can change the parameters of the column group. The code below changes the storage type to hdd for the default column group in the series_with_families table:

  1. ALTER TABLE series_with_families ALTER FAMILY default SET DATA "hdd";

ALTER TABLE - 图11

You can specify any column family parameters from the CREATE TABLE command.

Changing additional table parameters

Most of the table parameters in YDB described on the table description page can be changed with the ALTER command.

In general, the command to change any table parameter looks like this:

  1. ALTER TABLE table_name SET (key = value);

ALTER TABLE - 图12

key is a parameter name and value is its new value.

For example, this command disables automatic partitioning of the table:

  1. ALTER TABLE series SET (AUTO_PARTITIONING_BY_SIZE = DISABLED);

ALTER TABLE - 图13

Resetting additional table parameters

Some table parameters in YDB listed on the table description page can be reset with the ALTER command.

The command to reset the table parameter looks like this:

  1. ALTER TABLE table_name RESET (key);

ALTER TABLE - 图14

key: Name of the parameter.

For example, this command resets (deletes) TTL settings for the table:

  1. ALTER TABLE series RESET (TTL);

ALTER TABLE - 图15