Class 12 | MySQL 15

Alter table Command

This command helps us to modify the structure of an existing table.

Structure of an existing table can be modified in two ways

(i) by adding a new column
(ii) by modifying the definition of an existing column.

(i). adding a new column

“Add” clause when used with alter table command helps us to add a new column.

Syntax:
alter table tablename
add (colname1 datatype(size),
Colname2 datatype(size) );


Std(roll int ,name char(10))

Alter table std add(age int);

Alter table std add(age int,per decimal);

Note:
Newly added column will contain NULL of all the existing records.
In order to insert values in the newly added columns we have to use the command “update”.

Update age=10 where roll=101;

(ii). modifying the definition of an existing column

Modify clause when used with alter table command helps us to modify the definition of an existing table.

Syntax:
alter table tablename
modify(oldcolname1 newdatatype(newsize),
oldcolname2 new datatype(newsize));

Example:

Std(roll int,name char(10),age int);

Alter table std
Modify(age decimal);

Alter table std
Modify (name char(20));