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:
Create the table and insert data into them using the following commands:
CREATE TABLE old_table (
id INT PRIMARY KEY,
name VARCHAR(50)
);
INSERT INTO old_table (id, name) VALUES (1, 'John'), (2, 'Alice');
Now use ‘ALTER TABLE’ statement with the ‘RENAME‘ option and change the table name.
Use the following command to rename the table using the ‘ALTER’ statement:
ALTER TABLE old_table RENAME TO new_table;
Use SELECT Statement to check whether the table is renamed or not.
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.