Page 257 - PYTHON-12
P. 257
For example, ALTER TABLE student ADD(City char(6) DEFAULT “DELHI”);
The above command will add a new column City with default value as “DELHI” to the student table.
Resultant table: student
Rollno Name Gender Marks DOB Mobile_no City
1. Raj Kumar M 93 17-Nov-2000 NULL DELHI
2. Deep Singh M 98 22-Aug-1996 NULL DELHI
3. Ankit Sharma M 76 02-Feb-2000 NULL DELHI
4. Radhika Gupta F 78 03-Dec-1999 NULL DELHI
5. Payal Goel F 82 21-April-1998 NULL DELHI
6. Diksha Sharma F 80 17-Dec-1999 NULL DELHI
7. Gurpreet Kaur F 65 04-Jan-2000 NULL DELHI
8. Akshay Dureja M 90 05-May-1997 NULL DELHI
9. Shreya Anand F 70 08-Oct-1999 NULL DELHI
10. Prateek Mittal M 75 25-Dec-2000 NULL DELHI
C. Modifying an existing column definition:
The MODIFY clause can be used with ALTER TABLE command to change the datatype, size,
constraint related to any column of the table.
Syntax for modifying existing column datatype:
ALTER TABLE <table_name>
MODIFY([column_name1] <datatype1>);
For example,
ALTER TABLE student MODIFY (Name varchar(25));
The above command will modify the datatype size for the Name field from 20 to 25 characters.
D. Renaming a column:
The existing column in a relation can be renamed using ALTER TABLE command.
Syntax for renaming an existing column:
ALTER TABLE <table_name>
CHANGE [COLUMN] <old-column-name> <new-column-name> column_definition;
For example,
ALTER TABLE student
CHANGE City State varchar(10);
The above command shall rename the City column to State.
Rollno Name Gender Marks DOB Mobile_no City
1. Raj Kumar M 93 17-Nov-2000 NULL DELHI
2. Deep Singh M 98 22-Aug-1996 NULL DELHI
3. Ankit Sharma M 76 02-Feb-2000 NULL DELHI
4. Radhika Gupta F 78 03-Dec-1999 NULL DELHI
5. Payal Goel F 82 21-April-1998 NULL DELHI
6. Diksha Sharma F 80 17-Dec-1999 NULL DELHI
7. Gurpreet Kaur F 65 04-Jan-2000 NULL DELHI Relational Database and SQL
8. Akshay Dureja M 90 05-May-1997 NULL DELHI
9. Shreya Anand F 70 08-Oct-1999 NULL DELHI
10. Prateek Mittal M 75 25-Dec-2000 NULL DELHI
12.27