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.
Considerations
- The rename operation proceeds automatically. When the rename operation is in progress, other threads cannot read any tables.
- 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 table is locked and no transaction is active. RENAME TABLEis also applicable to views, but cannot 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;