Introduction

Renaming a database in SQL Server is often required during maintenance tasks such as deploying backups, preparing testing environments, or aligning naming conventions with organizational standards. While renaming a database might seem straightforward, it involves key considerations to ensure the operation is safe and seamless. This article explores the process using practical examples and SQL commands.

Why Rename a Database?

Database renaming is commonly performed for the following reasons.

Key Considerations

Before renaming a database, take the following precautions.

Let’s look at a practical example of renaming a database using SQL commands. Here, we’ll rename the AdventureWorksLT2022 database.

-- Step 1: Rename AdventureWorksLT2022 to AdventureWorksLT2022_NEW
USE master;
GO
ALTER DATABASE AdventureWorksLT2022 
    SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
ALTER DATABASE AdventureWorksLT2022 
    MODIFY NAME = AdventureWorksLT2022_NEW;
ALTER DATABASE AdventureWorksLT2022_NEW 
    SET MULTI_USER;
GO

Before running the script.

Database

After running the script.

Object Explorer

Now, let's roll back the database name to AdventureWorksLT2022.

-- Step 2: Rename AdventureWorksLT2022_NEW back to AdventureWorksLT2022
USE master;  
GO  
ALTER DATABASE AdventureWorksLT2022_NEW  
SET SINGLE_USER WITH ROLLBACK IMMEDIATE;  
ALTER DATABASE AdventureWorksLT2022_NEW  
MODIFY NAME = AdventureWorksLT2022;  
ALTER DATABASE AdventureWorksLT2022  
SET MULTI_USER;  
GO

Executing the above script will produce the following output.

Output

Key Notes

Troubleshooting

Conclusion

Renaming a database in SQL Server is a straightforward process but requires careful preparation and execution to avoid disruptions. By following the steps outlined in this article, you can confidently rename databases while maintaining system integrity. Always remember to back up your data and plan for dependency updates. Whether you're managing backup environments or conforming to naming conventions, these techniques ensure a smooth and efficient renaming process in SQL Server.