To change the structure of a table, we use the Alter table syntax.
There are a lot of options that MySQL gives for Alter table. We will add a column, remove a column, change the column's type, and change the foreign key constraint.
We will see few examples in the command-line client and some examples in the MySQL workbench.
Users require the Alter, Create, and Insert privileges to use the Alter Table command.
ALTER TABLE <table-name> ADD <column-name> <data type> <any conditions>
To add the column salary to the EMPLOYEES table in the somedb database.
ALTER TABLE EMPLOYEES ADD COLUMN SALARY VARCHAR(40);
ALTER TABLE <table-name> MODIFY <column-name> <data type/condition>
To modify a column in a table "ratings," we do:
ALTER TABLE RATINGS MODIFY PERCENTAGE FLOAT;
ALTER TABLE <table-name> ADD FOREIGN KEY (<column-name>) REFERENCES <other table-name> (<column_name>)
Add the foreign key to the employees table
ALTER TABLE EMPLOYEES
ADD FOREIGN KEY (rating) REFERENCES ratings(rating_id);
ALTER TABLE <table-name> DROP COLUMN <column-name>;
To drop a column in a table.
ALTER TABLE EMPLOYEES DROP COLUMN PERFORMANCE;