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:
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.