Hi friends, welcome to my academic blog section on SQL. Today, I’ll try to illustrate about various ways that you can use “alter” command in SQL.

As name suggest, alter command is used for alteration of existing relation(table) structures in database. This very command can be used for various purposes which includes:

to add a column to existing table

to rename any existing column

to change datatype of any column or to modify its size.

to drop a column.


  • To Add Column to existing Table

Using alter command we can add a column to an existing table. Following is the Syntax,

alter table table-name add(column-name datatype);

Here is an Example for this,

alter table Student add(address char);

The above command will add a new column address to the Student table


  • To Add Multiple Column to existing Table

Using alter command we can even add multiple columns to an existing table. Following is the Syntax,

alter table table-name add(column-name1 datatype1, column-name2 datatype2, column-name3 datatype3);

Here is an Example for this,

alter table Student add(father-name varchar(60), mother-name varchar(60), dob date);

The above command will add three new columns to the Student table


  • To Add column with Default Value

alter command can add a new column to an existing table with default values. Following is the Syntax,

alter table table-name add(column-name1 datatype1 default data);

Here is an Example for this,

alter table Student add(dob date default '1-Jan-99');

The above command will add a new column with default value to the Student table


  • To Modify an existing Column

alter command is used to modify data type of an existing column . Following is the Syntax,

alter table table-name modify(column-name datatype);

Here is an Example for this,

alter table Student modify(address varchar(30));

The above command will modify address column of the Student table


  • To Rename a column

Using alter command you can rename an existing column. Following is the Syntax,

alter table table-name rename old-column-name to column-name;

Here is an Example for this,

alter table Student rename address to Location;

The above command will rename address column to Location.


  • To Drop a Column

alter command is also used to drop columns also. Following is the Syntax,

alter table table-name drop(column-name);

Here is an Example for this,

alter table Student drop(address);

The above command will drop address column from the Student table

LEAVE A REPLY

Please enter your comment!
Please enter your name here