MySQL 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
PRIMARY KEY
orUNIQUE
to a column.
Add a single column
Syntax
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.
Example
The following command adds a column building_name
to the rent_payments
table.
ALTER TABLE rent_payments ADD building_name VARCHAR(10);
Add multiple columns
To add multiple columns, just list the columns inside parentheses as follows.
ALTER TABLE
table_name
ADD (
first_column_name data_type,
second_column_name data_type
);
Example
The following command adds two columns to the rent_payments
table.
ALTER TABLE
rent_payments
ADD (
building_name VARCHAR(10),
unit_number INT
);
Column constraints
Add any column constraints after the column data type as follows.
ALTER TABLE table_name ADD column_name constraints;
Example
The following command creates a column payment_id
with a PRIMARY KEY
constraint.
ALTER TABLE rent_payments ADD payment_id INT PRIMARY KEY;