|
| 1 | +You can create Azure SQL Databases using code or manually through the Azure portal. Additionally, you can integrate database creation into an automated continuous integration (CI) process. This approach is beneficial for maintaining consistency, as writing code to create a database ensures that you're always generating the exact same specifications each time. |
| 2 | + |
| 3 | +When you create an Azure SQL Database using code, there are four main steps: |
| 4 | + |
| 5 | +1. Create a resource group |
| 6 | +1. Create a SQL server |
| 7 | +1. Configure firewall rules |
| 8 | +1. Create a database |
| 9 | + |
| 10 | +## Create a resource group |
| 11 | + |
| 12 | +All resources in Azure are created within a resource group. When creating a resource group with code, you won't have the same visibility that you have when you create a database in the portal. If you need to include resource group creation in your automated deployment, consider using a randomized name. This approach helps prevent any duplication with existing resource group names in your tenant. |
| 13 | + |
| 14 | +> [!NOTE] |
| 15 | +> `$RANDOM` is a Bash function that returns a pseudorandom integer in the range 0 - 32767. This is a smaller range than that returned by PowerShell `Get-Random`. To ensure uniqueness, two `$RANDOM` results can be multiplied together. |
| 16 | +
|
| 17 | +For more information about creating a resource group using code, see [Manage Azure Resource Groups by using Azure CLI](/azure/azure-resource-manager/management/manage-resource-groups-cli?azure-portal=true) or [Manage Azure Resource Groups by using Azure PowerShell](/azure/azure-resource-manager/management/manage-resource-groups-powershell?azure-portal=true). |
| 18 | + |
| 19 | +## Create a SQL server |
| 20 | + |
| 21 | +When you create a SQL server, the server name must also be unique, and an Admin user must be created, with a strong password. |
| 22 | + |
| 23 | +This example uses Azure CLI to create a SQL server: |
| 24 | + |
| 25 | +```azurecli |
| 26 | +serverName="svr-$randomId" |
| 27 | +adminUser="azureadmin" |
| 28 | +adminPassword="pw-$randomId" |
| 29 | +az sql server create --name $serverName --resource-group $resourceGroup --location $location \ |
| 30 | +--admin-user $adminUser --admin-password $adminPassword |
| 31 | +``` |
| 32 | + |
| 33 | +This example uses PowerShell to create a SQL server: |
| 34 | + |
| 35 | +```powershell |
| 36 | +$serverName = "svr-$randomId" |
| 37 | +$adminUser = "azureadmin" |
| 38 | +$adminPassword = "pw-$randomId" |
| 39 | +
|
| 40 | +$credentials = New-Object -TypeName System.Management.Automation.PSCredential ` |
| 41 | +-ArgumentList $adminUser, $(ConvertTo-SecureString -String $adminPassword -AsPlainText -Force) |
| 42 | +
|
| 43 | +$server = New-AzSqlServer -ServerName $serverName -ResourceGroupName $resourceGroup ` |
| 44 | +-Location $location -SqlAdministratorCredentials $credentials |
| 45 | +
|
| 46 | +$server |
| 47 | +``` |
| 48 | + |
| 49 | +## Configure firewall rules |
| 50 | + |
| 51 | +Firewall rules specify which traffic is allowed or denied access to the server. |
| 52 | + |
| 53 | +This example uses Azure CLI to add a firewall rule: |
| 54 | + |
| 55 | +```azurecli |
| 56 | +startIpAddress="0.0.0.0" |
| 57 | +endIpAddress="0.0.0.0" |
| 58 | +firewallRuleName="AllowedIPRange" |
| 59 | +
|
| 60 | +az sql server firewall-rule create --server $serverName \ |
| 61 | +--resource-group $resourceGroup \ |
| 62 | +--name $firewallRuleName --start-ip-address $startIpAddress --end-ip-address $endIpAddress |
| 63 | +``` |
| 64 | + |
| 65 | +This example uses PowerShell to add a firewall rule: |
| 66 | + |
| 67 | +```powershell |
| 68 | +$startIpAddress = "0.0.0.0" |
| 69 | +$endIpAddress = "0.0.0.0" |
| 70 | +$firewallRuleName = "AllowedIPRange" |
| 71 | +
|
| 72 | +$serverFirewallRule = New-AzSqlServerFirewallRule -ServerName $serverName ` |
| 73 | +-ResourceGroupName $resourceGroup ` |
| 74 | +-FirewallRuleName $firewallRuleName -StartIpAddress $startIpAddress -EndIpAddress $endIpAddress |
| 75 | +
|
| 76 | +$serverFirewallRule |
| 77 | +
|
| 78 | +``` |
| 79 | + |
| 80 | +## Create a database |
| 81 | + |
| 82 | +You're now ready to create an Azure SQL Database on the server you created. In this code example, you'll create the sample database *AdventureWorksLT*. If you want to create an empty database, change the database name, and remove the line with the `--sample-name` parameter. |
| 83 | + |
| 84 | +The following code snippet uses Azure CLI to create the *AdventureWorksLT* database. |
| 85 | + |
| 86 | +```azurecli |
| 87 | +az sql db create --name AdventureWorksLT |
| 88 | +\ --resource-group $resourceGroup --server $serverName |
| 89 | +\ --sample-name AdventureWorksLT |
| 90 | +\ --edition GeneralPurpose --compute-model serverless --family Gen5 --capacity 2 |
| 91 | +``` |
| 92 | + |
| 93 | +The following code snippet uses PowerShell to create the *AdventureWorksLT* database. |
| 94 | + |
| 95 | +```powershell |
| 96 | +New-AzSqlDatabase -DatabaseName AdventureWorksLT ` |
| 97 | +-ResourceGroupName $resourceGroup -ServerName $serverName ` |
| 98 | +-SampleName AdventureWorksLT ` |
| 99 | +-Edition GeneralPurpose -ComputeModel Serverless -ComputeGeneration Gen5 -VCore 2 |
| 100 | +
|
| 101 | +``` |
| 102 | + |
| 103 | +The code snippet demonstrates creating an Azure SQL Database for one particular service tier, and compute tier. To get more information about the service and compute tiers available, see [Service tiers](/azure/azure-sql/database/sql-database-paas-overview?azure-portal=true). |
| 104 | + |
| 105 | +## Query the database |
| 106 | + |
| 107 | +You can create an Azure SQL Database using T-SQL if you have a resource group and server already created in Azure. T-SQL doesn't provide the ability to create Azure resources. |
| 108 | + |
| 109 | +You can, however, query an existing database using the **Query editor** in the Azure portal by going to your [Azure SQL dashboard](https://portal.azure.com/#view/HubsExtension/BrowseResource/resourceType/Microsoft.Sql%2Fazuresql?azure-portal=true). |
| 110 | + |
| 111 | +To get started, sign in to the Azure portal and navigate to your database. From the left navigation pane, select **Query editor**. Then, sign in with your SQL administrator credentials. Once you're signed in, run the following T-SQL query against the *AdventureWorksLT* database. |
| 112 | + |
| 113 | +```SQL |
| 114 | +SELECT TOP 10 C.Name AS Category, SUM(S.OrderQty * S.UnitPrice) AS SalesValue |
| 115 | + FROM [SalesLT].[ProductCategory] AS C |
| 116 | + INNER JOIN [SalesLT].[Product] AS P |
| 117 | + ON C.ProductCategoryID = P.ProductCategoryID |
| 118 | + INNER JOIN [SalesLT].[SalesOrderDetail] AS S |
| 119 | + ON P.ProductID = S.ProductID |
| 120 | + GROUP BY C.Name |
| 121 | + ORDER BY SalesValue DESC; |
| 122 | +``` |
| 123 | + |
| 124 | +For more information Azure SQL Database deployment models available, see [What is Azure SQL Database?](/azure/azure-sql/database/sql-database-paas-overview?azure-portal=true). |
0 commit comments