RENAME TABLE tbl_name [WAIT n | NOWAIT] TO new_tbl_name [, tbl_name2 TO new_tbl_name2] ...
This statement renames one or more tables or views, but not the privileges associated to them.
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):
CREATE TABLE new_table (...); RENAME TABLE old_table TO backup_table, new_table TO old_table;
tbl_name can optionally be specified as
tbl_name. See Identifier Qualifiers. This allows to use
RENAME to move a table from a database to another (as long as they are on the same filesystem):
RENAME TABLE db1.t TO db2.t;
Note that moving a table to another database is not possible if it has some triggers. Trying to do so produces the following error:
ERROR 1435 (HY000): Trigger in wrong schema
Also, views cannot be moved to another database:
ERROR 1450 (HY000): Changing schema from 'old_db' to 'new_db' is not allowed.
RENAME TABLE does not work with temporary tables.
ALTER TABLE can be used instead:
CREATE TEMPORARY TABLE t1 (c INT); ALTER TABLE t1 RENAME TO t2;
RENAME TABLE renames more than one table and one renaming fails, all renamings 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:
RENAME TABLE t1 TO tmp_table, t2 TO t1, tmp_table TO t2;