MySQL RENAME TABLE Statement (original) (raw)

Last Updated : 14 Mar, 2026

The MySQL RENAME TABLE statement is used to change the name of an existing table in a database. It helps keep the database organized by updating table names without affecting the stored data.

**Syntax:

RENAME TABLE old_table_name TO new_table_name [, ...];

Working with RENAME TABLE

The RENAME TABLE statement in MySQL is used to change the name of an existing table without affecting its stored data.

1. Renaming a Single Table

In this example, we create a table, insert some data, and then rename the table.

**Query:

-- Create a table and insert data
CREATE TABLE old_table (
id INT PRIMARY KEY,
name VARCHAR(50)
);
INSERT INTO old_table (id, name) VALUES (1, 'John'), (2, 'Alice'), (3, 'Bob');

-- Rename the table 'old_table' to 'new_table'
RENAME TABLE old_table TO new_table;

-- Check if the table is renamed
SELECT * FROM new_table;

**Output:

rename_2

2. Renaming Multiple Tables

MySQL also allows renaming multiple tables within a single statement.

**Query:

-- Create two tables
CREATE TABLE old_table1 (
id INT PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE old_table2 (
id INT PRIMARY KEY,
description TEXT
);

-- Insert data into both tables
INSERT INTO old_table1 (id, name) VALUES (1, 'John'), (2, 'Alice');
INSERT INTO old_table2 (id, description) VALUES (1, 'Description 1'), (2, 'Description 2');

-- Rename both tables simultaneously
RENAME TABLE old_table1 TO new_table1, old_table2 TO new_table2;

-- Check if tables are renamed
SELECT * FROM new_table1;
SELECT * FROM new_table2;

**Output:

rename_4

**Output:

rename_3

After renaming 'old_table1' to 'new_table1' and 'old_table2' to 'new_table2', the SELECT statements verify the renaming:

3. Renaming a Table Using ALTER TABLE

Another way to rename a table in MySQL is by using the ALTER TABLE statement.

**Query:

-- Create a table and insert data
CREATE TABLE old_table (
id INT PRIMARY KEY,
name VARCHAR(50)
);
INSERT INTO old_table (id, name) VALUES (1, 'John'), (2, 'Alice');

-- Rename the table using ALTER TABLE
ALTER TABLE old_table RENAME TO new_table;

-- Check if the table is renamed
SELECT * FROM new_table;

**Output:

rename_5

4. Renaming a Temporary Table

In MySQL, temporary tables store temporary data that exists only during the current session. These tables can also be renamed.

**Query:

-- Create a temporary table
CREATE TEMPORARY TABLE temp_table (
id INT PRIMARY KEY,
name VARCHAR(50)
);

-- Insert data into the temporary table
INSERT INTO temp_table(id,name) VALUES (1,'John'),(2,'Alice');

-- Rename the temporary table
ALTER TABLE temp_table RENAME TO new_temp_table;

-- Check if the temporary table is renamed
SELECT * FROM new_temp_table;

**Output:

rename_6

The temporary table temp_table is renamed to new_temp_table using ALTER TABLE. The data stored in the table remains unchanged.