Change the name of one or more tables atomically. This command moves tables within or between databases while preserving their data and structure.
This statement renames one or more tables or views, but not the privileges associated with them. For InnoDB tables, it also triggers a reload of InnoDB statistics.
The rename operation is done atomically, which means that no other session can access any of the tables while the rename is running. For example, if you have an existing table old_table, you can create another table new_table that has the same structure but is empty, and then replace the existing table with the empty one as follows (assuming that backup_table does not already exist):
tbl_name can optionally be specified as db_name.tbl_name. See . This allows to use RENAME to move a table from a database to another (as long as they are on the same filesystem):
Note that moving a table to another database is not possible if it has .
Trying to do so produces the following error:
Views cannot be moved to another database.
Trying to do so produces the following error:
Multiple tables can be renamed in a single statement. The presence or absence of the optional S (RENAME TABLE or RENAME TABLES) has no impact, whether a single or multiple tables are being renamed.
If a RENAME TABLE renames more than one table and one renaming fails, all renames executed by the same statement are rolled back.
Renames are always executed in the specified order. Knowing this, it is also possible to swap two tables' names:
Set the lock wait timeout. See .
Executing the RENAME TABLE statement requires the , and privileges for the table or the database.
RENAME TABLE is atomic for most storage engines, including InnoDB, MyRocks, MyISAM and Aria ().
This means that if there is a crash (server down or power outage) during RENAME TABLE, all tables revert to their original names and any changes to trigger files are reverted.
See for more information.
RENAME TABLE is not atomic.
There is a small chance that, during a server crash happening in the middle of RENAME TABLE, some tables could have been renamed (in the worst case partly) while others would not be renamed.
RENAME TABLE[S] [IF EXISTS] tbl_name
[WAIT n | NOWAIT]
TO new_tbl_name
[, tbl_name2 TO new_tbl_name2] ...CREATE TABLE new_table (...);
RENAME TABLE old_table TO backup_table, new_table TO old_table;RENAME TABLE db1.t TO db2.t;ERROR 1435 (HY000): Trigger in wrong schemaERROR 1450 (HY000): Changing schema from 'old_db' to 'new_db' is not allowed.RENAME TABLE t1 TO tmp_table,
t2 TO t1,
tmp_table TO t2;This page is licensed: GPLv2, originally from fill_help_tables.sql