Friday 13 July 2018

How To Rename Table Using MySQL RENAME TABLE Statement

How To Rename Table Using MySQL RENAME TABLE Statement?

Summary: in this tutorial, you will learn how to rename tables using MySQL RENAME TABLE statement and ALTER TABLE statement.

Introduction to MySQL RENAME TABLE statement

Because business requirements change, we need to rename the current table to a new one to better reflect the new situation. MySQL provides us with a very useful statement that changes the name of one or more tables.
To change one or more tables, we use the RENAME TABLE statement as follows:
The old table ( old_table_name) must exist, and the new table ( new_table_name) must not. If the new table new_table_name does exist, the statement will fail.
In addition to the tables, we can use the RENAME TABLE statement to rename views.
Before we execute the RENAME TABLE statement, we must ensure that there is no active transactions or locked tables.
Note that you cannot use the RENAME TABLE statement to rename a temporary table, but you can use the ALTER TABLE statement to rename a temporary table.
In terms of security, any existing privileges that we granted to the old table must be manually migrated to the new table.
Before renaming a table, you should evaluate the impact thoroughly. For example, you should investigate which applications are using the table. If the name of the table changes, so the application code that refers to the table name needs to be changed as well. In addition, you must manually adjust other database objects such as views, stored procedures, triggers, foreign key constraints, etc., that reference to the table. We will discuss this in more detail in the following examples.

MySQL RENAME TABLE examples

First, we create a new database named hr that consists of two tables: employees and departments for the demonstration.

Second, we insert sample data into both employees and departments tables:
Third, we review our data in the departments and employees tables:
MySQL RENAME TABLE departments Table

Renaming a table referenced by a view

If the table that you are going to rename is referenced by a view, the view will become invalid if you rename the table, and you have to adjust the view manually.
For example, we create a view named v_employee_info based on the employees and departmentstables as follows:
The views use the inner join clause to join departments and employees tables.
The following SELECT statement returns all data from the v_employee_info view.

Now we rename the employees to people table and query data from the v_employee_info view again.
MySQL returns the following error message:
We can use the CHECK TABLE statement to check the status of the v_employee_info view as follows:

We need to manually change the v_employee_info view so that it refers to the people table instead of the employees table.

Renaming a table that referenced by a stored procedure

In case the table that you are going to rename is referenced by a stored procedure, you have to manually adjust it like you did with the view.
First, rename the people table back to the employees table.
Then, create a new stored procedure named get_employee that refers to the employees table.
Next, we execute the get_employee table to get the data of the employee with id 1 as follows:
MySQL RENAME TABLE with Stored Procedure
After that, we rename the employees to the people table again.
Finally, we call the get_employee stored procedure to get the information of employee with id 2:
MySQL returns the following error message:
To fix this, we must manually change the employees table in the stored procedure to people table.

Renaming a table that has foreign keys referenced to

The departments table links to the employees table using the department_id column. The department_id column in the employees table is the foreign key that references to the departmentstable.
If we rename the departments table, all the foreign keys that point to the departments table will not be automatically updated. In such cases, we must drop and recreate the foreign keys manually.
We delete a department with id 1, because of the foreign key constraint, all rows in the people table should be also deleted. However, we renamed the departments table to the depts table without updating the foreign key manually, MySQL returns an error as illustrated below:

Renaming multiple tables

We can also use the RENAME TABLE statement to rename multiple tables at a time. See the following statement:
The following statement renames the people and depts tables to employees and departmentstables:
Note the RENAME TABLE statement is not atomic. It means that if any errors occurred, MySQL does a rollback all renamed tables to their old names.

Renaming tables using ALTER TABLE statement

We can rename a table using the ALTER TABLE statement as follows:
The ALTER TABLE statement can rename a temporary table while the RENAME TABLE statement cannot.

Renaming temporary table example

First, we create a temporary table that contains all unique last names which come from the last_namecolumn of the employees table:
Second, we use the RENAME TABLE to rename the lastnames table:
MySQL returns the following error message:
Third, we use the ALTER TABLE statement to rename the lastnames table.
Fourth, we query data from the unique_lastnames temporary table:
MySQL ALTER TABLE renames temporary table
In this tutorial, we have shown you how to rename tables using MySQL RENAME TABLE and ALTER TABLE statements.

0 comments:

Post a Comment