English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How to Rename a Table in MySQL?

To rename a table, you can use the alter and rename commands. These will be demonstrated with the help of the following steps-

Firstly, create a table using the create command. This gives the following-

mysql> CREATE table Employee
   -> (
   -> EmpId int,
   -> EmpName varchar(200)
   -> );

The syntax for changing the table name is as follows:

> alter table yourtableName rename toYourNewtableName;

The above syntax is used to change the table name by the following query:

mysql> alter table Employee rename to EmployeeTable;

Since the table name has been changed, use the SELECT statement to check if the renaming operation was successful. The query for this is as follows-

mysql> SELECT * from Employee;
ERROR 1146 (42S02): Table 'business.employee' doesn't exist

The above query returns an error because there is no longer a table named Employee. The name has been changed to EmployeeTable.

Execute the above query again using the table name 'EmployeeTable'. It is shown as follows-

mysql> SELECT * from EmployeeTable;
Empty set (0.00 sec)

Now, there are no errors in the above query because the table name Employee has been changed to EmployeeTable.