Course
Rename Tables
MySQL Tutorial
This SQL tutorial is structured for beginners to guide them from the foundational concepts to advanced data manipulation and querying techniques in SQL. By the end of this tutorial, you will have developed a robust understanding of SQL, equipping you with the knowledge to efficiently manage and analyze data across various database systems. This guide sets the stage for your growth into a skilled data professional, ready to tackle complex data challenges and contribute to the field of data analysis and database management.
Rename Tables
There can be a situation where both users and database administrators might want to change the name of a table in a relational database to make the table's name more suitable for a specific situation.
MySQL provides two different ways to rename an MySQL table. We can use either the RENAME TABLE or ALTER TABLE statement. In this tutorial, we will understand them with suitable examples.
MySQL RENAME TABLE Statement
The MySQL RENAME TABLE statement is used to rename an existing table in a database with another name.
Syntax
Following is the basic syntax of the MySQL RENAME TABLE statement
RENAME TABLE table_name TO new_name;
Where, table_name is the name of an existing table and new_name is the new name which you want to assign.
Example
Let us start by creating a table with name CUSTOMERS in MySQL database using CREATE statement as shown below
CREATE TABLE CUSTOMERS ( ID INT, NAME VARCHAR(20), AGE INT);
Here, we are renaming the above-created CUSTOMERS table to BUYERS using the following query
RENAME TABLE CUSTOMERS to BUYERS;
Output
The table has been renamed without any errors.
Query OK, 0 rows affected (0.01 sec)
Verification
Execute the following query to retrieve the description of the CUSTOMERS table
DESC CUSTOMERS;
It display an error because, we have changed the CUSTOMERS table name to BUYERS and there is no CUSTOMERS table in our database.
ERROR 1146 (42S02): Table 'tutorials.customers' doesn't exist
Renaming Multiple Tables
Using the MySQL RENAME TABLE statement, we can also rename multiple tables in a single query.
Syntax
Following is the syntax for renaming multiple tables using MySQL RENAME TABLE statement
RENAME TABLE old_table1 TO new_table1, old_table2 TO new_table2, old_table3 TO new_table3;
Example
In the following example, we are creating three different tables named Cust1, Cust2, and Cust3
CREATE TABLE Cust1(ID INT);CREATE TABLE Cust2(ID INT);CREATE TABLE Cust3(ID INT);
Here, we are verifying whether the above tables are created or not using the following query
SHOW TABLES;
As we can see in the output below, the above tables have been successfully created.
Now, let us rename all the above-created tables using the following query
RENAME TABLE Cust1 TO Buyer1, Cust2 TO Buyer2, Cust3 TO Buyer3;
Output
All three tables has been renamed without any errors.
Query OK, 0 rows affected (0.03 sec)
Verification
Let us verify the list of the tables again to find whether the table names have been changed or not
SHOW TABLES;
As we can see the output below, all three tables have been successfully renamed.
Renaming a Table using ALTER TABLE statement
In MySQL, we can also use the RENAME with ALTER TABLE statement to modify the name of an existing table.
Syntax
Following is the syntax to rename a table with ALTER TABLE statement
ALTER TABLE existing_table_name RENAME TO new_table_name
Example
In the following query, we are creating a table named PLAYERS.
CREATE TABLE PLAYERS ( ID INT, NAME VARCHAR(20), AGE INT);
Now, let us rename the above-created table with a new name TEAMS using the following query
ALTER TABLE PLAYERS RENAME TO TEAMS;
Output
The table has been renamed without any errors.
Query OK, 0 rows affected (0.02 sec)
Verification
Execute the following query to retrieve the description of the PLAYERS table
DESC PLAYERS;
It will display an error because, we have renamed the PLAYERS table to TEAMS and there is no PLAYERS table in our database.
ERROR 1146 (42S02): Table 'tutorials.players' doesn't exist
Renaming Table Using a Client Program
In addition to renaming a table in MySQL Database using MySQL query, we can also perform the RENAME TABLE operation on a table using a client program.
Syntax
Following are the syntaxes to rename table in MySQL database in various programming languages
PHPNodeJSJavaPython
To rename a table into MySQL database through PHP program, we need to execute RENAME TABLE statement using the mysqli function query() as
$sql = "RENAME TABLE old_table_name TO new_table_name";$mysqli->query($sql);
Example
Following are the programs
PHPNodeJSJavaPython
$dbhost = 'localhost';$dbuser = 'root';$dbpass = 'password';$dbname = 'TUTORIALS';$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($mysqli->connect_errno) { printf("Connect failed: %s", $mysqli->connect_error); exit();}// printf('Connected successfully.');
$sql = "RENAME TABLE tutorials_table TO tutorials_tbl ";if ($mysqli->query($sql)) { printf("table renamed successfully.");}if ($mysqli->errno) { printf("table could not rename: %s", $mysqli->error);}$mysqli->close();
Output
The output obtained is as follows
table renamed successfully.