- SQL - Home
- SQL - Roadmap
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Comments
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Cheatsheet
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL RENAME TABLE
The SQL RENAME TABLE Command
The RENAME TABLE command is used in SQL to change the name of an existing table. This is useful when you want to give your table a new name without affecting the data or structure of the table.
For example, if your table is named CUSTOMERS and you want to rename it to BUYERS, you can use the RENAME command to make this change easily.
However, not all database systems use the same syntax for renaming a table. The command differs between MySQL, SQL Server, and PostgreSQL:
- MySQL directly supports RENAME TABLE.
- SQL Server does not support this directly. You must use a special command called sp_rename.
- PostgreSQL does not support RENAME TABLE, but allows renaming using ALTER TABLE ... RENAME TO command.
Renaming a Table in MySQL Server
In MySQL, the RENAME TABLE command is used to rename a table. It is a simple and direct method to change a table name.
Syntax
Following is the basic syntax to rename a table in MySQL Server:
RENAME TABLE old_table_name TO new_table_name;
Example
Let us create a table with the name CUSTOMERS which contains the personal details of customers including their name, age, address and salary etc. as shown below:
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Now, let us insert few records into this table using the INSERT statement as follows:
INSERT INTO CUSTOMERS VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ), (2, 'Khilan', 25, 'Delhi', 1500.00 ), (3, 'Kaushik', 23, 'Kota', 2000.00 ), (4, 'Chaitali', 25, 'Mumbai', 6500.00 ), (5, 'Hardik', 27, 'Bhopal', 8500.00 ), (6, 'Komal', 22, 'Hyderabad', 4500.00 ), (7, 'Muffy', 24, 'Indore', 10000.00 );
The table will be created as follows:
| ID | Name | Age | Address | Salary |
|---|---|---|---|---|
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | Kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | Hyderabad | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
In the following example query, we are renaming the table CUSTOMERS to BUYERS in MySQL Server:
RENAME TABLE CUSTOMERS TO BUYERS;
We get the output as shown below:
Query OK, 0 rows affected (0.02 sec)
This command only changes the table name. All existing data and column definitions remain unchanged:
Verifying the Rename
After renaming a table, you can confirm the change by using the following SQL command:
SHOW TABLES;
This command will list all the tables in the current database. You should now see BUYERS instead of CUSTOMERS:
| Tables_in_testdb |
|---|
| BUYERS |
| salary |
You can also verify the renamed table by retrieving its data using the SELECT statement as shown below:
SELECT * FROM BUYERS;
This will display all records from the newly renamed BUYERS table:
| ID | Name | Age | Address | Salary |
|---|---|---|---|---|
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | Kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | Hyderabad | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
Renaming a Table in SQL Server
SQL Server does not support RENAME TABLE command directly. Instead, it uses a built-in stored procedure called sp_rename to rename tables.
Syntax
Following is the basic syntax to rename a table in SQL Server:
EXEC sp_rename 'old_table_name', 'new_table_name';
Example
The following command renames the above created CUSTOMERS table to WORKERS in SQL Server:
EXEC sp_rename 'CUSTOMERS', 'WORKERS';
We get the output as shown below:
Caution: Changing any part of an object name could break scripts and stored procedures. Completion time: 2025-07-29T15:46:44.1359549+05:30
SQL Server shows this standard cautionary message after a successful rename.
Verification
We can verify whether the changes are reflected by retrieving its contents using the SELECT statement as follows:
SELECT * FROM WORKERS;
This will list down all the records available in WORKERS table as follows:
| ID | Name | Age | Address | Salary |
|---|---|---|---|---|
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | Kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | Hyderabad | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
Since you have renamed the table to WORKERS, if you try to retrieve the details using the old table name, it will throw an error indicating that the table does not exist.
Renaming a Table in PostgreSQL
In PostgreSQL, the renaming is done using the ALTER TABLE command with a RENAME TO clause.
Syntax
Following is the basic syntax to rename a table in PostgreSQL:
ALTER TABLE old_table_name RENAME TO new_table_name;
Example
In this example query, we rename the above created CUSTOMERS table to BUYERS in PostgreSQL:
ALTER TABLE CUSTOMERS RENAME TO BUYERS;
We get the output as shown below:
ALTER TABLE
PostgreSQL outputs the keyword ALTER TABLE to confirm that the command executed successfully.
Verification
We can verify whether the changes are reflected by retrieving its contents using the SELECT statement as follows:
SELECT * FROM BUYERS;
This will list down all the records available in BUYERS table as follows:
| ID | Name | Age | Address | Salary |
|---|---|---|---|---|
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | Kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | Hyderabad | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
Important Points on Renaming Table
Following are a few important points to remember before renaming a table in a database:
- The RENAME command only changes the name of the table. It does not change the data or structure.
- Make sure no other part of your application depends on the old table name.
- You must have the required permission to rename a table.
- Each database system has its own preferred syntax. Use the correct one to avoid errors.