Rechercher dans le manuel MySQL

13.1.36 RENAME TABLE Syntax

  1.     tbl_name TO new_tbl_name
  2.     [, tbl_name2 TO new_tbl_name2] ...

RENAME TABLE renames one or more tables. You must have ALTER and DROP privileges for the original table, and CREATE and INSERT privileges for the new table.

For example, to rename a table named old_table to new_table, use this statement:

  1. RENAME TABLE old_table TO new_table;

That statement is equivalent to the following ALTER TABLE statement:

  1. ALTER TABLE old_table RENAME new_table;

RENAME TABLE, unlike ALTER TABLE, can rename multiple tables within a single statement:

  1. RENAME TABLE old_table1 TO new_table1,
  2.              old_table2 TO new_table2,
  3.              old_table3 TO new_table3;

Renaming operations are performed left to right. Thus, to swap two table names, do this (assuming that a table with the intermediary name tmp_table does not already exist):

  1. RENAME TABLE old_table TO tmp_table,
  2.              new_table TO old_table,
  3.              tmp_table TO new_table;

Metadata locks on tables are acquired in name order, which in some cases can make a difference in operation outcome when multiple transactions execute concurrently. See Section 8.11.4, “Metadata Locking”.

As of MySQL 8.0.13, you can rename tables locked with a LOCK TABLES statement, provided that they are locked with a WRITE lock or are the product of renaming WRITE-locked tables from earlier steps in a multiple-table rename operation. For example, this is permitted:

  1. LOCK TABLE old_table1 WRITE;
  2. RENAME TABLE old_table1 TO new_table1,
  3.              new_table1 TO new_table2;

This is not permitted:

  1. LOCK TABLE old_table1 READ;
  2. RENAME TABLE old_table1 TO new_table1,
  3.              new_table1 TO new_table2;

Prior to MySQL 8.0.13, to execute RENAME TABLE, there must be no tables locked with LOCK TABLES.

With the transaction table locking conditions satisfied, the rename operation is done atomically; no other session can access any of the tables while the rename is in progress.

If any errors occur during a RENAME TABLE, the statement fails and no changes are made.

You can use RENAME TABLE to move a table from one database to another:

  1. RENAME TABLE current_db.tbl_name TO other_db.tbl_name;

Using this method to move all tables from one database to a different one in effect renames the database (an operation for which MySQL has no single statement), except that the original database continues to exist, albeit with no tables.

Like RENAME TABLE, ALTER TABLE ... RENAME can also be used to move a table to a different database. Regardless of the statement used, if the rename operation would move the table to a database located on a different file system, the success of the outcome is platform specific and depends on the underlying operating system calls used to move table files.

If a table has triggers, attempts to rename the table into a different database fail with a Trigger in wrong schema (ER_TRG_IN_WRONG_SCHEMA) error.

An unencrypted table can be moved to an encryption-enabled database and vice versa. However, if the table_encryption_privilege_check variable is enabled, the TABLE_ENCRYPTION_ADMIN privilege is required if the table encryption setting differs from the default database encryption.

To rename TEMPORARY tables, RENAME TABLE does not work. Use ALTER TABLE instead.

RENAME TABLE works for views, except that views cannot be renamed into a different database.

Any privileges granted specifically for a renamed table or view are not migrated to the new name. They must be changed manually.

RENAME TABLE tbl_name TO new_tbl_name changes internally generated foreign key constraint names and user-defined foreign key constraint names that begin with the string tbl_name_ibfk_ to reflect the new table name. InnoDB interprets foreign key constraint names that begin with the string tbl_name_ibfk_ as internally generated names.

Foreign key constraint names that point to the renamed table are automatically updated unless there is a conflict, in which case the statement fails with an error. A conflict occurs if the renamed constraint name already exists. In such cases, you must drop and re-create the foreign keys for them to function properly.

RENAME TABLE tbl_name TO new_tbl_name changes internally generated and user-defined CHECK constraint names that begin with the string tbl_name_chk_ to reflect the new table name. MySQL interprets CHECK constraint names that begin with the string tbl_name_chk_ as internally generated names. Example:

  1. mysql> SHOW CREATE TABLE t1\G
  2. *************************** 1. row ***************************
  3.        Table: t1
  4.   `i1` int(11) DEFAULT NULL,
  5.   `i2` int(11) DEFAULT NULL,
  6.   CONSTRAINT `t1_chk_1` CHECK ((`i1` > 0)),
  7.   CONSTRAINT `t1_chk_2` CHECK ((`i2` < 0))
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
  9. 1 row in set (0.02 sec)
  10.  
  11. mysql> RENAME TABLE t1 TO t3;
  12. Query OK, 0 rows affected (0.03 sec)
  13.  
  14. mysql> SHOW CREATE TABLE t3\G
  15. *************************** 1. row ***************************
  16.        Table: t3
  17.   `i1` int(11) DEFAULT NULL,
  18.   `i2` int(11) DEFAULT NULL,
  19.   CONSTRAINT `t3_chk_1` CHECK ((`i1` > 0)),
  20.   CONSTRAINT `t3_chk_2` CHECK ((`i2` < 0))
  21. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
  22. 1 row in set (0.01 sec)

Find a PHP function

Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-rename-table.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:en Manuel MySQL : https://dev.mysql.com/

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut