RENAMING TABLES

RENAMING TABLES

Status
Done

(show you how to rename a table using the RENAME TABLE statement.)

Summary: in this tutorial, you will learn how to rename tables using MySQL RENAME TABLE statement and ALTER TABLE statement.

Introduction to MySQL RENAME TABLE statement

Due to evolving business requirements, you need to rename the existing table to better align with the new situation. MySQL offers a valuable statement for renaming one or more tables.

To rename one or more tables, you can use the RENAME TABLE statement as follows:

RENAME TABLE table_name
TO new_table_name;

In this syntax:

  • table_name: This is the name of the table that you want to rename.
  • new_table_name: This is the new table name.

The table with the table_name must exist or the RENAME statement will fail with an error.

While executing the RENAME TABLE statement, you need to ensure that there are no active transactions or locked tables.

💡
Note that you cannot use the RENAME TABLE statement to rename a temporary table, but you can use the ALTER TABLE statement to rename a temporary table.

Renaming a name involves some technicality, and it is important to check which applications are currently using the table. Changing the table name would necessitate corresponding changes in the application code that reference it. Additionally, you will need to manually adjust other database objects, including viewsstored procedurestriggers, and foreign key constraints that reference the table.

MySQL RENAME TABLE examples

CREATE A TABLE

RENAMING A TABLE REFERENCED BY A VIEW

RENAMING A TABLE THAT IS REFERENCED BY A STORED PROCEDURE

Renaming a table that is referenced by foreign key constraint

Renaming multiple tables