This tutorial explains how to use the MySQL ALTER TABLE statement to add a column, modify a column, drop a column, rename a column or rename a table (with syntax and examples).
The MySQL ALTER TABLE statement is used to add, modify, or drop/delete columns in a table. The MySQL ALTER TABLE statement is also used to rename a table.
The syntax to add a column in a table in MySQL (using the ALTER TABLE statement) is:
The name of the table to modify.
The name of the new column to add to the table.
The datatype and definition of the column (NULL or NOT NULL, etc).
Optional. It tells MySQL where in the table to create the column. If this parameter is not specified, the new column will be added to the end of the table.
Let's look at an example that shows how to add a column in a MySQL table using the ALTER TABLE statement.
For example:
This MySQL ALTER TABLE example will add a column called last_name to the contacts table. It will be created as a NOT NULL column and will appear after the contact_id field in the table.
The syntax to add multiple columns in a table in MySQL (using the ALTER TABLE statement) is:
The name of the table to modify.
The name of the new column to add to the table.
The datatype and definition of the column (NULL or NOT NULL, etc).
Optional. It tells MySQL where in the table to create the column. If this parameter is not specified, the new column will be added to the end of the table.
Let's look at an example that shows how to add multiple columns in a MySQL table using the ALTER TABLE statement.
For example:
This ALTER TABLE example will add two columns to the contacts table - last_name and first_name.
The last_name field will be created as a varchar(40) NOT NULL column and will appear after the contact_id column in the table. The first_name column will be created as a varchar(35) NULL column and will appear after the last_name column in the table.
The syntax to modify a column in a table in MySQL (using the ALTER TABLE statement) is:
The name of the table to modify.
The name of the column to modify in the table.
The modified datatype and definition of the column (NULL or NOT NULL, etc).
Optional. It tells MySQL where in the table to position the column, if you wish to change its position.
Let's look at an example that shows how to modify a column in a MySQL table using the ALTER TABLE statement.
For example:
This ALTER TABLE example will modify the column called last_name to be a data type of varchar(50) and force the column to allow NULL values.
The syntax to modify multiple columns in a table in MySQL (using the ALTER TABLE statement) is:
The name of the table to modify.
The name of the column to modify in the table.
The modified datatype and definition of the column (NULL or NOT NULL, etc).
Optional. It tells MySQL where in the table to position the column, if you wish to change its position.
Let's look at an example that shows how to modify multiple columns in a MySQL table using the ALTER TABLE statement.
For example:
This ALTER TABLE example will modify two columns to the contacts table - last_name and first_name.
The last_name field will be changed to a varchar(55) NULL column and will appear after the contact_type column in the table. The first_name column will be modified to a varchar(30) NOT NULL column (and will not change position in the contacts table definition, as there is no FIRST | AFTER specified).
The syntax to drop a column in a table in MySQL (using the ALTER TABLE statement) is:
The name of the table to modify.
The name of the column to delete from the table.
Let's look at an example that shows how to drop a column in a MySQL table using the ALTER TABLE statement.
For example:
This ALTER TABLE example will drop the column called contact_type from the table called contacts.
The syntax to rename a column in a table in MySQL (using the ALTER TABLE statement) is:
The name of the table to modify.
The column to rename.
The new name for the column.
The datatype and definition of the column (NULL or NOT NULL, etc). You must specify the column definition when renaming the column, even if it does not change.
Optional. It tells MySQL where in the table to position the column, if you wish to change its position.
Let's look at an example that shows how to rename a column in a MySQL table using the ALTER TABLE statement.
For example:
This MySQL ALTER TABLE example will rename the column called contact_type to ctype. The column will be defined as a varchar(20) NOT NULL column.
The syntax to rename a table in MySQL is:
The table to rename.
The new table name to use.
Let's look at an example that shows how to rename a table in MySQL using the ALTER TABLE statement.
For example:
This ALTER TABLE example will rename the contacts table to people.