SQL RENAME TABLE

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 NameCar ColorCar Cost
Hyundai CretaWhite10,85,000
Hyundai VenueWhite9,50,000
Hyundai i20Red9,00,000
Kia SonetWhite10,00,000
Kia SeltosBlack8,00,000
Swift DezireRed7,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_IdEmp_NameEmp_SalaryEmp_City
201Abhay25000Goa
202Ankit45000Delhi
203Bheem30000Goa
204Ram29000Goa
205Sumit40000Delhi

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_NameBike_ColorBike_Cost
KTM DUKEBlack185,000
Royal EnfieldBlackNULL
PulsarRed90,0000
ApacheWhiteNULL
LivoBlack80,000
KTM RCRed195,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_IDStu_NameStu_Marks
1001Abhay85
1002Ankit75
1003Bheem60
1004Ram79
1005Sumit80

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_IdEmp_NameEmp_SalaryEmp_City
201Abhay25000Goa
202Ankit45000Delhi
203Bheem30000Goa
204Ram29000Goa
205Sumit40000Delhi

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”.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *