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.
1. Create a Temporary table
Create a temporary table using the following command:
CREATE TEMPORARY TABLE temp_table (
id INT PRIMARY KEY,
name VARCHAR(50)
);
2. Insert data
Now, insert data in temporary table using the following command:
INSERT INTO temp_table (id, name) VALUES (1, 'John'), (2, 'Alice');
3. Rename Table
To rename temporary tables , we cannot use RENAME TABLE statement, we need to use ALTER TABLE statement.
Use the following command for rename the temporary table:
ALTER TABLE temp_table RENAME TO new_temp_table;
4. Check whether the Table is Renamed or Not
To check the table is renamed or not, you can use a SELECT statement.
Use the following command:
SELECT * FROM new_temp_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.