Changing the composition of columns
YDB supports adding columns to row and column tables, as well as deleting non-key columns from tables.
ADD COLUMN
Builds a new column with the specified name, type, and options for the specified table.
ALTER TABLE table_name ADD COLUMN column_name column_data_type [FAMILY <family_name>] [NULL | NOT NULL] [DEFAULT <default_value>];
Request parameters
table_name
The path of the table to be modified.
column_name
The name of the column to be created. When choosing a name for the column, consider the common column naming rules.
column_data_type
The data type of the column. The complete list of data types supported by YDB is available in the YQL data types section.
FAMILY <family_name> (column setting)
Specifies that this column belongs to the specified column group. For more information, see Column groups.
DEFAULT <default_value>
Warning
The DEFAULT option is supported:
- Only for row-oriented tables. Support for column-oriented tables is under development.
- Only with literal values. Support for computed expressions is under development.
Allows you to set a default value for a column. If no value is specified for this column when inserting a row, the specified default value will be used. The default value must match the column's data type.
The DEFAULT false NOT NULL construct is invalid due to ambiguity in interpretation. In this case, use a comma-separated list or change the order of options.
NULL
This column can contain NULL values (default).
NOT NULL
This column does not accept NULL values.
Example
The code below will add a column named views with data type Uint64 to the episodes table.
ALTER TABLE episodes ADD COLUMN views Uint64;
The code below will add a column named rate with data type Double and default value 5.0 to the episodes table.
ALTER TABLE episodes ADD COLUMN rate Double NOT NULL DEFAULT 5.0;
ALTER TABLE episodes ADD COLUMN rate Double (DEFAULT 5.0, NOT NULL); -- alternative syntax
DROP COLUMN
Deletes a column with the specified name from the specified table.
ALTER TABLE table_name DROP COLUMN column_name;
Request parameters
table_name
The path of the table to be modified.
column_name
The name of the column to be deleted.
Example
The code below will delete the column named views from the episodes table.
ALTER TABLE episodes DROP COLUMN views;