MySQL ALTER TABLE Statement
The alter table
statement is used to do any modification to the schema of the table like add, modify or remove column(s) to the table.
We can also change the datatype of the column using the alter table
statement.
ALTER TABLE - ADD Column
In order to add a new column to the existing table we will use the following syntax.
Syntax for ADD Column statement
alter table table_name
ADD column_name datatype;
Example
alter table employees
add column role varchar(50);
ALTER TABLE - DROP Column
In order to drop a column from the existing table we will be using the following syntax.
Syntax for ADD Column statement
alter table table_name
drop column_name datatype;
Example
alter table employees
drop column role varchar;
ALTER TABLE - MODIFY Column
Suppose if we need to change the datatype of the existing column the we will be using MODIFY COLUMN
.
Syntax for ADD Column statement
alter table table_name
modify column column_name datatype;
Example
alter table employees
modify column role int;
The above query will modify the column type of the role
column to int
in the employees
table.