Open In App

SQL Server Rename Table

Last Updated : 28 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In SQL Server, renaming tables is a frequent operation that we often require during database maintenance or schema changes. This article ensures your seamless transition through the table-renaming process without compromising data integrity. it provides comprehensive guidance and guarantees protection for your valuable information – all in pursuit of an unimpeachable database management strategy.

Prerequisites:

  1. Permissions: The table belongs to a schema, and you must possess ALTER permission on that specific schema.
  2. Backups: It is advisable: before implementing any structural modifications, to take a backup of the database to safeguard data integrity.

Syntax:

sp_rename ‘old_table_name’, ‘new_table_name’

This procedure allows to change the name of a table while preserving structure, associated constraints, indexes, and triggers.

Example 1: Renaming of table

old_table_name: persons

new_table_name: people

old_table_name-persons

old_table_name: persons

After execution of query,

After-execution-of-query-new_table_name-people

After execution of query, new_table: people

Example 2: Updating Foreign Key Relationships While Renaming

Let’s suppose we have two tables ‘Orders’ and ‘OrderDetails’, linked by a foreign key constraint. Our target is to rename ‘Orders’ to ‘SalesOrders’ while ensuring foreign key relationship is maintained.

Initial Setup

-- Create the Orders table
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE
);

-- Create the OrderDetails table with a foreign key constraint
CREATE TABLE OrderDetails (
OrderDetailID INT PRIMARY KEY,
OrderID INT FOREIGN KEY REFERENCES Orders(OrderID),
ProductID INT,
Quantity INT
);

Before renaming let’s insert some data into the tables:

-- Insert data into Orders table
INSERT INTO Orders (OrderID, CustomerID, OrderDate)
VALUES (1, 101, '2023-01-01'), (2, 102, '2023-01-02');

-- Insert data into OrderDetails table
INSERT INTO OrderDetails (OrderDetailID, OrderID, ProductID, Quantity)
VALUES (101, 1, 201, 10), (102, 2, 202, 5);

Using sp_rename

-- Rename the Orders table to SalesOrders
EXEC sp_rename 'Orders', 'SalesOrders';

Verifying the changes

-- Verify the updated table names
SELECT * FROM SalesOrders; -- Formerly Orders

-- Verify data in OrderDetails (foreign key relationship)
SELECT * FROM OrderDetails;

Updating foreign key relationships

-- Drop the existing foreign key constraint
ALTER TABLE OrderDetails DROP CONSTRAINT FK_OrderDetails_Orders;

-- Recreate the foreign key constraint with the new table name
ALTER TABLE OrderDetails
ADD CONSTRAINT FK_OrderDetails_SalesOrders
FOREIGN KEY (OrderID) REFERENCES SalesOrders(OrderID);

Ensuring foreign key updates:

-- Verify data in OrderDetails with the updated foreign key constraint
SELECT * FROM OrderDetails;

Conclusion

A well-organized and adaptable database necessitates efficient table renaming in SQL Server; this is a vital aspect of maintenance. By following the provided guidance, guaranteeing required permissions and backups and you can navigate through this process seamlessly without compromising data integrity: it’s an operation that demands your attention. Table renaming, when approached meticulously–is instrumental in shaping an unimpeachable strategy for managing databases.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads