SQL | ALTER (RENAME)

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(Oracle):

ALTER TABLE table_name
RENAME COLUMN old_name TO new_name;

Syntax(MySQL,MariaDB):

ALTER TABLE table_name
CHANGE COLUMN old_name TO new_name;

Sample Table:

Student

ROLL_NO NAME AGE
1 Ram 20
2 Abhi 21
3 Rahul 22
4 Tanu 19

QUERY:

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

OUTPUT:

ROLL_NO FIRST_NAME AGE
1 Ram 20
2 Abhi 21
3 Rahul 22
4 Tanu 19
  • Change the name of the table Student to Student_Details
ALTER TABLE Student RENAME TO Student_Details;

OUTPUT:

Student_Details

ROLL_NO FIRST_NAME AGE
1 Ram 20
2 Abhi 21
3 Rahul 22
4 Tanu 19

This article is contributed by Shubham Chaudhary. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : vishnureddy7



Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.