ADD COLUMN

On this page Carat arrow pointing down

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 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.
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 A 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.

Examples

Add a Single Column

> ALTER TABLE accounts ADD COLUMN names STRING;
> 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

> ALTER TABLE accounts ADD COLUMN location STRING, ADD COLUMN amount DECIMAL;
> 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

> ALTER TABLE accounts ADD COLUMN interest DECIMAL NOT NULL DEFAULT (DECIMAL '1.3');
> 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

> ALTER TABLE accounts ADD COLUMN cust_number DECIMAL UNIQUE NOT NULL;

Add a Column with Collation

> 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

> ALTER TABLE accounts ADD COLUMN location1 STRING CREATE FAMILY new_family;

Add a Column and Assign it to an Existing Column Family

> 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

> ALTER TABLE accounts ADD COLUMN new_name STRING CREATE IF NOT EXISTS FAMILY f1;

See Also


Yes No
On this page

Yes No