ADD COLUMN Tutorial
Table of contents
- Add single column - Add a single column to a table.
- Add multiple columns - Add multiple columns to a table.
- Column constraints - Add constraints like
UNIQUEto a column.
Add a single column
ALTER TABLE table_name ADD column_name data_type;
table_name- The name of the table you want to alter.
column_name- The name of the column you want to add to the table.
data_type- A supported data type.
The following command adds a column
building_name to the
ALTER TABLE rent_payments ADD building_name VARCHAR;
Add multiple columns
To add multiple columns, just list the columns inside parentheses as follows.
ALTER TABLE table_name ADD COLUMN first_column_name data_type, ADD COLUMN second_column_name data_type;
The following command adds two columns to the
ALTER TABLE rent_payments ADD COLUMN building_name varchar, ADD COLUMN unit_number int;
Add any column constraints after the column data type as follows.
ALTER TABLE table_name ADD column_name constraints;
The following command creates a column
payment_id with a
PRIMARY KEY constraint.
ALTER TABLE rent_payments ADD payment_id INT PRIMARY KEY;