13.1.36 RENAME TABLE Statement语法

RENAME TABLE
tbl_name TO new_tbl_name
    [, tbl_name2 TO new_tbl_name2] ...

RENAME TABLE renames one or more tables. RENAME TABLE重命名一个或多个表。You must have ALTER and DROP privileges for the original table, and CREATE and INSERT privileges for the new table.您必须具有原始表的ALTERDROP权限,以及新表的CREATEINSERT权限。

For example, to rename a table named old_table to new_table, use this statement:例如,要将名为old_table的表重命名为new_table,请使用以下语句:

RENAME TABLE old_table TO new_table;

That statement is equivalent to the following ALTER TABLE statement:该语句相当于以下ALTER TABLE语句:

ALTER TABLE old_table RENAME new_table;

RENAME TABLE, unlike ALTER TABLE, can rename multiple tables within a single statement:ALTER TABLE不同,RENAME TABLE可以在一条语句中重命名多个表:

RENAME TABLE old_table1 TO new_table1,
             old_table2 TO new_table2,
             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):因此,要交换两个表名,请执行以下操作(假设中间名为tmp_table的表不存在):

RENAME TABLE old_table TO tmp_table,
             new_table TO old_table,
             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”.请参阅第8.11.4节,“元数据锁定”

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. 从MySQL 8.0.13开始,您可以重命名使用LOCK TABLES语句锁定的表,前提是这些表是使用WRITE锁锁定的,或者是在多表重命名操作的前面步骤中重命名写锁定表的结果。For example, this is permitted:例如,这是允许的:

LOCK TABLE old_table1 WRITE;
RENAME TABLE old_table1 TO new_table1,
             new_table1 TO new_table2;

This is not permitted:这是不允许的:

LOCK TABLE old_table1 READ;
RENAME TABLE old_table1 TO new_table1,
             new_table1 TO new_table2;

Prior to MySQL 8.0.13, to execute RENAME TABLE, there must be no tables locked with LOCK TABLES.在MySQL 8.0.13之前,要执行RENAME TABLE,必须使用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.如果RENAME TABLE期间发生任何错误,则该语句将失败,并且不会进行任何更改。

You can use RENAME TABLE to move a table from one database to another:可以使用RENAME TABLE将表从一个数据库移动到另一个数据库:

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.使用此方法将所有表从一个数据库移动到另一个数据库实际上会重命名该数据库(MySQL没有单独语句的操作),除了原始数据库仍然存在,尽管没有表。

Like RENAME TABLE, ALTER TABLE ... RENAME can also be used to move a table to a different database. RENAME TABLEALTER TABLE ... RENAME还可用于将表移动到其他数据库。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.如果一个表有触发器,尝试将该表重命名到其他数据库时会失败,并出现错误架构中的触发器Trigger in wrong schemaER_TRG_IN_WRONG_SCHEMA)错误。

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.但是,如果启用了table_encryption_privilege_check变量,则如果TABLE_ENCRYPTION_ADMIN权限设置与默认数据库加密不同,则需要TABLE_ENCRYPTION_ADMIN权限。

To rename TEMPORARY tables, RENAME TABLE does not work. 若用RENAME TABLE重命名TEMPORARY表,则无效。Use ALTER TABLE instead.请改用ALTER TABLE

RENAME TABLE works for views, except that views cannot be renamed into a different database.RENAME TABLE适用于视图,但不能将视图重命名为其他数据库。

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. RENAME TABLE tbl_name TO new_tbl_name会更改内部生成的外键约束名称和用户定义的外键约束名称(以字符串“tbl_name_ibfk_”开头),以反映新表名称。InnoDB interprets foreign key constraint names that begin with the string tbl_name_ibfk_ as internally generated names.InnoDB将以字符串“tbl_name_ibfk_”开头的外键约束名称解释为内部生成的名称。

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. RENAME TABLE tbl_name TO new_tbl_name会更改内部生成的CHECK约束名称和用户定义的CHECK约束名称(以字符串“tbl_name_chk_”开头),以反映新表名称。MySQL interprets CHECK constraint names that begin with the string tbl_name_chk_ as internally generated names. MySQL将以字符串“tbl_name_chk_”开头的CHECK约束名称解释为内部生成的名称。Example:例如:

mysql> SHOW CREATE TABLE t1\G
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `i1` int(11) DEFAULT NULL,
  `i2` int(11) DEFAULT NULL,
  CONSTRAINT `t1_chk_1` CHECK ((`i1` > 0)),
  CONSTRAINT `t1_chk_2` CHECK ((`i2` < 0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.02 sec)

mysql> RENAME TABLE t1 TO t3;
Query OK, 0 rows affected (0.03 sec)

mysql> SHOW CREATE TABLE t3\G
*************************** 1. row ***************************
       Table: t3
Create Table: CREATE TABLE `t3` (
  `i1` int(11) DEFAULT NULL,
  `i2` int(11) DEFAULT NULL,
  CONSTRAINT `t3_chk_1` CHECK ((`i1` > 0)),
  CONSTRAINT `t3_chk_2` CHECK ((`i2` < 0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.01 sec)