How we change the name of the existing column of the table?
We use Alter command to done this:
Syntax:
ALTER TABLE table_name
CHANGE COLUMN old_name new_name
column_definition
[ FIRST | AFTER column_name ]
Where,
old_name is refer to the existing column
new_name is the name to change with column definition.
[ FIRST | AFTER column_name ] is the position of column in between FIRST AND SECOND
The following is the student table in the database:
| Id |
name |
Book_Id |
| 1 |
Aman |
234 |
| 2 |
Ram |
213 |
| 3 |
Ramesh |
89 |
| 4 |
John |
567 |
Now we change the name of the column named as 'name' to the 'Stud_name'
Query:
ALTER TABLE library CHANGE COLUMN name stud_name;
Now we display the table again:
SHOW library;
| Id |
Stud_name |
Book_Id |
| 1 |
Aman |
234 |
| 2 |
Ram |
213 |
| 3 |
Ramesh |
89 |
| 4 |
John |
567 |