Purpose
You can use this statement to rename one or more tables.
To execute this statement, you must have the ALTER and DROP privileges on the original table and the CREATE and INSERT privileges on the new table.
Limitations and considerations
- When the rename operation is in progress, the system automatically locks the process and prevents other threads from reading or writing the renamed tables until the operation is done.
- If a target table has any active transactions, the rename operation will wait for them to roll back or commit, and will not proceed until then.
- If you use this statement to rename multiple tables, the rename operation proceeds from left to right.
- Before you execute the
RENAME TABLEstatement, make sure that no target table is locked and no transaction is active on any target table. RENAME TABLEis also applicable to views, but you cannot execute this statement to move views across databases.
Syntax
RENAME TABLE table_name TO [new_database_name.]new_table_name
[, table_name2 TO [new_database_name.]new_table_name2 ...];
Parameters
| Parameter | Description |
|---|---|
| table_name | The original table name. |
| new_table_name | The new table name. |
| table_name TO [new_database_name.]new_table_name | Multiple table names must be separated with commas (,). You can specify new_database_name to move the table to another database. |
Examples
Create tables
t1andt2.obclient> CREATE TABLE t1(c1 INT); obclient> CREATE TABLE t2(c1 INT);Rename table
t1ast11.obclient> RENAME TABLE t1 TO t11;Rename table
t11ast111and tablet2ast22.obclient> RENAME TABLE t11 TO t111, t2 TO t22;Move table
t22to databasemysql.obclient> RENAME TABLE t22 TO mysql.t22;