ALTER
MySQL
RENAME

MySQL Rename Table: 3 Different Approaches

intro

Learn the different ways available to change table names in MySQL, using queries or a database client.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT
MySQL logo MySQL
THE MYSQL DATABASE

When defining a database schema, you may think that the tables you created have perfect names. Over time, this may no longer be true. After spending some time on a database and struggling to find the data you want, you may begin to realize that some tables might have better names. After all, changing the name of a table is as common as renaming a file. Here is why there are different MySQL rename table approaches!

In this guide, you will see three different ways to rename a table in MySQL:

  • With an ALTER TABLE query.
  • With a RENAME TABLE query.
  • Approaches using a database client.

Let’s dive in!

What Happens When You Rename a MySQL Table

Under the hood, MySQL updates the metadata to reflect the new table name, ensuring that references to the specified table use the new name instead of the old name. During this process, the actual table data remains unchanged.

This means that MySQL will rename only those files that match the table_name, without making a copy of them. Consequently, renaming a table in MySQL is an instantaneous operation that does not require disk space.

With that being said, it is worth remembering that ALTER TABLE usually makes a copy of the table on the disk. Then, it applies the required changes to the data on the copy. Next, it then swaps the copy with the original table. So, in many cases, using ALTER TABLE will require disk space, especially if the table is big.

Also, when using ALTER or other queries, remember that privileges granted specifically to a table are not migrated to the new name when renaming that table. You must change them manually.

Now that you know how the MySQL rename table process works, you are ready to see three different approaches to performing it!

Approach #1: Rename a Table With ALTER TABLE

The MySQL ALTER TABLE statement allows you to change the name of an existing table through this syntax:

Copy
        
1 ALTER TABLE table_name RENAME TO new_table_name;

This query will update the name of the table_name table to new_table_name.

For example, assume that your database contains a table called articles and you want to rename it to products. This is the query you need to write:

Copy
        
1 ALTER TABLE products RENAME TO articles;

Wait for the operation to complete, and your MySQL database will now have an articles table instead of products.

Approach #2: Change the Name of One or More Tables With a RENAME TABLE Query

The MySQL RENAME TABLE query enables you to update the name of a table. This is how you can use it:

Copy
        
1 RENAME TABLE table_name TO new_table_name;

This is equivalent to running the ALTER TABLE ... RENAME TO query seen earlier.

What is different from ALTER TABLE is that RENAME TABLE allows you to rename multiple tables with a single query, as follows:

Copy
        
1 RENAME TABLE table_name_1 TO new_table_name_1, 2 table_name_2 TO new_table_name_2, 3 table_name_3 TO new_table_name_3;

Suppose you want to change the name of a table from customers to users. This is how you can do it:

Copy
        
1 RENAME TABLE customers TO users;

At the end of this query, the table customers will now have the new name users.

Approach #3: Rename a Table With Some Clicks In a Database Client

If running queries is not your thing, or you simply prefer to have visual feedback of what you are doing, you can perform the MySQL table rename operation in the database client. This is the easiest way to rename tables in MySQL.

First, choose a powerful database client, such as DbVisualizer. This complete tool equips you with everything you need to visually manage, query, and explore your databases, tables, and data.

In detail, DbVisualizer is a MySQL client with full support for all specific data types and features offered by the MySQL database technology. To rename a table in DbVisualizer, all you have to do is:

Step #1: Connect to your MySQL server as explained in the official documentation.

Step #2: Select your target server in the “Connections” menu on the left, right-click on it, and choose “Connect.”

Connecting to a database in DbVisualizer
Connecting to a database in DbVisualizer

Step #3: Open the “Databases” dropdown, find the database you want to explore, open its “Schemas” dropdown, and click on the “Tables” option:

Opening the “Tables” dropdown
Opening the Tables dropdown

Step #4: Identify the table you want to rename, right-click on it, and select the “Rename Table…” option:

Selecting the “Rename Table…” option
Selecting the Rename Table option

Step #5: In the “Rename table” popup, type the new name you want to give to the table in the “New Table Name” input, and press the “Execute” button:

Visually renaming a table in MySQL in DbVisualizer
Visually renaming a table in MySQL in DbVisualizer

Step #6: Confirm the operation and the newly renamed table will immediately appear in the “Tables” dropdown:

Note the “offers” table
Note the offers table

Et voilà! You just learned how to rename a table in MySQL!

MySQL Rename Table: Best Practices

Here is a list of MySQL rename table best practices:

  • When dealing with multiple databases, explicitly mention the database name along with the table name for clarity (e.g., db_name.table_name).
  • Adhere to MySQL naming conventions such as using lowercase letters or adding underscores for word separation. Also, use descriptive names to enhance readability.
  • Avoid using reserved words as table names to prevent potential conflicts and improve compatibility.
  • After renaming, perform thorough testing to make sure that the applications that connect to the database still work correctly.
  • Maintain documentation reflecting the table renaming, including reasons and any associated modifications made.

Extra: Move a MySQL Table to Another Database By Renaming

ALTER TABLE ... RENAME TO and RENAME TABLE can also be used to move a table to a different database. To do this, you only have to specify the databases in the table names, as shown below:

Copy
        
1 ALTER TABLE current_db.table_name RENAME TO other_db.table_name;

This will move the table_name table from the database current_db to the database other_db.

Equivalently, you can achieve the same goal with:

Copy
        
1 RENAME TABLE current_db.table_name TO other_db.table_name;

Keep in mind that if a table has triggers, attempting to rename it to a different database will fail with a Trigger in wrong schema (ER_TRG_IN_WRONG_SCHEMA) error. For more information, check out our complete guide on SQL triggers.

Conclusion

In this article, you understood what MySQL does under the scenes when you rename a table and how to do it in three different approaches. The first involves an ALTER TABLE command, the second a RENAME TABLE query, and the third a GUI tool.

In particular, a full-featured database client such as DbVisualizer makes it easy to deal with databases and tables while providing advanced features like query optimization and drag-and-drop query definition capabilities. Download DbVisualizer for free!

FAQ

Is it possible to rename all tables in MySQL Server?

With the right permission, it is possible. However, renaming MySQL system tables is not recommended, as it can lead to instability and data corruption. System tables, which store crucial information about the database structure, should retain their original names for proper functioning.

How to swap the names of two tables in MySQL?

To swap the names of two tables in MySQL, you can use a single RENAME TABLE query. Assuming you have tables named table_1 and table_2 (and that a table with the name tmp_table does not already exist), you can swap their names with:

Copy
        
1 RENAME TABLE table1 TO temp_table, 2 table2 TO table1, 3 temp_table TO table2;

This query renames table1 to temp_table, table2 to table1, and finally, temp_table to table2, effectively swapping their names.

What permissions does a user need to perform the MySQL table rename operation?

To perform a MySQL rename table operation, a user typically needs the ALTER privilege on the database containing the table. With that, the user should have sufficient permissions to modify the structure of tables.

What happens when you try to rename a MySQL table to a name that already exists?

If you try to rename an existing MySQL table to a new name that matches the name of another existing table, the operation will fail with this error:

Copy
        
1 [Code: 1050, SQL State: 42S01] Table 'table_name' already exists

How to rename a view in MySQL?

To rename a view in MySQL, you can use a RENAME TABLE statement as below:

Copy
        
1 RENAME TABLE view_name RENAME TO new_view_name;

This query effectively changes the name of the view from view_name to new_view_name. Thus, RENAME TABLE works for views, except that views cannot be renamed/moved into a different database.

Dbvis download link img
About the author
Antonello Zanini

Antonello is a software engineer, and often refers to himself as a technology bishop. His mission is to spread knowledge through writing.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

A Guide To the SQL DECLARE Statement

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SERVER 7 min 2024-12-12
title

SQL Upsert: Inserting a Record If It Does Not Exist

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2024-12-10
title

A Complete Guide to the MySQL COUNT Function

author Antonello Zanini tags MySQL 9 min 2024-12-09
title

MySQL Nullable Columns: Everything You Need to Know

author Antonello Zanini tags MySQL 6 min 2024-12-03
title

A Complete Guide to the MySQL GROUP_CONCAT Function

author Antonello Zanini tags Data aggregation MySQL 8 min 2024-12-02
title

How and Why to Add an Index in SQL

author Lukas Vileikis tags MySQL POSTGRESQL Search SQL SQL SERVER 8 min 2024-11-28
title

MySQL 9.0 and the New EXPLAIN Features

author Lukas Vileikis tags MySQL SQL 9 min 2024-11-27
title

MySQL CAST: A Guide to the Data Conversion Function

author Antonello Zanini tags MySQL 3 min 2024-11-25
title

List of SQL Reserved Words in All Major Databases

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 16 min 2024-11-20
title

SQL Variable: What It Is and How To Use It

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2024-11-18

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.

Cookie policy

We use cookies to ensure that we give you the best experience on our website. However you can change your cookie settings at any time in your browser settings. Please find our cookie policy here ↗