MySQL rename the table

In this tutorial, you will learn how to use MySQL RENAME TABLEstatement and the ALTER TABLEstatement 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 TABLEstatement as follows:

RENAME TABLE old_table_name TO new_table_name;
SQL

Old table ( old_table_name) must exist, a new table ( new_table_name) does not. If the new table new_table_nameexists, the statement will fail.

In addition to the table, we can also use the RENAME TABLEstatement to rename a view .

In the execution RENAME TABLEbefore the statement, you must ensure that no activity or transaction locking table .

Please note that you can not use RENAME TABLEthe 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 hrdbnew database, which consists of two tables: employeesand departments.

Create a database -

CREATE DATABASE IF NOT EXISTS hrdb; 
SQL

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) ); 
SQL

Next , the sample data into employeesand departmentstables:

-- 插入数据到 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); 
SQL

Third , query employeesand departmentsdata 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 
SQL

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 employeesand departmentstables create a named v_employee_infoview, as follows:

CREATE VIEW v_employee_info as
    SELECT 
        id, first_name, last_name, dept_name from employees inner join departments USING (department_id); 
SQL

View of the use of the connector clause connected employeesand departmentstables.

The following SELECT statement returns v_employee_infoall 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 
SQL

Now, the v_employee_infoview employeesrename the table people, and query the data view.

RENAME TABLE employees TO people;

-- 查询数据 SELECT * FROM v_employee_info; 
SQL

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 
SQL

We can use the CHECK TABLEstatement to check v_employee_infothe 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 
SQL

Need to manually change v_employee_infothe view so that it references peoplethe table instead of employeesthe 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, peoplerename the table to employeestable.

RENAME TABLE people TO employees;
SQL

Then, create a file named get_employeenew stored procedure, the process reference employeestable.

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; 
SQL

Next, a get_employeestored procedure from employeesacquiring table idof 1employee data, as follows:

CALL get_employee(1); 
SQL

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 
SQL

After that, we will once again employeestable rename peopletable.

RENAME TABLE employees TO people;
SQL

Finally, call the get_employeestored procedure to obtain idas 2employee information:

CALL get_employee(2); 
SQL

MySQL returns the following error message:

1146 - Table 'hrdb.employees' doesn't exist
SQL

To solve this problem, we must manually stored procedure employeesto change the table to peopletable.

Rename table foreign key references

departmentsUse table department_idcolumn linked to a employeestable. employeesTable department_idcolumn reference departmentstable department_idcolumn as the foreign key .

If you rename a departmentstable, pointing to departmentsthe 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;
SQL

We removed IDfor the 1sector, because the foreign key constraint, peopleall rows in the table should also be deleted. However, we will departmentrename the table to deptstable, without manually update the foreign keys, MySQL returns an error, as follows:

DELETE FROM depts 
WHERE
    department_id = 1; 
SQL

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`)) 
SQL

Rename multiple tables

You can also use RENAME TABLEthe 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,... 
SQL

The following statement peopleand deptsrename employeesand departmentstable:

RENAME TABLE depts TO departments,
             people TO employees; 
SQL

Note that RENAME TABLEthe 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 TABLEstatement to rename a table, as follows:

ALTER TABLE old_table_name
RENAME TO new_table_name; 
SQL

RENAME TABLEStatement can not be used to rename the temporary table, then you can use the ALTER TABLEstatement to rename a temporary table.

Rename the temporary shows an example

First, we create a temporary table , which contains from employeestable of last_nameall unique last name columns:

CREATE TEMPORARY TABLE lastnames
SELECT DISTINCT last_name from employees; 
SQL

The second step , using the RENAME TABLErename table surname:

RENAME TABLE lastnames TO unique_lastnames;
SQL

MySQL returns the following error message:

Error Code: 1017. Can't find file: '.\hrdb\lastnames.frm' (errno: 2 - No such file or directory) 
SQL

Third , use the ALTER TABLEstatement to rename a table surname.

ALTER TABLE lastnames
RENAME TO unique_lastnames; 
SQL

Fourth , from the unique_lastnamesquery data temporary table:

SELECT 
    last_name
FROM
    unique_lastnames;

+-----------+ | last_name | +-----------+ | Doe | | Lily | | Dave | | Jane | | Josh | | More | +-----------+ 6 rows in set 
SQL

In this tutorial, we show you how to use MySQL RENAME TABLEand ALTER TABLEstatement to rename tables.



Guess you like

Origin www.cnblogs.com/borter/p/12452696.html