MySQL MariaDB

How to Rename a Table in MySQL Using Terminal?

While using MySQL, often we need to modify or change the name of a database table due to some issues, such as business situations or requirements change, spelling mistakes, not meaningful names, or other reasons. In this situation, MySQL provides different statements to modify the table names.

This article will talk about:

How to Rename a Single Table in MySQL Using “ALTER” Query?

To rename a single table in MySQL using the “ALTER” statement, follow the provided instructions:

    • Access the MySQL server through the terminal.
    • List existing databases.
    • Check database tables and select table names.
    • Run the “ALTER TABLE <existing-name> RENAME <new-name>;” statement.

Step 1: Connect With MySQL Server

First, connect with the MySQL server by utilizing the “mysql” statement with the username and password:

mysql -u root -p

 

Step 2: List Databases

Run the “SHOW” query to list all existing databases:

SHOW DATABASES;

 
From the displayed list, we have selected the “mynewdb” database:


Step 3: Change Database

Next, execute the provided command to change the database:

USE mynewdb;

 

Step 4: View Tables

After that, display the existing tables of the current database by running the below-stated command:

SHOW TABLES;

 
Here, we have selected the database table name which needs to be renamed. In our case, it is the “employee_table” table:


Step 5: Alter Table

Finally, run the “ALTER TABLE” statement to rename the database table name:

ALTER TABLE employee_table RENAME employee_data;

 
Here:

    • ALTER TABLE” statement is used to rename the table name.
    • employee_table” is the existing name database table.
    • RENAME” statement changes the name of an existing database table name to a new name.
    • employee_data” is the new table name.

It can be observed that the query has been executed successfully:


Step 6: Verify Alter Table

Execute the “SHOW” statement to ensure that the table name changed or not:

SHOW TABLES;

 
Below-output indicates that the table name has been modified:

How to Rename Single Table in MySQL Using “RENAME” Query?

Another statement to modify the name of the single database table name is stated below:

RENAME TABLE employee_table TO employee_data;

 
Here:

    • RENAME TABLE” statement is used to modify the name of the database table.
    • employee_table” is the existing name of the database table.
    • employee_data” is the new name of the current database table.

From the given output, the “Query OK” indicates that the table has been modified:


Execute the “SHOW” statement to list the current database table:

SHOW TABLES;

 
The “employee_table” table name has been changed into a new table name “employee_data”:

How to Rename Multiple Tables in MySQL Using “RENAME” Query?

The “RENAME” command can also be utilized for modifying/changing the names of multiple tables at once. To do so, try the following steps:

Step 1: View Database Table

First, list all tables by utilizing the “SHOW” statement and select the tables which need to modify:

SHOW TABLES;

 
As you can see, we have selected the “employee_table” and “testdata” tables:


Step 2: Use RENAME Query

Then, execute the “RENAME” statement along with the selected table’s old names, and new names, separated through the comma:

RENAME TABLE employee_table TO employee_data, testdata TO demo_table;

 
Here, the “employee_table” and “testdata” are the old name of the tables:


Step 3: Verification

To ensure that the tables name are modified or not, execute the “SHOW” statement:

SHOW TABLES;

 

That’s all! We have provided different ways to rename a table in MySQL using a terminal.

Conclusion

To rename a table in MySQL using the terminal, the “ALTER TABLE <existing-name> RENAME <new-name>;” and the “RENAME TABLE <existing-name> TO <new-name>;” statements are used. Moreover, the “RENAME TABLE” statement can be used to modify multiple table names at once. This article demonstrated the method to rename the table in MySQL using the terminal.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.