Skip to content

Commit

Permalink
Updates for Azure SQL Database
Browse files Browse the repository at this point in the history
  • Loading branch information
GraemeMalcolm committed Oct 22, 2024
1 parent ca2befa commit 6f0b797
Show file tree
Hide file tree
Showing 17 changed files with 1,482 additions and 1,537 deletions.
17 changes: 16 additions & 1 deletion Instructions/Labs/00-setup.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
lab:
title: 'Lab Environment Setup'
title: 'Lab Environment Setup - Local SQL Server'
module: 'Setup'
---

Expand Down Expand Up @@ -47,3 +47,18 @@ The labs use a lightweight version of the AdventureWorks sample database. Note t
- **Server group**: <Default>
- **Name**: AdventureWorks

## Explore the *AdventureWorks* database

We'll use the **AdventureWorks** database in this lab, so let's start by exploring it in Azure Data Studio.

1. Start Azure Data Studio if it's not already started, and in the **Connections** tab, select the **AdventureWorks** connection by clicking on the arrow just to the left of the name. This will connect to the SQL Server instance and show the objects in the **AdventureWorks** database.
2. Expand the **Tables** folder to see the tables that are defined in the database. Note that there are a few tables in the **dbo** schema, but most of the tables are defined in a schema named **SalesLT**.
3. Expand the **SalesLT.Product** table and then expand its **Columns** folder to see the columns in this table. Each column has a name, a data type, an indication of whether it can contain *null* values, and in some cases an indication that the columns is used as a primary key (PK) or foreign key (FK).
4. Right-click the **SalesLT.Product** table and use the **SELECT TOP (1000)** option to create and run a new query script that retrieves the first 1000 rows from the table.
5. Review the query results, which consist of 1000 rows - each row representing a product that is sold by the fictitious *Adventure Works Cycles* company.
6. Close the **SQLQuery_1** pane that contains the query and its results.
7. Explore the other tables in the database, which contain information about product details, customers, and sales orders. The tables are related through primary and foreign keys, as shown here (you may need to resize the pane to see them clearly):

![An entity relationship diagram of the AdventureWorks database](./images/adventureworks-erd.png)

> **Note**: If you're familiar with the standard **AdventureWorks** sample database, you may notice that in this lab we are using a simplified version that makes it easier to focus on learning Transact-SQL syntax.
74 changes: 74 additions & 0 deletions Instructions/Labs/00a-setup-azure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
lab:
title: 'Lab Environment Setup - Azure SQL Database'
module: 'Setup'
---

# Lab Environment Setup

You can complete the Transact-SQL exercises in a sample database in Microsoft Azure SQL Database. Use the instructions in this page to prepare a suitable Azure SQL Database environment.

> **Note**: You will need a [Microsoft Azure subscription](https://azure.microsoft.com/free) in which you have sufficient permissions to create and configure the required resources.
## Provision Azure SQL Database

First, you need to provision an instance of Azure SQL Database with a sample database that you can query.

1. In a web browser, navigate to the [Azure portal](https://portal.azure.com) at `https://portal.azure.com` and sign in using the credentials associated with your Azure subscription.
1. On the **Home** page, create a **SQL Database** resource with the following settings (be sure to select the **sample** database option on the **Additional settings** tab!):
- **Basics**:
- **Subscription**: *Select your Azure subscription*
- **Resource group**: *Create or select a resource group where you want to create the SQL Database resource*
- **Database name**: `Adventureworks`
- **Server**: Create a new server with the following settings:
- **Server name**: *A unique name*
- **Location**: *Select any available region*
- **Authentication method**: Use Microsoft Entra-only authentication
- **Select Microsoft Entra admin**: *Select your own user account*
- **Want to use SQL elastic pool?**: No
- **Workload environment**: Development
- **Compute + storage**: General purpose - serverless *(with the default configuration)*
- **Backup storage redundancy**: Locally-redundant backup storage
- **Networking**:
- **Connectivity method**: Public endpoint
- **Firewall rules**:
- **Allow Azure services and resources to access this server**: Yes
- **Add current client IP address**: Yes
- **Connection policy**: Default
- **Minimum TLS version**: TLS 1.2
- **Security**:
- **Enable Microsoft Defender for SQL**: Not now
- **Ledger**: Not configured
- **Server identity**: Not configured
- **Transparent data encryption key management**:
- **Server level key**: Service-managed key selected
- **Database level key**: Not configured
- **Enable secure enclaves**: Off
- **Additional settings**:
- **Use existing data**: Sample *(Confirm that **AdventureWorksLT** database will be created)*
- **Tags**:
- None
1. Wait for deployment to complete. Then go to the **Adventureworks** SQL Database resource you deployed.

## Open the query editor

The query editor is a browser-based interface that you can use to run Transact-SQL statements in your database.

1. In the Azure portal, on the page for your **Adventureworks** SQL Database, in the pane on the left, select **Query editor**.
1. On the welcome page, sign into your database using Entra authentication (if necessary, allow access from your client IP address first).
1. In the query editor, expand the **Tables** folder to view the tables in the database.

> **Note**: If you're familiar with the standard **AdventureWorks** sample database for Microsoft SQL Server, you may notice that we are using a simplified, lightweight (*LT*) version with fewer tables.
1. In the **Query 1** pane, enter the following Transact-SQL code:

```sql
SELECT * FROM SalesLT.Product;
```

1. Use the **▷ Run** button to run the query, and and after a few seconds, review the results, which includes all columns for all products.
1. Close the Query editor page, discarding your changes if prompted.

Now that you've created the database and learned how to use the query editor to run Transact-SQL code, you can return to the query editor in the Azure Portal at any time to complete the lab exercises.
> **Tip**: When you've finished with the database, delete the resources you created in your Azure subscription to avoid unnecessary charges.
47 changes: 16 additions & 31 deletions Instructions/Labs/01-get-started-with-tsql.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,54 @@ lab:

# Get Started with Transact-SQL

In this lab, you will use some basic SELECT queries to retrieve data from the **AdventureWorks** database.
In this exercise, you will use some basic SELECT queries to retrieve data from the **AdventureWorks** database.

## Explore the *AdventureWorks* database

We'll use the **AdventureWorks** database in this lab, so let's start by exploring it in Azure Data Studio.

1. Start Azure Data Studio, and in the **Connections** tab, select the **AdventureWorks** connection by clicking on the arrow just to the left of the name. This will connect to the SQL Server instance and show the objects in the **AdventureWorks** database.
2. Expand the **Tables** folder to see the tables that are defined in the database. Note that there are a few tables in the **dbo** schema, but most of the tables are defined in a schema named **SalesLT**.
3. Expand the **SalesLT.Product** table and then expand its **Columns** folder to see the columns in this table. Each column has a name, a data type, an indication of whether it can contain *null* values, and in some cases an indication that the columns is used as a primary key (PK) or foreign key (FK).
4. Right-click the **SalesLT.Product** table and use the **SELECT TOP (1000)** option to create and run a new query script that retrieves the first 1000 rows from the table.
5. Review the query results, which consist of 1000 rows - each row representing a product that is sold by the fictitious *Adventure Works Cycles* company.
6. Close the **SQLQuery_1** pane that contains the query and its results.
7. Explore the other tables in the database, which contain information about product details, customers, and sales orders. The tables are related through primary and foreign keys, as shown here (you may need to resize the pane to see them clearly):

![An entity relationship diagram of the AdventureWorks database](./images/adventureworks-erd.png)

> **Note**: If you're familiar with the standard **AdventureWorks** sample database, you may notice that in this lab we are using a simplified version that makes it easier to focus on learning Transact-SQL syntax.
> **Note**: This exercise assumes you have created the sample **AdventureWorks** database.
## Use SELECT queries to retrieve data

Now that you've had a chance to explore the **AdventureWorks** database, it's time to dig a little deeper into the product data it contains by querying the **Product** table.
The SELECT statement is the primary Transact-SQL statement used to query tables in a database.

1. In Azure Data Studio, create a new query (you can do this from the **File** menu or on the *welcome* page).
2. In the new **SQLQuery_...** pane, ensure that the **AdventureWorks** database is selected at the top of the query pane. If not, use the **Connect** button to connect the query to the **AdventureWorks** saved connection.
3. In the query editor, enter the following code:
1. Open a query editor for your **AdventureWorks** database, and create a new query.
1. In the query editor, enter the following code:

```sql
SELECT * FROM SalesLT.Product;
```

4. Use the **⏵Run** button to run the query, and and after a few seconds, review the results, which includes all columns for all products.
5. In the query editor, modify the query as follows:
1. Run the query, and and after a few seconds, review the results, which includes all columns for all products.
1. In the query editor, modify the query as follows:

```sql
SELECT Name, StandardCost, ListPrice
FROM SalesLT.Product;
```

6. Use the **⏵Run** button to re-run the query, and and after a few seconds, review the results, which this time include only the **Name**, **StandardCost**, and **ListPrice** columns for all products.
7. Modify the query as shown below to include an expression that results in a calculated column, and then re-run the query:
1. Re-run the query, and and after a few seconds, review the results, which this time include only the **Name**, **StandardCost**, and **ListPrice** columns for all products.
1. Modify the query as shown below to include an expression that results in a calculated column, and then re-run the query:

```sql
SELECT Name, ListPrice - StandardCost
FROM SalesLT.Product;
```

8. Note that the results this time include the **Name** column and an unnamed column containing the result of subtracting the **StandardCost** from the **ListPrice**.
9. Modify the query as shown below to assign names to the columns in the results, and then re-run the query.
1. Note that the results this time include the **Name** column and an unnamed numeric column containing the result of subtracting the **StandardCost** from the **ListPrice**.
1. Modify the query as shown below to assign names to the columns in the results, and then re-run the query.

```sql
SELECT Name AS ProductName, ListPrice - StandardCost AS Markup
FROM SalesLT.Product;
```

10. Note that the results now include columns named **ProductName** and **Markup**. The **AS** keyword has been used to assign an *alias* for each column in the results.
11. Replace the existing query with the following code, which also includes an expression that produces a calculated column in the results:
1. Note that the results now include columns named **ProductName** and **Markup**. The **AS** keyword has been used to assign an *alias* for each column in the results.
1. Replace the existing query with the following code, which also includes an expression that produces a calculated column in the results:

```sql
SELECT ProductNumber, Color, Size, Color + ', ' + Size AS ProductDetails
FROM SalesLT.Product;
```

12. Run the query, and note that the **+** operator in the calculated **ProductDetails** column is used to *concatenate* the **Color** and **Size** column values (with a literal comma between them). The behavior of this operator is determined by the data types of the columns - had they been numeric values, the **+** operator would have *added* them. Note also that some results are *NULL* - we'll explore NULL values later in this lab.
1. Run the query, and note that the **+** operator in the calculated **ProductDetails** column is used to *concatenate* the **Color** and **Size** column values (with a literal comma between them). The behavior of this operator is determined by the data types of the columns - had they been numeric values, the **+** operator would have *added* them. Note also that some results are *NULL* - we'll explore NULL values later in this lab.
## Work with data types
Expand Down Expand Up @@ -230,7 +215,7 @@ As you continue to work with the Adventure Works customer data, you must create
- You have been asked to provide a list of all customer companies in the format *Customer ID* : *Company Name* - for example, *78: Preferred Bikes*.
2. Retrieve a list of sales order revisions
- The **SalesLT.SalesOrderHeader** table contains records of sales orders. You have been asked to retrieve data for a report that shows:
- The sales order number and revision number in the format <Order Number> (<Revision>) – for example SO71774 (2).
- The purchase order number and revision number in the format *<Order Number> (<Revision>)* – for example *PO348186287 (2)*.
- The order date converted to ANSI standard *102* format (*yyyy.mm.dd* – for example *2015.01.31*).
### Challenge 3: Retrieve customer contact details
Expand Down Expand Up @@ -299,7 +284,7 @@ This section contains suggested solutions for the challenge queries.
2. Retrieve a list of sales order revisions:
```sql
SELECT SalesOrderNumber + ' (' + STR(RevisionNumber, 1) + ')' AS OrderRevision,
SELECT PurchaseOrderNumber + ' (' + STR(RevisionNumber, 1) + ')' AS OrderRevision,
CONVERT(nvarchar(30), OrderDate, 102) AS OrderDate
FROM SalesLT.SalesOrderHeader;
```
Expand Down
Loading

0 comments on commit 6f0b797

Please sign in to comment.