SQL Tutorial - ALTER Statement


The 'ALTER' statement. is used to alter the structure of a table. It is a 'Data Definition Langauge' (DDL) command.

The ALTER Statement


The 'ALTER" statement modifies the structure of a table by adding/modifying/deleting column(s) to it. It can also be used to add or drop a constraint on a table. Since it is DDL command, it is irrevocable.

SQL ALTER Syntax and Example


Alter table tablename add columnname datatype

Alter table Employees add JoinDate smalldatetime

The above command adds a new column to an existing table

Alter table tablename drop column columnname

Alter table Employees drop column JoinDate

The above command drops a column to an existing table

Alter table tablename Add constraint constraintname Primary key (columnname)

Alter table Designation Add constraint DesigPrimary Primary key (DesignationID)

The above command adds a primary key on a column in an existing table

Alter table tablename drop constraint constraintname

Alter table Designation drop constraint DesigPrimary

The above command drops a constraint on an existing table

Alter table tablename Alter column columnname datatype

Alter table Employees Alter column PhoneNo int

The above command changes the datatype of a column in an existing table