Documente Academic
Documente Profesional
Documente Cultură
Provision an Azure SQL database to store application data
Exercise - Create your Azure SQL database
Bookmark
Theme
LEVEL 77400/16199 XP
RajnishKumarJha
Previous
Unit 3 of 5
Next
Activate sandbox
Your transportation company wants to set itself apart from other companies, without
breaking the bank. You must have a good handle on how to set up the database to
provide the best service while controlling costs.
Let's take a quick look at some things you need to consider when you create an Azure
SQL database.
For now, you need just one database. But a logical server enables you to add more later
and tune performance among all your databases.
Because your logical server can hold more than one database, there's also the idea
of eDTUs, or elastic Database Transaction Units. This option enables you to choose
one price, but allow each database in the pool to consume fewer or greater
resources depending on current load.
What are vCores?
vCores are Virtual cores, which give you greater control over the compute and
storage resources that you create and pay for.
While the DTU model provides fixed combinations of compute, storage, and IO
resources, the vCore model enables you to configure resources independently. For
example, with the vCore model you can increase storage capacity but keep the
existing amount of compute and IO throughput.
Your transportation and logistics prototype only needs one Azure SQL Database
instance. You decide on the DTU option because it provides a good balance of compute,
storage, and IO performance and is less expensive to get started.
SQL elastic pools relate to eDTUs. They enable you to buy a set of compute and storage
resources that are shared among all the databases in the pool. Each database can use
the resources they need, within the limits you set, depending on current load.
For your prototype, you won't need a SQL elastic pool because you need only one SQL
database.
What is collation?
Collation refers to the rules that sort and compare data. Collation helps you define
sorting rules when case sensitivity, accent marks, and other language characteristics are
important.
Because you don't have specific requirements around how data is sorted and compared,
you choose the default collation.
Over time if you realize you need additional compute power to keep up with demand,
you can adjust performance options or even switch between the DTU and vCore
performance models.
1. Sign into the Azure portal using the same account you activated the sandbox
with.
2. From the Azure portal menu or the Home page, select Create a resource.
Select Databases, then select SQL Database.
3. Use these values to fill out the rest of the form.
Setting Value
Subscription Concierge Subscription
Resource group [sandbox resource group name]
Database name Logistics
Server [See below]
Want to use SQL elastic pool? No
Compute + storage [See below]
4. Under Server, click Create new, fill out the form, then click OK. Here's more
information on how to fill out the form:
Setting Value
Server name A globally unique server name.
Server admin login A database identifier that serves as your primary administrator login name.
Setting Value
Password Any valid password that has at least eight characters and contains characters from
categories: uppercase characters, lowercase characters, numbers, and non-alphanu
Location Any valid location from the available list below.
Allow Azure services This checkbox should be checked.
to access server
5. The free sandbox allows you to create resources in a subset of the Azure global
regions. Select a region from the following list when you create resources:
o West US 2
o South Central US
o Central US
o East US
o West Europe
o Southeast Asia
o Japan East
o Brazil South
o Australia Southeast
o Central India
6. Under Compute + storage, click configure database, then use the following
steps:
Setting Value
Data source None
Database Collation SQL_Latin1_General_CP1_CI_AS
Advanced Data Security Not now
Important
Remember your server name, admin login, and password for later.
When the process completes, click Pin to dashboard to pin your database server to the
dashboard so that you have quick access when you need it later.
For your prototype, you only need to access the database from your laptop. Later, you
can add additional systems, such as your mobile app.
For now, let's enable your development computer to access the database through the
firewall.
1. Go to the overview pane of the Logistics database. If you pinned the database
earlier, you can click the Logistics tile on the dashboard to get there.
2. Click Set server firewall.
3. Click Add client IP, this will automatically add the IP address for your
development computer.
4. Click Save.
In the next unit, you'll get some hands-on practice with your new database and with
Azure Cloud Shell. You'll connect to the database, create a table, add some sample data,
and execute a few SQL statements.
Learn
Browse
Provision an Azure SQL database to store application data
Exercise - Connect to your database and add sample data
Bookmark
Theme
LEVEL 77500/16199 XP
RajnishKumarJha
Previous
Unit 4 of 5
Next
Activate sandbox
Before you connect the database to your app, you want to verify that you can connect
to it, add a basic table, and work with sample data.
We maintain the infrastructure, software updates, and patches for your Azure SQL
database. Beyond that, you can treat your Azure SQL database like you would any other
SQL Server installation. For example, you can use Visual Studio, SQL Server Management
Studio, Azure Data Studio, or other tools to manage your Azure SQL database.
How you access your database and connect it to your app is up to you. But to get some
experience working with your database, here you'll connect to it directly from the portal,
create a table, and run a few basic CRUD operations. You'll learn:
What Cloud Shell is and how to access it from the portal.
How to access information about your database from the Azure CLI, including
connection strings.
How to connect to your database using sqlcmd.
How to initialize your database with a basic table and some sample data.
Behind the scenes, Cloud Shell runs on Linux. But depending on whether you prefer a
Linux or Windows environment, you have two experiences to choose from: Bash and
PowerShell.
Cloud Shell is accessible from anywhere. Besides the portal, you can also access Cloud
Shell from shell.azure.com, the Azure mobile app, or from Visual Studio Code. The panel
on the right is a Cloud Shell terminal for you to use during this exercise.
Cloud Shell includes popular tools and text editors. Here's a brief look at the az, jq,
and sqlcmd utilities, which are three tools that you'll use for this exercise.
az is also known as the Azure CLI. It's the command-line interface for working
with Azure resources. You'll use this to get information about your database,
including the connection string.
jq is a command-line JSON parser. You'll pipe output from az commands to this
tool to extract important fields from JSON output.
sqlcmd enables you to execute statements on SQL Server. You'll use sqlcmd to
create an interactive session with your Azure SQL database.
Tip
When running the T-SQL commands in this module using sqlcmd, the GO on the second
line may not copy through to the sqlcmd prompt, so you will likely need to type this out.
The T-SQL command won't execute without it, so make sure to run the GO command.
1. The az commands you'll run require the name of your resource group and the
name of your Azure SQL logical server. To save typing, run this azure
configure command to specify them as default values.
Azure CLICopy
az configure --defaults group=[sandbox resource group name] sql-server=[server-
name]
Note
Depending on the pane you are on in the portal, your SQL server name may be
displayed as a FQDN (e.g. servername.database.windows.net). However, for this
command, you only need the logical name without the .database.windows.net
suffix.
2. Run az sql db list to list all databases on your Azure SQL logical server.
Azure CLICopy
az sql db list
3. Since we want to see only the database names, run the command a second time.
But this time, pipe the output to jq to print out only the name fields.
Azure CLICopy
az sql db list | jq '[.[] | {name: .name}]'
JSONCopy
[
{
"name": "Logistics"
},
{
"name": "master"
}
]
Logistics is your database. Like SQL Server, master includes server metadata, such
as sign-in accounts and system configuration settings.
Azure CLICopy
az sql db show --name Logistics
5. Run the command a second time. This time, pipe the output to jq to limit output
to only the name, maximum size, and status of the Logistics database.
Azure CLICopy
az sql db show --name Logistics | jq '{name: .name, maxSizeBytes:
.maxSizeBytes, status: .status}'
You see that the database is online and can hold around 2 GB of data.
JSONCopy
{
"name": "Logistics",
"maxSizeBytes": 2147483648,
"status": "Online"
}
Azure CLICopy
az sql db show-connection-string --client sqlcmd --name Logistics
Your output resembles this.
outputCopy
"sqlcmd -S tcp:contoso-1.database.windows.net,1433 -d Logistics -U <username>
-P <password> -N -l 30"
consoleCopy
sqlcmd -S tcp:contoso-1.database.windows.net,1433 -d Logistics -U martina -P
"password1234$" -N -l 30
Tip
Place your password in quotes so that "&" and other special characters aren't
interpreted as processing instructions.
Important
You may see an error message that is similar to the following example:
outputCopy
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server:
Cannot open server 'contoso' requested by the login.
Client with IP address 'nnn.nnn.nnn.nnn' is not allowed to access the server.
To enable access, use the Windows Azure Management Portal or run
sp_set_firewall_rule
on the master database to create a firewall rule for this IP address or address
range.
It may take up to five minutes for this change to take effect.
If this happens, you will need to add another firewall rule for your client. To do so,
use the following steps:
1. Sign into the Azure portal using the same account you activated the
sandbox with.
2. Search for and select your database.
3. Click Set server firewall.
4. Specify a unique Rule name, then enter your IP address from the error
message for both the Start IP and End IP fields.
5. Click Save.
3. From your sqlcmd session, run the following T-SQL statements to create a table
named Drivers.
SQLCopy
CREATE TABLE Drivers (DriverID int, LastName varchar(255), FirstName
varchar(255), OriginCity varchar(255));
GO
The table contains four columns: a unique identifier, the driver's last and first
name, and the driver's city of origin.
Note
SQLCopy
SELECT name FROM sys.tables;
GO
outputCopy
name
-------------------------------------------------------------------------------
-------------------------------------------------
Drivers
(1 rows affected)
5. Run the following T-SQL statements to add a sample row to the table. This is
the create operation.
SQLCopy
INSERT INTO Drivers (DriverID, LastName, FirstName, OriginCity) VALUES (123,
'Zirne', 'Laura', 'Springfield');
GO
outputCopy
(1 rows affected)
SQLCopy
SELECT DriverID, OriginCity FROM Drivers;
GO
You see one result with the DriverID and OriginCity for the row you created in the
previous step.
outputCopy
DriverID OriginCity
----------- --------------------------
123 Springfield
(1 rows affected)
7. Run the following T-SQL statements to change the city of origin from
"Springfield" to "Boston" for the driver with a DriverID of 123. This is
the update operation.
SQLCopy
UPDATE Drivers SET OriginCity='Boston' WHERE DriverID=123;
GO
SQLCopy
SELECT DriverID, OriginCity FROM Drivers;
GO
You should now see the following output. Notice how the OriginCity reflects the
update to Boston.
outputCopy
DriverID OriginCity
----------- --------------------------
123 Boston
(1 rows affected)
9. Run the following T-SQL statements to delete the record. This is
the delete operation.
SQLCopy
DELETE FROM Drivers WHERE DriverID=123;
GO
outputCopy
(1 rows affected)
SQLCopy
SELECT COUNT(*) FROM Drivers;
GO
outputCopy
-----------
0
(1 rows affected)
Now that you have a general idea for working with Azure SQL Database from Cloud
Shell, you can get the connection string for your favorite SQL management tool –
whether that's from SQL Server Management Studio, Visual Studio, or something else.
Cloud Shell makes it easy to access and work with your Azure resources. Because Cloud
Shell is browser-based, you can access it from Windows, macOS, or Linux – essentially
any system with a web browser.
You gained some hands-on experience running Azure CLI commands to get information
about your Azure SQL database. As a bonus, you practiced your T-SQL skills.
In the next unit, we'll wrap up this module and discuss how to tear down your database.
RajnishKumarJha
Previous
Unit 5 of 8
Next
You decide to create an Azure Database for PostgreSQL server to store routes captured
from runners' fitness devices. Based on historic captured data volumes, you know your
server storage requirements should be set at 20 GB. To support your processing
requirements, you need compute Gen 5 support with 1 vCore. You also know that you
require a retention period of 15 days for data backups.
1. Set the name of our new database server into an environment variable so we can
reuse it.
Azure CLICopy
serverName=wingtiptoys-$RANDOM
2. Set the username of the user that is allowed to access the server into an
environment variable so we can reuse it.
Azure CLICopy
userName=azureuser
3. Use the az postgres server create method to create a new database using the
following syntax.
Azure CLICopy
az postgres server create \
--name [unique_server_name] \
--resource-group [resource_group_name] \
--location [your_region] \
--sku-name [sku_name] \
--storage-size [size_in_mb] \
--backup-retention [number_days] \
--version [server_version] \
--admin-user [admin_user_name] \
--admin-password [server_admin_password]
Where:
TABLE 1
Parameter Description
--name Specify a unique server name using lowercase letters 'a'-'z', the
numbers 0-9, and the hyphen.
--resource- Specify your resource group; use [sandbox resource group
group
name] for this exercise.
--location Specify a location from the following list: - westus2 -
southcentralus - centralus - eastus - westeurope - southeastasia -
japaneast - brazilsouth - australiasoutheast - centralindia
--admin-user Specify the admin username for your server.
--admin- Specify the admin password for your server.
password
--sku-name For this exercise, specify B_Gen5_1 for pricing tier B, generation 5
hardware, and 1 vCore.
--storage- Specify the storage capacity of the server in megabytes.
size
--backup- Specify the number of days a backup is retained.
retention
--version Specify the major version of the server.
For example:
Azure CLICopy
az postgres server create \
--name $serverName \
--resource-group [sandbox resource group name] \
--location centralus \
--sku-name B_Gen5_1 \
--storage-size 20480 \
--backup-retention 15 \
--version 10 \
--admin-user $userName \
--admin-password "P@ssw0rd"
4. The system will take a few minutes to process the information when executed. Go
ahead and wait for the command to complete.
5. Once the command has completed, a JavaScript Object Notation (JSON) string
that describes the server is returned. If there was a failure, an error message is
displayed. You can use this error information to review and fix your command
parameters and try again.
The JSON object will look something like:
JSONCopy
{
"administratorLogin": "azureuser",
"earliestRestoreDate": "2018-09-17T00:35:50.170000+00:00",
"fullyQualifiedDomainName": "wingtiptoys.postgres.database.azure.com",
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/
[sandbox Resource
Group]/providers/Microsoft.DBforPostgreSQL/servers/wingtiptoys",
"location": "centralus",
"name": "wingtiptoys-16764",
"resourceGroup": "[sandbox Resource Group]",
"sku": {
"capacity": 1,
"family": "Gen5",
"name": "B_Gen5_1",
"size": null,
"tier": "Basic"
},
"sslEnforcement": "Enabled",
"storageProfile": {
"backupRetentionDays": 15,
"geoRedundantBackup": "Disabled",
"storageMb": 20480
},
"tags": null,
"type": "Microsoft.DBforPostgreSQL/servers",
"userVisibleState": "Ready",
"version": "10"
}
You've successfully created a PostgreSQL server using the Azure CLI. In the next unit,
you'll see how to configure your server's security settings.