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.

FieldTypeAttributesNullDefaultExtra
idint(2)
Noauto_increment
namevarchar(50)
No
classvarchar(10)
No
marksint(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.

FieldTypeAttributesNullDefaultExtra
idint(2)
Noauto_increment
namevarchar(50)
No
classvarchar(10)
No
candidate_ marksint(3)
No0

Pretty simple :)

Leave a Review

Searches for Topic