In this tutorial, you will learn how to use MySQL RENAME TABLE
statement and the ALTER TABLE
statement to rename tables.
MySQL RENAME TABLE statement Introduction
As the business needs change, we need to rename the current table to a new table, to better reflect or represent the new situation. MySQL provides a very useful statement to change the name of one or more tables.
To change one or more tables, we use the RENAME TABLE
statement as follows:
RENAME TABLE old_table_name TO new_table_name;
Old table ( old_table_name
) must exist, a new table ( new_table_name
) does not. If the new table new_table_name
exists, the statement will fail.
In addition to the table, we can also use the RENAME TABLE
statement to rename a view .
In the execution RENAME TABLE
before the statement, you must ensure that no activity or transaction locking table .
Please note that you can not use
RENAME TABLE
the statement to rename the temporary table , but you can use the ALTER TABLE statement to rename the temporary table.
In terms of security, we grant the old table any permissions must be manually migrated to the new table.
Prior to rename the table, should thoroughly assess the impact. For example, you should investigate what applications are using the table. If you change the name of the table, then the table name references the application code also needs to be changed. You must manually adjust the reference objects other database tables, such views , stored procedures , triggers , foreign key constraints like. We will discuss in more detail in the following examples.
MySQL RENAME TABLE example
First , we create a file named hrdb
new database, which consists of two tables: employees
and departments
.
Create a database -
CREATE DATABASE IF NOT EXISTS hrdb;
Create a table -
USE hrdb;
CREATE TABLE departments ( department_id INT AUTO_INCREMENT PRIMARY KEY, dept_name VARCHAR(100) ); CREATE TABLE employees ( id int AUTO_INCREMENT primary key, first_name varchar(50) not null, last_name varchar(50) not null, department_id int not null, FOREIGN KEY (department_id) REFERENCES departments (department_id) );
Next , the sample data into employees
and departments
tables:
-- 插入数据到 departments 表中
INSERT INTO departments(dept_name) VALUES('Sales'),('Markting'),('Finance'),('Accounting'),('Warehouses'),('Production'); -- 插入数据到 employees 表中 INSERT INTO employees(first_name,last_name,department_id) VALUES('John','Doe',1), ('Bush','Lily',2), ('David','Dave',3), ('Mary','Jane',4), ('Jonatha','Josh',5), ('Mateo','More',1);
Third , query employees
and departments
data in the table:
mysql> SELECT
department_id, dept_name
FROM
departments; +---------------+------------+ | department_id | dept_name | +---------------+------------+ | 1 | Sales | | 2 | Markting | | 3 | Finance | | 4 | Accounting | | 5 | Warehouses | | 6 | Production | +---------------+------------+ 6 rows in set mysql> SELECT id, first_name, last_name, department_id FROM employees; +----+------------+-----------+---------------+ | id | first_name | last_name | department_id | +----+------------+-----------+---------------+ | 1 | John | Doe | 1 | | 2 | Bush | Lily | 2 | | 3 | David | Dave | 3 | | 4 | Mary | Jane | 4 | | 5 | Jonatha | Josh | 5 | | 6 | Mateo | More | 1 | +----+------------+-----------+---------------+ 6 rows in set
Rename the referenced table view
If the rename is a view of a table reference, after the rename table, view is invalid and must be manually adjust the view.
For example, we based employees
and departments
tables create a named v_employee_info
view, as follows:
CREATE VIEW v_employee_info as
SELECT
id, first_name, last_name, dept_name from employees inner join departments USING (department_id);
View of the use of the connector clause connected employees
and departments
tables.
The following SELECT statement returns v_employee_info
all of the data view.
mysql> SELECT
*
FROM
v_employee_info; +----+------------+-----------+------------+ | id | first_name | last_name | dept_name | +----+------------+-----------+------------+ | 1 | John | Doe | Sales | | 2 | Bush | Lily | Markting | | 3 | David | Dave | Finance | | 4 | Mary | Jane | Accounting | | 5 | Jonatha | Josh | Warehouses | | 6 | Mateo | More | Sales | +----+------------+-----------+------------+ 6 rows in set
Now, the v_employee_info
view employees
rename the table people
, and query the data view.
RENAME TABLE employees TO people;
-- 查询数据 SELECT * FROM v_employee_info;
MySQL returns the following error message:
1356 - View 'hrdb.v_employee_info' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
We can use the CHECK TABLE
statement to check v_employee_info
the view of the state as follows:
CHECK TABLE v_employee_info;
mysql> CHECK TABLE v_employee_info; +----------------------+-------+----------+----------------------------------------------------------------------------------------------------------------------------------------+ | Table | Op | Msg_type | Msg_text | +----------------------+-------+----------+----------------------------------------------------------------------------------------------------------------------------------------+ | hrdb.v_employee_info | check | Error | Table 'hrdb.employees' doesn't exist | | hrdb.v_employee_info | check | Error | View 'hrdb.v_employee_info' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them | | hrdb.v_employee_info | check | error | Corrupt | +----------------------+-------+----------+----------------------------------------------------------------------------------------------------------------------------------------+ 3 rows in set
Need to manually change v_employee_info
the view so that it references people
the table instead of employees
the table.
Rename table referenced by the stored procedure
To rename the stored procedure referenced tables, it must be manually adjusted to view the same image.
First, people
rename the table to employees
table.
RENAME TABLE people TO employees;
Then, create a file named get_employee
new stored procedure, the process reference employees
table.
DELIMITER $$
CREATE PROCEDURE get_employee(IN p_id INT) BEGIN SELECT first_name ,last_name ,dept_name FROM employees INNER JOIN departments using (department_id) WHERE id = p_id; END $$ DELIMITER;
Next, a get_employee
stored procedure from employees
acquiring table id
of 1
employee data, as follows:
CALL get_employee(1);
Implementation of the above query, the following results -
mysql> CALL get_employee(1); +------------+-----------+-----------+ | first_name | last_name | dept_name | +------------+-----------+-----------+ | John | Doe | Sales | +------------+-----------+-----------+ 1 row in set Query OK, 0 rows affected
After that, we will once again employees
table rename people
table.
RENAME TABLE employees TO people;
Finally, call the get_employee
stored procedure to obtain id
as 2
employee information:
CALL get_employee(2);
MySQL returns the following error message:
1146 - Table 'hrdb.employees' doesn't exist
To solve this problem, we must manually stored procedure employees
to change the table to people
table.
Rename table foreign key references
departments
Use table department_id
column linked to a employees
table. employees
Table department_id
column reference departments
table department_id
column as the foreign key .
If you rename a departments
table, pointing to departments
the table all foreign keys will not be automatically updated. In this case, we have to manually delete and re-create the foreign key.
RENAME TABLE departments TO depts;
We removed ID
for the 1
sector, because the foreign key constraint, people
all rows in the table should also be deleted. However, we will department
rename the table to depts
table, without manually update the foreign keys, MySQL returns an error, as follows:
DELETE FROM depts
WHERE
department_id = 1;
Implementation of the above statements, the following the following error message -
1451 - Cannot delete or update a parent row: a foreign key constraint fails (`hrdb`.`people`, CONSTRAINT `people_ibfk_1` FOREIGN KEY (`department_id`) REFERENCES `depts` (`department_id`))
Rename multiple tables
You can also use RENAME TABLE
the statement to rename multiple tables once. See the following statement:
RENAME TABLE old_table_name_1 TO new_table_name_2,
old_table_name_2 TO new_table_name_2,...
The following statement people
and depts
rename employees
and departments
table:
RENAME TABLE depts TO departments,
people TO employees;
Note that
RENAME TABLE
the statement is not atomic. So if an error occurs at any time, MySQL will rename all the tables are rolled back to the old name.
ALTER TABLE statement to rename tables
We can use the ALTER TABLE
statement to rename a table, as follows:
ALTER TABLE old_table_name
RENAME TO new_table_name;
RENAME TABLE
Statement can not be used to rename the temporary table, then you can use the ALTER TABLE
statement to rename a temporary table.
Rename the temporary shows an example
First, we create a temporary table , which contains from employees
table of last_name
all unique last name columns:
CREATE TEMPORARY TABLE lastnames
SELECT DISTINCT last_name from employees;
The second step , using the RENAME TABLE
rename table surname:
RENAME TABLE lastnames TO unique_lastnames;
MySQL returns the following error message:
Error Code: 1017. Can't find file: '.\hrdb\lastnames.frm' (errno: 2 - No such file or directory)
Third , use the ALTER TABLE
statement to rename a table surname.
ALTER TABLE lastnames
RENAME TO unique_lastnames;
Fourth , from the unique_lastnames
query data temporary table:
SELECT
last_name
FROM
unique_lastnames;
+-----------+ | last_name | +-----------+ | Doe | | Lily | | Dave | | Jane | | Josh | | More | +-----------+ 6 rows in set
In this tutorial, we show you how to use MySQL RENAME TABLE
and ALTER TABLE
statement to rename tables.