MySQL Alter Table

MySQL ALTER TABLE command is used to add, modify, or delete columns in an existing table.

ALTER TABLE - ADD Column

To add a column in a table, use the following syntax:

ALTER TABLE [table_name] ADD [column_name] [datatype];

The following SQL Command adds an "doj" column to the "student" table:

ALTER TABLE student ADD doj varchar(10);

ALTER TABLE - MODIFY Column

To change the data type of a column in a table, use the following syntax:

ALTER TABLE [table_name] MODIFY COLUMN [column_name] [datatype];

The following SQL Command modifies an "doj" column to the "student" table:

ALTER TABLE student MODIFY COLUMN doj DATE;

ALTER TABLE - DROP Column

To delete a column in a table, use the following syntax:

ALTER TABLE [table_name] DROP COLUMN [column_name];

The following SQL deletes the "email" column from the "student" table:

ALTER TABLE [table_name] DROP COLUMN email;

ALTER TABLE - Add constraints

ALTER TABLE statement is also used to add and drop various constraints on an existing table.

ALTER TABLE [table_name] DROP COLUMN [column_name];

The following SQL deletes the "email" column from the "student" table:

ALTER TABLE [table_name] DROP COLUMN email;