In previous SQL post we had discussed about Data Definition Language and Data Manipulation Language and CREATE Command.
ALTER Command – DDL Statements
The ALTER command used to modify the structure of table without deleting and recreating.
Once we created table in database, some time we may need to modify the definition of it. The ALTER command allows you to make changes to the structure of a table without deleting.
We can use ADD, MODIFY and DROP command with ALTER command.
ADD – ADD used to add new columns in existing table.
ALTER – Modify the definition like datatype and size of columns in table.
DROP – Remove the column from table.
Syntax of Add column
ALTER TABLE tablename
ADD columns1 datatype
Example of Add new column
ADD StudentSurname new columns in Student table.
ALTER TABLE Student
ADD StudentSurname varchar(50) NULL
Syntax of Modify column
ALTER TABLE tablename
MODIFY column datatype
Example of MODIFY column
Modify the StudentSurname columns size from 50 to 100.
ALTER TABLE Student
MODIFY (StudentSurname varchar(100))
Above statement modify the StudentSurname columns size, the new columns size is 100.
Syntax of DROP column
ALTER TABLE tablename
DROP column
Example of DROP column
Drop the column StudentSurname from table Student.
ALTER TABLE Student
DROP StudentSurname