What is RENAME in SQL?

 Sometimes we may want to rename our table to give it a more relevant name. For this purpose we can use ALTER TABLE to rename the name of table.

*Syntax may vary in different databases. 
 
Syntax(Oracle,MySQL,MariaDB):
 

ALTER TABLE table_name
RENAME TO new_table_name;

Columns can be also be given new name with the use of ALTER TABLE.

Syntax(MySQL, Oracle):

ALTER TABLE table_name
RENAME COLUMN old_name TO new_name;

Syntax(MariaDB):

ALTER TABLE table_name
CHANGE COLUMN old_name TO new_name;

Sample Table:
Student

ROLL_NONAMEAGE
1Ram20
2Abhi21
3Rahul22
4Tanu19

QUERY:

  • Change the name of column NAME to FIRST_NAME in table Student.
ALTER TABLE Student RENAME COLUMN NAME TO FIRST_NAME;

OUTPUT:
 

ROLL_NOFIRST_NAMEAGE
1Ram20
2Abhi21
3Rahul22
4Tanu19

 

  • Change the name of the table Student to Student_Details
ALTER TABLE Student RENAME TO Student_Details;

OUTPUT:
Student_Details
 

ROLL_NOFIRST_NAMEAGE
1Ram20
2Abhi21
3Rahul22
4Tanu19

Post a Comment

Previous Post Next Post

Contact Form