How to create Azure SQL database

How to create an Azure SQL database

This Azure SQL tutorial will discuss the quick steps to create an SQL database in Azure using Azure Portal and PowerShell. Apart from this, we will also discuss the below topics

How to create SQL database in Azure [Azure Portal]

Before starting the topic of How to create an Azure SQL database, you can check out the Azure SQL Database Tutorial for a better idea.

Well, it’s quite easy to create the Azure SQL database using the Azure Portal.

Follow the below steps to create a free SQL Database in Azure.

1. Log in to the Azure portal (https://portal.azure.com/)

2. Search for Azure SQL and select it.

azure sql database tutorial

3. Click on the Add button On the Azure SQL page.

azure sql create database

4. On the Select SQL deployment option window, click the Create button from the SQL database option.

azure sql database tutorial

5. On the Basics, Fill the below details.

  • Subscription: Select the correct subscription.
  • Resource group: Create a new resource group by clicking on the Create new link.
  • Database name: Provide a meaningful database name.

Server: Click on the Create new link to create a new server. Enter the below details on the New server form.

  • Server name: Enter a unique server name
  • Server admin login: Enter Azure user.
  • Password: Enter a password and enter it again in the Confirm password field.
  • Location: Choose a location here

Click on the OK button

create sql database in azure

6. Compute + storage: It will load a default one. if you want to reconfigure the defaults, select the Configure database link.

On the Configure page, you can choose the below details.

  • You can change the Compute tier from Provisioned to Serverless.
  • Change the settings for vCores and Data max size.
  • Click on the Change Configuration button to change the hardware generation.

Now click on Apply.

how to create azure sql database

7. Click the Next: Networking> button in the Networking tab.

In the Networking tab, Choose the below options

  • Select the Connectivity method as a Public endpoint.
  • Choose the No option for Allow Azure services and resources to access this server.
  • Select the Yes option for Add current client IP address.
azure sql database create

8. Click on the Next: Additional settings > button to go to the Additional settings page

  • Select the Sample option to Use existing data
  • Click on the Review + Create button.
azure sql database creation

9. Click the Create button to create the Azure SQL database.

Note: Do not forget to read the terms properly before clicking the Create button.

azure sql for beginners

Query the SQL database in Azure

Once your database is created using the above steps, it is time to query it.

To query the database follow the below steps.

1. Log in to the Azure portal, search for SQL databases, and select it.

azure sql for beginners

2. Select the database you created From the SQL databases window.

3. Select the Query editor (preview) option from the left menu on the database page.

4. Enter your credentials under SQL server authentication, which you generated earlier on the New server page, and then click the OK button.

5. Enter your SQL query in the query editor and click the Run button to execute the query. It will show the output in the Results pane.

You can also check out how To Create Tables In SQL Azure Database

Create Azure SQL database using Powershell

Let’s discuss how to create an Azure SQL database using PowerShell. Follow the below steps

Set the subscription details

Use the below PowerShell cmdlet to set the subscription details.

$MysubscriptionId = '1cdf4300-dee5-4518-9c9c-feaa72a5cbd1'
Set-AzContext -SubscriptionId $MysubscriptionId 

You can check out the screenshot below for your reference.

How to create an Azure SQL database using PowerShell

Creation of the Resource group

Use the below PowerShell cmdlet to create the resource group.

$MyresourceGroupName = "newresgroup-$(Get-Random)"
$Mylocation = "Eastus"
$MyresourceGroup = New-AzResourceGroup -Name $MyresourceGroupName -Location $Mylocation

See the image below.

create an Azure SQL database using PowerShell

Creation of server

To create the server, use the below PowerShell cmdlet.

$MyresourceGroupName = "newresgroup-$(Get-Random)"
$NewserverName = "myservernew-$(Get-Random)"
$Mylocation = "Eastus"
$NewadminSqlLogin = "SqlAdmin"
$Newpassword = "Password@xyz123"

$Myserver = New-AzSqlServer -ResourceGroupName $MyresourceGroupName `
    -ServerName $NewserverName `
    -Location $Mylocation `
    -SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $NewadminSqlLogin, $(ConvertTo-SecureString -String $Newpassword -AsPlainText -Force))

Check out the image below.

Creation of an Azure SQL database using PowerShell

Creation of Firewall Rule

We can use the below PowerShell cmdlet to create the firewall rule

$MyresourceGroupName = "newresgroup-$(Get-Random)"
$NewserverName = "myservernew-$(Get-Random)"
$NewstartIp = "0.0.0.0"
$NewendIp = "0.0.0.0"

$MyserverFirewallRule = New-AzSqlServerFirewallRule -ResourceGroupName $MyresourceGroupName `
    -ServerName $NewserverName `
    -FirewallRuleName "AllowedIPs" -StartIpAddress $NewstartIp -EndIpAddress $NewendIp

Check out the below image for your reference.

create an azure sql database

Create a blank Database

Create the blank database using the below PowerShell cmdlet

$MyresourceGroupName = "newresgroup-$(Get-Random)"
$NewserverName = "myservernew-$(Get-Random)"
$NewdatabaseName = "mynewdb36845"

$mydatabase = New-AzSqlDatabase  -ResourceGroupName $MyresourceGroupName `
    -ServerName $NewserverName `
    -DatabaseName $NewdatabaseName `
    -RequestedServiceObjectiveName "S0"

See the below image.

powershell create sql database azure

Run the below complete script to create SQL database in Azure using PowerShell.

$MysubscriptionId = '1cdf4300-dee5-4518-9c9c-feaa72a5cbd1'

$MyresourceGroupName = "newresgroup-$(Get-Random)"
$Mylocation = "Eastus"
#Admin login and password for your server
$NewadminSqlLogin = "SqlAdmin"
$Newpassword = "Password@xyz123"

$NewserverName = "myservernew-$(Get-Random)"
$NewdatabaseName = "mynewdb36845"

$NewstartIp = "0.0.0.0"
$NewendIp = "0.0.0.0"
Set-AzContext -SubscriptionId $MysubscriptionId 
$MyresourceGroup = New-AzResourceGroup -Name $MyresourceGroupName -Location $Mylocation
$Myserver = New-AzSqlServer -ResourceGroupName $MyresourceGroupName `
    -ServerName $NewserverName `
    -Location $Mylocation `
    -SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $NewadminSqlLogin, $(ConvertTo-SecureString -String $Newpassword -AsPlainText -Force))
$MyserverFirewallRule = New-AzSqlServerFirewallRule -ResourceGroupName $MyresourceGroupName `
    -ServerName $NewserverName `
    -FirewallRuleName "AllowedIPs" -StartIpAddress $NewstartIp -EndIpAddress $NewendIp
$mydatabase = New-AzSqlDatabase  -ResourceGroupName $MyresourceGroupName `
    -ServerName $NewserverName `
    -DatabaseName $NewdatabaseName `
    -RequestedServiceObjectiveName "S0" 

After executing the above PowerShell script, the Azure SQL database has been created successfully. Check out the below screenshot for your reference.

create azure sql database

Now, after running the above script, you can log in to the Azure portal and check if the SQL database has been created

setup azure sql database

Create Azure SQL database – Video Tutorial

FAQs

Why Create an SQL Database on Azure

Maintaining the on-premise environment is a headache. You can easily create the SQL database in Azure, and there is no headache for the infrastructure.

You can easily move the databases between different machines in the cloud environments.

The main benefits of creating an Azure SQL database are

  • You can use the unlimited storage
  • Quick and easy access
  • Enhanced security features
  • Backups and restores are easy

You may like following Azure tutorials

Conclusion

This Azure SQL database tutorial discussed the quick steps to create Azure SQL database using Azure Portal and PowerShell. Hope you have enjoyed this article !!!