SQL ALTER Command

by on October 5th, 2007

Table structure can be changed by using alter command. With this command Field type or property can be changed or a new field can be added. If you decide to change a field to UNIQUE so it will not accept any duplicate records then if some records with duplicate value are already there then system will not allow this. Same way we have to take care of other constraints.

Field Type Attributes Null Default Extra
id int(2)
No auto_increment
name varchar(50)
No
class varchar(10)
No
marks int(3)
No

We will apply alter table command to this table and change field name marks to candidate_marks.

ALTER TABLE `student3` CHANGE `marks` `candidate_marks` INT( 3 ) DEFAULT '0' NOT NULL

With this alter table command the field name marks will change to candidate_marks. This way we are changing a field name only. Same way field type, default value and other properties of the field can be changed. The new table structure is listed below.

Field Type Attributes Null Default Extra
id int(2)
No auto_increment
name varchar(50)
No
class varchar(10)
No
candidate_ marks int(3)
No 0

Pretty simple :)


Share