How to Rename a Table in MySQL?
MySQL offers two ways to rename tables. The first one uses the ALTER TABLE
syntax:
ALTER TABLE old_table_name RENAME new_table_name;
The second way is to use RENAME TABLE
RENAME TABLE old_table_name TO new_table_name;
RENAME TABLE
offers more flexibility. It allows renaming multiple tables in one statement. This can be useful when replacing a table with a new pre-populated version:
RENAME TABLE products TO products_old, products_new TO products;
The above statement is executed left to right, so there’s no conflict naming products_new
to products
since the existing table has already been renamed to products_old
.