Rename a Database in SQL Server

Rename a database in SQL Server is a straightforward process that can be done using the ALTER DATABASE statement.

Syntax:

ALTER DATABASE {old_database_name} MODIFY NAME = {new_database_name};

Example:

Let's say we want to rename a database named OldDB to NewDB.

Before Renaming:

-- Check the current database name 
SELECT name FROM sys.databases WHERE database_id = DB_ID();
-- Output: OldDB

Rename the Database:

ALTER DATABASE OldDB MODIFY NAME = NewDB;

After Renaming:

-- Check the new database name 
SELECT name FROM sys.databases WHERE database_id = DB_ID();
-- Output: NewDB

Important Notes:

  • You need to be a member of the sysadmin fixed server role or have the ALTER permission on the database to rename it.
  • Renaming a database does not change the database's physical file names or locations.
  • If you have any connections open to the database, you may need to close them before renaming the database.
  • Renaming a database can break any dependencies, such as synonyms, views, or stored procedures, that reference the old database name. You may need to update these dependencies after renaming the database.

That's it! Your database should now have a new name.