| title | Rename a Database | Microsoft Docs | ||
|---|---|---|---|
| ms.custom | |||
| ms.date | 10/02/2018 | ||
| ms.prod | sql | ||
| ms.prod_service | database-engine | ||
| ms.reviewer | |||
| ms.technology | |||
| ms.topic | conceptual | ||
| helpviewer_keywords |
|
||
| ms.assetid | 44c69d35-abcb-4da3-9370-5e0bc9a28496 | ||
| author | stevestein | ||
| ms.author | sstein | ||
| monikerRange | =azuresqldb-current||>=sql-server-2016||=sqlallproducts-allversions||>=sql-server-linux-2017||=azuresqldb-mi-current |
[!INCLUDEappliesto-ss-asdb-xxxx-xxx-md] This topic describes how to rename a user-defined database in [!INCLUDEssCurrent] or Azure SQL Database by using [!INCLUDEssManStudioFull] or [!INCLUDEtsql]. The name of the database can include any characters that follow the rules for identifiers.
-
Before you begin:
-
To rename a database, using:
-
Follow Up: After renaming a database
Note
To rename a database in Azure SQL Data Warehouse or Parallel Data Warehouse, use the RENAME (Transact-SQL) statement.
- System databases cannot be renamed.
- The database name cannot be changed while other users are accessing the database.
- In SQL Server, you can set a database in single user mode to close any open connections. For more information, see set the database to single-user mode.
- In Azure SQL Database, you must make sure no other users have an open connection to the database to be renamed.
Requires ALTER permission on the database.
Use the following steps to rename a SQL Server or Azure SQL database using SQL Server Management Studio.
-
In Object Explorer, connect to your SQL instance.
-
Make sure that there are no open connections to the database. If you are using SQL Server, you can set the database to single-user mode to close any open connections and prevent other users from connecting while you are changing the database name.
-
In Object Explorer, expand Databases, right-click the database to rename, and then click Rename.
-
Enter the new database name, and then click OK.
-
Optionally, if the database was your default database, see Reset your default database after rename.
Use the following steps to rename a SQL Server database using T-SQL in SQL Server Management Studio including the steps to place the database in single-user mode and, after the rename, place the database back in multi-user mode.
-
Connect to the
masterdatabase for your instance. -
Open a query window.
-
Copy and paste the following example into the query window and click Execute. This example changes the name of the
MyTestDatabasedatabase toMyTestDatabaseCopy.USE master; GO ALTER DATABASE MyTestDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE GO ALTER DATABASE MyTestDatabase MODIFY NAME = MyTestDatabaseCopy ; GO ALTER DATABASE MyTestDatabaseCopy SET MULTI_USER GO
-
Optionally, if the database was your default database, see Reset your default database after rename.
Use the following steps to rename an Azure SQL database using T-SQL in SQL Server Management Studio.
-
Connect to the
masterdatabase for your instance. -
Open a query window.
-
Make sure that no one is using the database.
-
Copy and paste the following example into the query window and click Execute. This example changes the name of the
MyTestDatabasedatabase toMyTestDatabaseCopy.ALTER DATABASE MyTestDatabase MODIFY NAME = MyTestDatabaseCopy ;
After renaming a database in SQL Server, back up the master database. In Azure SQL Database, this is not needed as backups occur automatically.
If the database you're renaming was set as your default database, use the following command to reset your default to the renamed database:
USE [master]
GO
ALTER LOGIN [your-login] WITH DEFAULT_DATABASE=[new-database-name]
GO