PHPGurukul

MySQL Altering tables

The format of an existing database table can be changed with an
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 :
ALTER TABLE table-name;
ADD COLUMN column-name data-type optional-modifier/s;
It can also ADD a PRIMARY KEY to an existing column definition using this syntax :
ALTER TABLE table-name;
ADD PRIMARY KEY (column-name);
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 :
ALTER TABLE table-name;
CHANGE old-column-name new-column-name data-type optional-modifiers/s;
delete an entire column from the table using the DROP COLUMN keywords :
ALTER TABLE table-name;
DROP COLUMN column-name;
Ex :
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;

Anuj Kumar

This is Anuj Kumar. I’m a professional web developer with 4+ year experience. I write blogs in my free time. I love to learn new technologies and share with others.
I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials.
.

Recommended Tutorials for you


Add comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Web Hosting with a FREE domain. On sale ₹99.00/mo