MySQL Add Column: Syntax & Examples
To add a column to a MySQL table, you can use the ALTER TABLE statement. Below is the basic syntax for adding a column to a table:
ALTER TABLE table_name
ADD column_name data_type [constraints];
Add a column called email to a table named users, with a data type of VARCHAR and a length of 50.
ALTER TABLE users
ADD email VARCHAR(50);
You can also specify constraints when adding a column, such as the NOT NULL constraint.
ALTER TABLE users
ADD phone_number VARCHAR(20) NOT NULL;
Please note that when adding a column, the new column will be added at the end of the existing columns in the table. You can also use the AFTER keyword to specify where the new column should be inserted after.
ALTER TABLE users
ADD age INT AFTER email;
When using the ALTER TABLE statement to add a column, make sure to back up the data in the table to prevent accidental deletions or changes to the data.