MySQL Altering tables
The format of an existing database table can be changed with an ALTER TABLE query . This query can make a single alteration or specify as number of alterations as comma-separated list.
It can also ADD a PRIMARY KEY to an existing column definition using this syntax :
An ALTER TABLE query can CHANGE the name of an existing column. The new column will not inherit any data type or modifiers specified to the original column – these must be set a new in the ALTER TABLE query, like this :
delete an entire column from the table using the DROP COLUMN keywords :
Ex :
In the ADD COLUMN and DROP COLUMN examples the COLUMN keyword is optional- it is what the manual calls “a pure noise word” that is only there to aid readability.An ALTER TABLE query can ADD a complete new COLUMN to an existing table , like this :
1 2 |
ALTER TABLE table-name; ADD COLUMN column-name data-type optional-modifier/s; |
1 2 |
ALTER TABLE table-name; ADD PRIMARY KEY (column-name); |
1 2 |
ALTER TABLE table-name; DROP COLUMN column-name;<!-- /wp:code --><!-- wp:paragraph --> |
1 2 3 4 5 6 |
ALTER TABLE Emp_Data ADD PRIMARY KEY (id), ADD COLUMN code INT UNIQUE NOT NULL, CHANGE Emp_Name VARCHAR(100) NOT NULL, DROP COLUMN Emp_Address; |