How to rename a database in Azure SQL

In this Azure SQL tutorial, we will discuss the topic “Azure SQL rename database“. We will discuss how to rename the database in Azure SQL.

  • Can we rename Azure SQL database
  • Azure SQL rename database
  • Rename Azure SQL database using PowerShell
  • Azure SQL rename database portal

Can we rename Azure SQL database

We can rename the Azure SQL database. If you are familiar with SQL Server, you will notice that the methods for renaming the database are almost the same in both types of databases. Also, you will see some examples that will help you to understand better.

Note: You cannot rename a database while it is being accessed by another service. You have to stop all the user connections to the database when you will rename the database.

Azure SQL rename database

In this section, we will learn how to rename a database in Microsoft Azure. There are two ways in which you can rename a database:

  1. Using GUI
  2. Use T-SQL query

Using GUI: There are various IDEs like SQL server management studio, Azure Data Studio, Microsoft Visual Studio, etc. These IDEs provide user-friendly ways to interact with the Azure SQL database.

For example, SQL Server Management Studio is the most common IDE used to connect to any SQL Server database or any Azure SQL database. To rename a database in SQL server management studio, follow the below steps:

  • Connect to the Azure SQL database. If you do not know how to connect to the Azure SQL database with SQL Server Management Studio, you can read our article Connect to Azure SQL database.
  • In the Object Explorer Window, right click on the database and click Rename.
rename azure sql database
Rename the database

After renaming the azure SQL database, it will show you a prompt asking for confirmation. Confirm the changes. The changes will not reflect immediately. It will take some time. After some time, refresh the database and you will see that the database has been renamed.

Using T-SQL query: If you want to rename the Azure SQL database using the T-SQL query method, you can use the ALTER DATABASE statement. The syntax for renaming the database is:

ALTER DATABASE [dbname] MODIFY NAME = [newdbname]

We are using SQL Server Management Studio to connect to the Azure SQL database.

For example, we have created a single serverless database named DemoDB in Azure SQL. If we want to rename this database, we can connect to the database first and write the following query in the query editor window:

ALTER DATABASE [DemoDB] MODIFY NAME = [NewDemoDB]

You can also write this query in the query editor in the Azure portal. After this query has been executed, it will take some time i.e. about 1-2 minutes to reflect the changes. Refresh the azure portal to see the changes.

Read How to create table in azure sql database

Rename Azure SQL database powershell

In this section, you will learn how to rename a database using Windows PowerShell. You can use the Az.Sql module in PowerShell to rename the Azure SQL database.

Firstly, you have to connect to the Azure SQL database from Windows PowerShell. You will need to install some tools in PowerShell. You can refer to our article connect to Azure SQL database if you want to know how you can connect to the Azure SQL database using PowerShell.

Once you are connected to the Azure SQL database from Windows, you can execute the below command to rename the Azure SQL database:

Set-AzSqlDatabase -ResourceGroupName "<your resource group>" -ServerName '<server name>' -DatabaseName '<original database name>' -NewName '<New name of the database>'

For example, if my original database name is DemoDB and I want it to rename as DemoDatabase, I will write the command as:

Set-AzSqlDatabase -ResourceGroupName "GroupAzure" -ServerName 'mysql1000' -DatabaseName 'DemoDB' -NewName 'DemoDatabase'

Here GroupAzure is the resource group under which the database is created and mysql1000 is the server name on which the database is created.

Thus, you might have learned how you can use Windows PowerShell to rename a database in Azure SQL.

Read Cannot drop schema because it is being referenced by object

Azure SQL rename database portal

In this section, you will learn how to rename a database in the Azure portal. If we have a database named DemoDB and we want to rename it to NewDemoDB, we will follow the below steps:

  • Go to Azure portal and navigate to the Azure SQL database.
  • In the side menu, click on Query editor (preview).
  • Login into the database and execute the below query in the query editor window:
ALTER DATABASE [DemoDB] MODIFY NAME = [NewDemoDB]
azure sql rename database
Query Editor

Once you have executed the query, you may face the error as shown in the below image:

azure sql rename database portal
Renaming Database in Azure

But if you refresh the database, you will find that you cannot access the database. You have to wait for some time and refresh the Azure portal. Open the SQL databases again and you will find your database with the new name.

You may like the following Azure SQL tutorials:

In this tutorial, we learned how to rename a database in Microsoft Azure.

  • Can we rename Azure SQL database
  • Azure SQL rename database
  • How to Rename Azure SQL database using powershell
  • Azure SQL rename database portal