LogIn
I don't have account.

How to Rename a Tables in SQL

DevSniper

169 Views

Renaming a table in SQL is a simple operation, but it requires careful attention to avoid issues with dependencies or application code. It is a common task in database administration. There may be several reasons why you might want to rename a table. Some reasons are you want a more descriptive name, reorganizing your system, to avoid naming conflicts, adopting new naming conventions etc. Renaming a table needs to be done with care to avoid data loss, service disruptions or inconsistencies.

SQL provides straightforward methods to rename tables, but the exact syntax may vary depending on the relational database management system. This article covers how to rename tables in popular databases like MySQL, SQL Server, PostgreSQL and Oracle.

Renaming a Table in MySQL

MySQL provides two methods to rename table. RENAME TABLE statement and the ALTER TABLE statement.

RENAME TABLE old_table_name TO new_table_name;
           OR
ALTER TABLE old_table_name RENAME TO new_table_name;

Example

If you want to rename the Employees table to Staffs, you can use the following command

RENAME TABLE Employees TO Staffs;
              OR
ALTER TABLE Employees RENAME TO Staffs;

Verification

SELECT * FROM Staffs;

When you run above query after Renaming table you will get all records of the table and if you try to use the old table name Employees, the database will return an error saying the table does not exist.

Renaming a Table in SQL Server

In SQL Server, there is no direct SQL statement like RENAME TABLE. Instead of this, SQL Server provides the system stored procedure sp_rename to rename tables and other database objects. The sp_rename procedure also works for renaming tables, columns and other database objects.

EXEC sp_rename 'old_table_name', 'new_table_name';

Example

If you want to rename the Employees table to Staffs in SQL Server, you would use the following command

EXEC sp_rename 'Employees', 'Staffs';

Verification

SELECT * FROM Staffs;

When you run above query on SQL Server after running Rename Query. You will get all records of the table and if you try to use the old table(Employees) , SQL Server will return an error saying the table does not exist.

Note :- This stored procedure does not automatically update references to the table in foreign keys, views or stored procedures. You need to manually update these references.

Renaming a Table in PostgreSQL

In PostgreSQL, you can rename a table using the ALTER TABLE statement.

ALTER TABLE old_table_name RENAME TO new_table_name;

Example

If you want to rename the Employees table to Staffs in PostgreSQL, your rename query will be

ALTER TABLE Employees RENAME TO Staffs;

Verification

SELECT * FROM Staffs;

When you execute the above query on a PostgreSQL database, after running the rename query, you will get all records in the table. However, if you attempt to access the old table (Employees), the database will return an error saying that the table does not exist.

Note :- PostgreSQL automatically handles internal references to the table, so you don’t need to worry about manually updating constraints or indexes.

Renaming a Table in Oracle

In Oracle, the RENAME statement is used to change the name of a table.

RENAME old_table_name TO new_table_name;

Example

rename the Employees table to Staffs

RENAME Employees TO Staffs;

Verification

SELECT * FROM Staffs;

Notes

  • In MySQL, you can use RENAME TABLE or ALTER TABLE to rename tables.
  • In SQL Server, use the sp_rename stored procedure to rename tables.
  • In PostgreSQL, use ALTER TABLE to rename tables.
  • In Oracle, use RENAME statement to rename tables.
  • Avoid renaming system tables :- System tables are crucial for the proper functioning of the database. Renaming these tables can interfere with database operations. Therefore, it is advisable to avoid renaming system tables unless you have a good reason and understand the impact.
  • After renaming a table, you must update all stored procedures, views, triggers and application code that reference the table to ensure they continue to function correctly.
  • Always test the renaming operation in a development or staging environment before performing it on a production database. This helps to ensures that all dependencies are properly updated and the application behaves as expected.
  • It’s recommended to take back up of the database. before performing such operations on database.
  • You must have the necessary privileges to rename table.