ADD COLUMN

On this page Carat arrow pointing down
Warning:
CockroachDB v2.0 is no longer supported as of October 4, 2019. For more details, refer to the Release Support Policy.

The ADD COLUMN statement is part of ALTER TABLE and adds columns to tables.

Synopsis

ALTER TABLE IF EXISTS table_name ADD COLUMN IF NOT EXISTS column_name typename col_qualification

Required Privileges

The user must have the CREATE privilege on the table.

Parameters

Parameter Description
table_name The name of the table to which you want to add the column.
column_name The name of the column you want to add. The column name must follow these identifier rules and must be unique within the table but can have the same name as indexes or constraints.
typename The data type of the new column.
col_qualification An optional list of column definitions, which may include column-level constraints, collation, or column family assignments.

Note that it is not possible to add a column with the Foreign Key constraint. As a workaround, you can add the column without the constraint, then use CREATE INDEX to index the column, and then use ADD CONSTRAINT to add the Foreign Key constraint to the column.

Viewing Schema Changes

Whenever you initiate a schema change, CockroachDB registers it as a job, which you can view with SHOW JOBS.

Examples

Add a Single Column

icon/buttons/copy
> ALTER TABLE accounts ADD COLUMN names STRING;
icon/buttons/copy
> SHOW COLUMNS FROM accounts;
+-----------+-------------------+-------+---------+-----------+
|   Field   |       Type        | Null  | Default |  Indices  |
+-----------+-------------------+-------+---------+-----------+
| id        | INT               | false | NULL    | {primary} |
| balance   | DECIMAL           | true  | NULL    | {}        |
| names     | STRING            | true  | NULL    | {}        |
+-----------+-------------------+-------+---------+-----------+

Add Multiple Columns

icon/buttons/copy
> ALTER TABLE accounts ADD COLUMN location STRING, ADD COLUMN amount DECIMAL;
icon/buttons/copy
> SHOW COLUMNS FROM accounts;
+-----------+-------------------+-------+---------+-----------+
|   Field   |       Type        | Null  | Default |  Indices  |
+-----------+-------------------+-------+---------+-----------+
| id        | INT               | false | NULL    | {primary} |
| balance   | DECIMAL           | true  | NULL    | {}        |
| names     | STRING            | true  | NULL    | {}        |
| location  | STRING            | true  | NULL    | {}        |
| amount    | DECIMAL           | true  | NULL    | {}        |
+-----------+-------------------+-------+---------+-----------+

Add a Non-Null Column with a Default Value

icon/buttons/copy
> ALTER TABLE accounts ADD COLUMN interest DECIMAL NOT NULL DEFAULT (DECIMAL '1.3');
icon/buttons/copy
> SHOW COLUMNS FROM accounts;
+-----------+-------------------+-------+---------------------------+-----------+
|   Field   |       Type        | Null  |          Default          |  Indices  |
+-----------+-------------------+-------+---------------------------+-----------+
| id        | INT               | false | NULL                      | {primary} |
| balance   | DECIMAL           | true  | NULL                      | {}        |
| names     | STRING            | true  | NULL                      | {}        |
| location  | STRING            | true  | NULL                      | {}        |
| amount    | DECIMAL           | true  | NULL                      | {}        |
| interest  | DECIMAL           | false | ('1.3':::STRING::DECIMAL) | {}        |
+-----------+-------------------+-------+---------------------------+-----------+

Add a Non-Null Column with Unique Values

icon/buttons/copy
> ALTER TABLE accounts ADD COLUMN cust_number DECIMAL UNIQUE NOT NULL;

Add a Column with Collation

icon/buttons/copy
> ALTER TABLE accounts ADD COLUMN more_names STRING COLLATE en;

Add a Column and Assign it to a Column Family

Add a Column and Assign it to a New Column Family

icon/buttons/copy
> ALTER TABLE accounts ADD COLUMN location1 STRING CREATE FAMILY new_family;

Add a Column and Assign it to an Existing Column Family

icon/buttons/copy
> ALTER TABLE accounts ADD COLUMN location2 STRING FAMILY existing_family;

Add a Column and Create a New Column Family if Column Family Does Not Exist

icon/buttons/copy
> ALTER TABLE accounts ADD COLUMN new_name STRING CREATE IF NOT EXISTS FAMILY f1;

See Also


Yes No
On this page

Yes No