In certain situations, the DB administrators and users may want to change/alter the table name in the SQL database. They may want to give the table a more relevant or easy to use name. In such situations, a database user or administrator can change the name easily by using the ALTER TABLE and RENAME TABLE statements in SQL (Structured Query Language).
In short, the ALTER TABLE and RENAME TABLE syntax help SQL users in changing the table name.
In this article, we will dive deeper into the RENAME in SQL according to the GATE Syllabus for (Computer Science Engineering) CSE. Keep reading ahead to learn more.
Table of Contents
What is RENAME in SQL?
Sometimes users may want to rename their database table and give it a more catchy or relevant name. For such a purpose, the users can deploy the ALTER TABLE or the RENAME TABLE in order to rename the table.
Syntax
The syntax of the RENAME in SQL would go like this (the syntax may vary considerably in various databases):
Syntax for MySQL, Oracle, MariaDB:
ALTER TABLE name_of_table
RENAME TO new_name_of_table;
The columns can also be given a new name when using the ALTER TABLE.
Syntax of MySQL, Oracle:
ALTER TABLE name_of_table
RENAME COLUMN old_name_of_table TO new_name_of_table;
Syntax of MariaDB:
ALTER TABLE name_of_table
CHANGE COLUMN old_name_of_table TO new_name_of_table;
Sample Table
Employee
ID_NO | FIRST_NAME | SN |
1 | Ramesh | 20 |
2 | Abhilash | 21 |
3 | Rahulya | 22 |
4 | Tanuja | 19 |
QUERY:
We will change the name of the column FIRST_NAME to NAME in the table “Employee”.
ALTER TABLE Employee RENAME COLUMN FIRST_NAME TO NAME;
OUTPUT:
ID_NO | NAME | SN |
1 | Ramesh | 20 |
2 | Abhilash | 21 |
3 | Rahulya | 22 |
4 | Tanuja | 19 |
We will also change the name of the table Employee to Employee_Details
ALTER TABLE Employee RENAME TO Employee_Details;
OUTPUT:
Employee_Details
ID_NO | NAME | SN |
1 | Ramesh | 20 |
2 | Abhilash | 21 |
3 | Rahulya | 22 |
4 | Tanuja | 19 |
Keep learning and stay tuned to get the latest updates on the GATE Exam along with Eligibility Criteria, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.
Also Explore,
Comments