RENAME TABLE Examples

Lets see the example how to rename the table. We need to create the database first. We can create a database using the following command:

CREATE DATABASE GFG;

After creating we need to select that database. This can be done by using the following command:

USE GFG;

Now, create a table named as old_table using the following command and insert some data in the table

CREATE TABLE old_table (
     id INT PRIMARY KEY,
     name VARCHAR(50)
      );
INSERT INTO old_table (id, name) VALUES (1, 'John'), (2, 'Alice'), (3, 'Bob');

Now Rename the Table ‘old_table’ to ‘new_table’. For renaming a table RENAME TABLE statement is used.

RENAME TABLE old_table TO new_table;

For checking that the old_table is renamed to new_table query all rows and columns using SELECT statement.

SELECT * FROM new_table;

Output:

new_table

MySQL RENAME TABLE Statement

In MySQL, the RENAME TABLE statement is used to rename one or more tables in a database. We will discuss about MySQL RENAME TABLE statement. We will see how to rename one table, multiple tables, and a temporary table. We will also see the different ways we have to rename a table.

Sometimes our table is non-meaningful, so it is required to rename or change the name of the table. MySQL provides a useful syntax that can rename one or more tables in the current database.

We have to make sure that the new name that we are giving to the table does not exist in our database, then only we can give that name to any table and the table to which we are going to give a new name must exist in our database. Else, it will give an error message. MySQL RENAME TABLE statement can be used to change the table name.

Similar Reads

RENAME TABLE Statement

Efficient database management often involves modifying table structures to meet evolving requirements. In MySQL, the RENAME TABLE statement emerges as a powerful tool, allowing database administrators to seamlessly rename one or more tables....

RENAME TABLE Examples

Lets see the example how to rename the table. We need to create the database first. We can create a database using the following command:...

RENAME Multiple Tables

To rename multiple tables MySQL RENAME TABLE statement can be used....

ALTER Statement

In MySQL, we can also use the ALTER TABLE statement to rename the existing table in the current database. The following example will help you to understand it better:...

RENAME Temporary Table

In MySQL, temporary tables are tables that allows us to keep temporary data, which is visible and accessible in the current session....

Conclusion

In Conclusion ,we discussed about how to rename a table, and also how to rename multiple tables. And how to rename a temporary table. RENAME TABLE and ALTER statements are used to rename tables. It’s a quick and efficient way to update table names without messing with the data or structure. You can easily rename one or more tables in one go, making it a handy tool for managing your database. It ensures a smooth transition without any disruption to the data....