MySQL MariaDB

How to rename table in MySQL

What is meant by rename a table in MySQL? It is the technique in MySQL by which we can change the names of the existing table, for example, we have a table in the database with the name “ABC” to display the list of students of the school, and we want to use the same table for making a list of students of the school bus, so instead of creating the new table with the same data, we can change the name of the existing table. For this purpose, we use the “Rename” technique to edit the name of the existing table.
This article will make us understand how we can rename the table in MySQL.

How we can rename a table in MySQL

To understand how we can rename the table in MySQL, we will first open the terminal and go to the MySQL environment by using the command

$ sudo mysql

Once we are in MySQL, we should list the databases we have already created in MySQL as

SHOW DATABASES;

In the output, we can see the list of databases and can also see the database “Organization” which has been created by us. To access this database, we will execute the following command with the database name.

USE Organization;

Once we are in the database, display the list of tables of this database.

SHOW TABLES;

Open the table, “Employees_data” using the command DESCRIBE with the name of the table.

DESCRIBE Employees_data;

In the output, a table has been displayed. Now if we want to change the name of the table we can change it by command whose general syntax is.

RENAME TABLE [old_name] TO [new_name];

In our example, we use this command as:

RENAME TABLE Employees_data TO myEmployee_data;

The name has been changed now to verify it we will open the table by its new name.

DESCRIBE myEmployee_data;

We can also rename the table by using the ALTER command. The general structure of the command will be.

ALTER TABLE [old_name_of_table] RENAME TO  [new_table_of_table];

In our case, it will be as:

ALTER TABLE myEmployee_data RENAME TO TSEP_Employees_data;

For the verification purpose, we again display the table by using the new name as:

 DESCRIBE TSEP_Employees_data;

We can also use ALTER TABLE command as:

ALTER TABLE [old_name_of_table] RENAME [new_name_of_talble];

In our example, it will be used as:

ALTER TABLE TSEP_Employees_data RENAME LinuxHint_Employees_data;

For the display of the newly named table:

DESCRIBE LinuxHint_Employees_data;

Now we will exit the MySQL environment:

exit

Conclusion

In this article, we have discussed with some examples, a technique of how to rename a table works in MySQL. We rename a table with the command of “RENAME” as well as with the command of “ALTER”. I hope this article will help you in understanding the technique of “renaming a table”.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.