In some situations, database administrators and users want to change the name of the table in the SQL database because they want to give a more relevant name to the table.
Any database user can easily change the name by using the RENAME TABLE and ALTER TABLE statement in Structured Query Language.
The RENAME TABLE and ALTER TABLE syntax help in changing the name of the table.
Syntax of RENAME statement in SQL
RENAME old_table _name To new_table_name ;
Examples of RENAME statement in SQL
Here, we have taken the following two different SQL examples, which will help you how to change the name of the SQL table in the database using RENAME statement:
Example 1: Let’s take an example of a table named Cars:
Car Name | Car Color | Car Cost |
---|---|---|
Hyundai Creta | White | 10,85,000 |
Hyundai Venue | White | 9,50,000 |
Hyundai i20 | Red | 9,00,000 |
Kia Sonet | White | 10,00,000 |
Kia Seltos | Black | 8,00,000 |
Swift Dezire | Red | 7,95,000 |
Table: Cars
- Suppose, you want to change the above table name into “Car_2021_Details”. For this, you have to type the following RENAME statement in SQL:
RENAME Cars To Car_2021_Details ;
- After this statement, the table “Cars” will be changed into table name “Car_2021_Details”.
Example 2: Let’s take an example of a table named Employee:
Emp_Id | Emp_Name | Emp_Salary | Emp_City |
---|---|---|---|
201 | Abhay | 25000 | Goa |
202 | Ankit | 45000 | Delhi |
203 | Bheem | 30000 | Goa |
204 | Ram | 29000 | Goa |
205 | Sumit | 40000 | Delhi |
Table: Employee
- Suppose, you want to change the name of the above table into the “Coding_Employees”. For this, you have to type the following RENAME statement in SQL:
RENAME Employee To Coding_Employees ;
- After this statement, the table “Employee” will be changed into the table name “Coding_Employees”.
Syntax of ALTER TABLE statement in SQL
ALTER TABLE old_table_name RENAME TO new_table_name;
In the Syntax, we have to specify the RENAME TO keyword after the old name of the table.
Examples of ALTER TABLE statement in SQL
Here, we have taken the following three different SQL examples, which will help you how to change the name of the table in the SQL database using ALTER TABLE statement:
Example 1: Let’s take an example of a table named Bikes:
Bike_Name | Bike_Color | Bike_Cost |
---|---|---|
KTM DUKE | Black | 185,000 |
Royal Enfield | Black | NULL |
Pulsar | Red | 90,0000 |
Apache | White | NULL |
Livo | Black | 80,000 |
KTM RC | Red | 195,000 |
Table : Bikes
- Suppose, you want to change the name of the above table into “Bikes_Details” using ALTER TABLE statement. For this, you have to type the following query in SQL:
ALTER TABLE Bikes RENAME TO Bikes_Details ;
After this statement, the table “Bikes” will be changed into the table name “Bikes_Details”.
Example 2: Let’s take an example of a table named Student:
Stu_ID | Stu_Name | Stu_Marks |
---|---|---|
1001 | Abhay | 85 |
1002 | Ankit | 75 |
1003 | Bheem | 60 |
1004 | Ram | 79 |
1005 | Sumit | 80 |
Table : Student
- Suppose, you want to change the name of the above table into “MCA_Student_Details” using ALTER TABLE statement. For this, you have to type the following query in SQL:
ALTER TABLE Student RENAME TO MCA_Student_Details ;
After this statement, the table “Student” will be changed into table name “MCA_Student_Details”.
Example 3: Let’s take an example of a table named Employee:
Emp_Id | Emp_Name | Emp_Salary | Emp_City |
---|---|---|---|
201 | Abhay | 25000 | Goa |
202 | Ankit | 45000 | Delhi |
203 | Bheem | 30000 | Goa |
204 | Ram | 29000 | Goa |
205 | Sumit | 40000 | Delhi |
Table: Employee
- Suppose, you want to change the name of the above table into the “Coding_Employees” using an ALTER TABLE statement. For this, you have to type the following query in SQL:
ALTER TABLE Employee RENAME To Coding_Employees ;
After this statement, the table “Employee” will be changed into the table name “Coding_Employees”.
Leave a Reply