RENAME TABLE instantly changes a table’s name or schema without copying data.
RENAME TABLE changes a table’s name in the data dictionary instantly; no rows are moved or copied. The command can also relocate a table to a different database by adding a target schema prefix.
Use RENAME TABLE old_name TO new_name;
.Supply multiple old TO new
pairs, comma-separated, to execute several renames atomically in one statement.
Rename Orders
to Orders_2024
:
RENAME TABLE Orders TO Orders_2024;
Prefix each name with its schema:
RENAME TABLE shop.Orders TO archive.Orders_2023;
Yes. Example:
RENAME TABLE Customers TO Customers_old,
Products TO Products_backup;
All rename pairs run in a single transaction.If any pair fails, none are applied, ensuring consistency.
Lock out dependent code, update foreign keys, views, and stored procedures, and schedule during low traffic. Backup first and verify InnoDB for transactional safety.
See details below to avoid broken dependencies and lock timeouts.
.
No. It only updates metadata, so the operation is instantaneous regardless of table size.
Yes. Run another RENAME TABLE to switch the name back, or restore from backup if dependencies were already changed.
No. Foreign key definitions reference the table name literally. Update or recreate them if the renamed table is part of a foreign key relationship.