How to rename a SQL Server database?

Expert User Verified

This article describes, how to rename a SQL database name by T-SQL query.

USE master;  
GO  
ALTER DATABASE OldDBName SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE OldDBName MODIFY NAME = NewDBName;
GO  
ALTER DATABASE NewDBName SET MULTI_USER;
GO

There are some limitations to renaming a SQL database name. Please find below the limitations:

  • System databases can't be renamed
  • Databases cannot be renamed when anyone is using that database
  • Renaming a database name does not mean that it will rename the physical file of the database like MDF, NDFand LDF file name.


ALTER permission is required to rename the SQL Database.

Find below the T-SQL query to rename a SQL database.

USE master;  

GO  

ALTER DATABASE OldDBName SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

GO

ALTER DATABASE OldDBName MODIFY NAME = NewDBName;

GO  

ALTER DATABASE NewDBName SET MULTI_USER;

GO



References and Credits

Comments

Leave a Comment