10985C ENU Companion
10985C ENU Companion
10985C ENU Companion
10985C
Introduction to SQL Databases
ii Introduction to SQL Databases
Information in this document, including URL and other Internet Web site references, is subject to change
without notice. Unless otherwise noted, the example companies, organizations, products, domain names,
e-mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with
any real company, organization, product, domain name, e-mail address, logo, person, place or event is
intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the
user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in
or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical,
photocopying, recording, or otherwise), or for any purpose, without the express written permission of
Microsoft Corporation.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property
rights covering subject matter in this document. Except as expressly provided in any written license
agreement from Microsoft, the furnishing of this document does not give you any license to these
patents, trademarks, copyrights, or other intellectual property.
The names of manufacturers, products, or URLs are provided for informational purposes only and
Microsoft makes no representations and warranties, either expressed, implied, or statutory, regarding
these manufacturers or the use of the products with any Microsoft technologies. The inclusion of a
manufacturer or product does not imply endorsement of Microsoft of the manufacturer or product. Links
may be provided to third party sites. Such sites are not under the control of Microsoft and Microsoft is not
responsible for the contents of any linked site or any link contained in a linked site, or any changes or
updates to such sites. Microsoft is not responsible for webcasting or any other form of transmission
received from any linked site. Microsoft is providing these links to you only as a convenience, and the
inclusion of any link does not imply endorsement of Microsoft of the site or the products contained
therein.
© 2017 Microsoft Corporation. All rights reserved.
Module 1
Introduction to Databases
Contents:
Lesson 1: Introduction to Relational Databases 2
Lesson 1
Introduction to Relational Databases
Contents:
Question and Answers 3
Demonstration: Exploring a Relational Database 3
Introduction to Databases 1-3
( ) To check that the values added to a column are valid by referencing a different column, usually a
column in a different table.
Answer:
(√) To check that the values added to a column are valid by referencing a different column,
usually a column in a different table.
3. In the User Account Control dialog box, click Yes, and wait for setup to complete.
4. Open Microsoft SQL Server Management Studio, and then connect to the MIA-SQL instance of the
database engine by using Windows Authentication.
6. If the Microsoft SQL Server Management Studio dialog box appears asking if you wish to create
support objects for database diagramming, click Yes.
7. In the Add Table dialog box, press and hold down the CTRL key, click Customer (Sales), click
SalesOrderDetail (Sales), click SalesOrderHeader (Sales), click ShipMethod (Purchasing), click
Add, and then click Close.
a. The SalesOrderHeader (Sales) table contains the SalesOrderID, which is the primary key
column.
9. In the SalesOrderDetail (Sales) table, right-click the SalesOrderID column, and then click
Properties.
10. In the Properties window, click the Description field, and then click the ellipsis button (…).
11. In the Description Property dialog box, note that the column is a primary key column, and that
there is a foreign key that references the SalesOrderID column in the SalesOrderHeader column,
and then click Cancel.
12. Click the line between the Customer (Sales) table and the SalesOrderHeader (Sales) table. This line
represents a foreign key relationship.
13. In the Properties window, click Description, and then click the ellipsis button (…).
1-4 Introduction to SQL Databases
14. In the Description Property dialog box, note that the foreign key references the CustomerID
column in the Customer (Sales) table, and then click Cancel.
15. Close the database diagram window, and do not save any changes. Close SQL Server Management
Studio.
Introduction to Databases 1-5
Lesson 2
Other Types of Databases and Storage
Contents:
Question and Answers 6
1-6 Introduction to SQL Databases
Items
2 A broad category of different types of databases that are often used to store semi-
structured and unstructured data, or highly complex data.
Answer:
Lesson 3
Data Analysis
Contents:
Question and Answers 8
1-8 Introduction to SQL Databases
( ) True
( ) False
Answer:
( ) True
(√) False
Introduction to Databases 1-9
Lesson 4
Database Languages in SQL Server
Contents:
Question and Answers 10
Demonstration: Querying a SQL Server Database 10
1-10 Introduction to SQL Databases
( ) SELECT <table>
FROM <column list>
WHERE <filter that limits the rows returned>
Answer:
( ) SELECT <table>
FROM <column list>
WHERE <filter that limits the rows returned>
(√) SELECT <column list>
FROM <table>
WHERE <filter that limits the rows returned>
( ) SELECT <rows>
FROM <table>
WHERE <filter that limits the rows returned>
3. Select the code under the comment Return all rows from all columns in the
Sales.SalesOrderHeader table, and then click Execute.
4. Review the result set, noting the columns that the query returns and, in the bottom right-hand corner
of the results set, the number of rows the query returned.
5. Select the code under the comment Return all rows from the SalesOrderID and OrderDate
columns from the Sales.SalesOrderHeader table, and then click Execute.
6. Review the result set, noting the columns and the number of rows the query returned.
7. Select the code under the comment Return only rows from the SalesOrderID, OrderDate, and
SalesPersonID columns for which the SalespersonID = 282, and then click Execute.
8. Review the result set, noting the columns and the number of rows the query returned. Note that the
number of rows returned is much lower than for the previous two queries.
9. Select the code under the comment Return only rows from the SalesOrderID, OrderDate, and
SalesPersonID columns for which the SalespersonID > 282, and then click Execute.
10. Review the result set, noting the columns and the number of rows the query returned. Note that the
number of rows returned has increased again.
Introduction to Databases 1-11
11. Select the code under the comment Return only rows from the SalesOrderID and OrderDate
columns for which the SalespersonID = 282 and the orderdate is from the year 2013, and then
click Execute.
12. Review the result set, noting the columns and the number of rows the query returned. This query
returned the lowest number of rows because it contained the most restrictive filters.
13. Close the TransactSQLQueries.sql query window and do not save any changes. Close SQL Server
Management Studio.
1-12 Introduction to SQL Databases
Answer: Answers will vary because students’ experiences are likely to be different.
Introduction to Databases 1-13
Answer: The number of rows varied because the WHERE clause in the queries acts as a filter that
limits the rows that the query returns. The WHERE clause in each statement uses different
conditions, therefore different numbers of rows are returned.
Data Modeling 2-1
Module 2
Data Modeling
Contents:
Lesson 1: Data Modeling 2
Lesson 1
Data Modeling
Contents:
Question and Answers 3
Data Modeling 2-3
( ) True
( ) False
Answer:
(√) True
( ) False
2-4 Introduction to SQL Databases
Lesson 2
ANSI-SPARC Database Model
Contents:
Question and Answers 5
Data Modeling 2-5
( ) External level
( ) Conceptual level
( ) Internal level
Answer:
( ) External level
( ) Conceptual level
Lesson 3
Entity Relationship Modeling
Contents:
Question and Answers 7
Resources 7
Data Modeling 2-7
( ) True
( ) False
Answer:
( ) True
(√) False
Resources
Notations
Approach data modeling in a logical manner. Do not try to add detail too early in the process, before you
fully understand what is required in general.
Review Question(s)
Question: How would you approach data modeling?
Answer:
Tools
There are several ER modeling tools that can be used for the type of modeling discussed in this module.
However, for simple models, Microsoft Visio® is sufficiently powerful.
Answer:
Entities represent real-world objects about which information is required to be stored. An entity
can represent a physical object, such as a person, place, or a thing; or it could represent a virtual
object, such as an event.
Answer:
Relationships exist between entities in a data model. They represent how one entity is related to
another at the level of their respective attributes.
In a conceptual model, a relationship can exist between two entities. It is a named, meaningful
link between entities. It represents how the attributes in one entity are related to those in the
other, and can be thought of as a verb phrase that links the two entities to form a sentence. For
example:
STUDENT attends COURSE PRESENTATION.
COMPANY sponsors STUDENT.
COURSE PRESENTATION is one presentation of COURSE TYPE.
Normalization 3-1
Module 3
Normalization
Contents:
Lesson 1: Fundamentals of Normalization 2
Lesson 3: Denormalization 8
Module Review and Takeaways 10
Lesson 1
Fundamentals of Normalization
Contents:
Question and Answers 3
Demonstration: Identifying Candidate Keys 3
Normalization 3-3
( ) Surrogate key
( ) Candidate key
Answer:
( ) Candidate key
2. In the D:\Demofiles\Mod03 folder, right-click Setup.cmd, and then click Run as administrator.
3. In the User Account Control dialog box, click Yes, and wait for setup to complete.
4. Open Microsoft SQL Server Management Studio, and then connect to the MIA-SQL instance of
the database engine by using Windows Authentication.
6. In the Open File dialog box, navigate to the D:\Demofiles\Mod03 folder, click Candidate Keys.sql,
and then click Open.
7. Under the comment View the columns in the Production.Product table and identify the total
number of rows, select the Transact-SQL statement, and then click Execute.
8. Review the results, and in the results pane, in the bottom right corner, note the number of rows that
the query returned.
9. Under the comment Assess ProductID as a candidate key, select the Transact-SQL statement, and
then click Execute.
10. Review the results, and in the results pane, in the bottom right corner, note that the number of rows
that the query returned is the same as for the query in step 7. This means that ProductID is a
candidate key because it contains a unique value for each of the rows in the table.
11. Under the comment Assess Name as a candidate key, select the Transact-SQL statement, and then
click Execute.
12. Review the results, and in the results pane, in the bottom right corner, note that the number of rows
that the query returned is the same as for the query in step 7. This means that Name is a candidate
key because it contains a unique value for each of the rows in the table.
13. Under the comment Assess ProductNumber as a candidate key, select the Transact-SQL statement,
and then click Execute.
14. Review the results, and in the results pane, in the bottom right corner, note that the number of rows
that the query returned is the same as for the query in step 7. This means that ProductNumber is a
candidate key because it contains a unique value for each of the rows in the table.
3-4 Introduction to SQL Databases
15. Under the comment Assess Color as a candidate key, select the Transact-SQL statement, and then
click Execute.
16. Review the results, and in the results pane, in the bottom right corner, note that the number of rows
that the query returned is less than the number of rows that the query in step 7 returned. This means
that Color is not a candidate key because it does not contain a unique value for each of the rows in
the table.
Lesson 2
Normal Form
Contents:
Question and Answers 6
Demonstration: Normalizing to Third Normal Form 7
3-6 Introduction to SQL Databases
Items
Answer:
b. There are multiple copies of the same book titles, and each copy might be a different pressing.
This information is stored in repeating data group columns called Copy 1, Copy 2, and Copy 3.
3. On the 1NF worksheet, note the changes that have been made to bring the table into first normal
form. These changes include:
a. Adding a composite primary key consisting of the Book Title, Copy Number, and Pressing
columns.
6. On the 3NF worksheet, note the change that has been made to ensure that the Author table
conforms to third normal form. The Author Age column has been removed because it was
transitively dependent on the primary key, through the Author Date of Birth non-key column. If
required, the age of each author can be calculated by using the date of birth value, so it not
necessary to store it.
3-8 Introduction to SQL Databases
Lesson 3
Denormalization
Contents:
Question and Answers 9
Normalization 3-9
( ) True
( ) False
Answer:
( ) True
(√) False
3-10 Introduction to SQL Databases
Answer: Answers will vary because students will have access to different data sets, but the basic
process should be the same:
1. Eliminate repeating data groups and ensure that every row is uniquely identifiable.
2. Ensure that all attributes depend on the whole of the primary key.
3. Eliminate transitive dependencies.
Normalization 3-11
Question: In the suggested solution, in the Customers table, are there any other columns that you could
break down even further? If so, why might you do this?
Module 4
Relationships
Contents:
Lesson 1: Introduction to Relationships 2
Lesson 1
Introduction to Relationships
Contents:
Question and Answers 3
Relationships 4-3
( ) A one-to-many relationship.
( ) A one-to-one relationship.
( ) A many-to-many relationship.
( ) A super-type/sub-type relationship.
Answer:
( ) A one-to-many relationship.
( ) A one-to-one relationship.
Lesson 2
Planning Referential Integrity
Contents:
Question and Answers 5
Demonstration: Implementing Referential Integrity by Using a Foreign Key 5
Relationships 4-5
( ) A foreign key constraint was configured with the ON DELETE SET NULL option.
( ) A foreign key constraint was configured with the ON DELETE SET DEFAULT option.
( ) A foreign key constraint was configured with the ON DELETE CASCADE option.
( ) A foreign key constraint was configured with the ON UPDATE CASCADE option.
Answer:
( ) A foreign key constraint was configured with the ON DELETE SET NULL option.
( ) A foreign key constraint was configured with the ON DELETE SET DEFAULT option.
(√) A foreign key constraint was configured with the ON DELETE CASCADE option.
( ) A foreign key constraint was configured with the ON UPDATE CASCADE option.
4. Open Microsoft SQL Server Management Studio and then connect to the MIA-SQL instance of the
database engine by using Windows Authentication.
5. On the File menu, point to Open, click File, browse to D:\Demofiles\Mod04, click Referential
Integrity.sql, and then click Open.
6. In the query window, under the comment Create a database and change database context, select
the Transact-SQL statement and then click Execute.
7. In the query window, under the comment Create Customers table with a primary key constraint
and four rows, review the Transact-SQL statement, select the Transact-SQL statement, and then click
Execute.
8. In the query window, under the comment Create a table with a foreign key constraint, review the
Transact-SQL statement, select the Transact-SQL statement, and then click Execute.
9. In the query window, under the comment Test foreign key constraint by adding an order with a
valid CustomerID value, review the Transact-SQL statement, select the Transact-SQL statement, and
then click Execute.
11. In the query window, under the comment Add an order with an invalid CustomerID value, review
the Transact-SQL statement, select the Transact-SQL statement, and then click Execute.
12. In the Results pane, review the message that states that the insert statement conflicted with the
foreign key constraint.
4-6 Introduction to SQL Databases
13. In the query window, under the comment Update a CustomerID value in Customers that has no
matching value in Orders, review the Transact-SQL statement, select the Transact-SQL statement,
and then click Execute.
17. In the query window, under the comment Drop foreign key and add new foreign key that
specifies cascading referential integrity, review the Transact-SQL statement, select the Transact-
SQL statement, and then click Execute.
18. In the query window, under the comment Test cascading referential integrity, review the Transact-
SQL statement, select the Transact-SQL statement, and then click Execute.
19. In the Results pane, review the results, noting that the row in the Orders table was successfully
updated.
Answer: Answers will vary, depending on students’ knowledge, opinions, and experience.
4-8 Introduction to SQL Databases
Answer: Cascading the delete to the CustomerDetails table removes all customer information for
the deleted customers, which means that it is more difficult to perform data analysis later on,
when it is based on customer demographics. Using the SET NULL option in Orders results in order
rows that are not associated with a specific customer, but instead have a default catch-all
CustomerID value. Removing the context for an order in this way can make it very difficult to
aggregate data in a meaningful manner, which again can reduce the usefulness of the data for
analytics.
One possible solution would be to use triggers to make copies of the data before deleting it from
the tables. This data could then be used for analytical purposes. If it is necessary to eliminate data
that identifies a customer, you could selectively save the non-identifying elements, such as the
city where the customer lives, or their age, and not save the identifying data, such as their name,
phone number, or street address.
Performance 5-1
Module 5
Performance
Contents:
Lesson 1: Indexing 2
Lesson 3: Concurrency 6
Lesson 1
Indexing
Contents:
Demonstration: Testing Index Performance 3
Performance 5-3
2. In the Connect to Server dialog box, in the Server name box, type MIA-SQL. In the Authentication
box, ensure Windows Authentication is selected, and then click Connect.
3. On the File menu, point to Open, click File, navigate to the D:\Demofiles\Mod05\Demo1.sql script
file, and then click Open.
4. To turn statistics on, highlight the code under Step 1 and click Execute.
5. To run a query with no indexes, highlight the code under Step 2 and click Execute.
6. To create indexes, highlight the code under Step 3 and click Execute.
7. To run a query with indexes, highlight the code under Step 4 and click Execute.
8. To drop the indexes, highlight the code under Step 5 and click Execute.
Lesson 2
Query Performance
Contents:
Question and Answers 5
Demonstration: Nonsargable Queries 5
Performance 5-5
( ) True
( ) False
Answer:
(√) True
( ) False
3. On the File menu, point to Open, click File, navigate to the D:\Demofiles\Mod05\Demo2.sql script
file, and then click Open.
4. To turn statistics on, highlight the code under Step 1 and click Execute.
5. To run a query with no indexes, highlight the code under Step 2 and click Execute.
6. To create indexes, highlight the code under Step 3 and click Execute.
7. To run a query with indexes, highlight the code under Step 4 and click Execute.
8. To drop the indexes, highlight the code under Step 5 and click Execute.
Lesson 3
Concurrency
Contents:
Question and Answers 7
Resources 7
Demonstration: Locking and Blocking 7
Performance 5-7
( ) True
( ) False
Answer:
( ) True
(√) False
Resources
Transactions
Additional Reading: For more information, see the Isolation Level and Locking topics later
in this module.
3. On the File menu, point to Open, click File, navigate to the D:\Demofiles\Mod05\Demo3A.sql
script file, and then click Open.
4. On the File menu, point to Open, click File, navigate to the D:\Demofiles\Mod05\Demo3B.sql
script file, and then click Open.
5. To start the first query, switch to the Demo3A tab, highlight the code under Step 1 and click
Execute.
6. Immediately switch to the Demo3B tab, highlight the code under Step 2 and click Execute.
7. Note that the query does not complete because it is being blocked.
8. Switch to the Demo3A tab and, on the toolbar, click Cancel Executing Query.
9. Wait until the cancellation has completed and switch to the Demo3B tab.
10. Notice that the query has now completed because the locks have been released.
11. Close SQL Server Management Studio without saving any changes.
5-8 Introduction to SQL Databases
Module 6
Database Objects
Contents:
Lesson 1: Tables 2
Lesson 2: Views 6
Lesson 1
Tables
Contents:
Question and Answers 3
Demonstration: Creating Databases and Tables 4
Database Objects 6-3
Items
Answer:
3. On the File menu, point to Open, click File, browse to D:\Demofiles\Mod06, click Create
Tables.sql, and then click Open.
4. In the query window, under the comment Create a database and change database context, review
the Transact-SQL statement, select it, and then click Execute.
5. In the query window, under the comment Create schemas, review the Transact-SQL statement, select
it, and then click Execute.
6. In the query window, under the comment Create tables, review the Transact-SQL statement, select it,
and then click Execute.
7. In the query window, under the comment Add CHECK constraint to Sales.Order, review the
Transact-SQL statement, select it, and then click Execute.
8. In the query window, under the comment Test CHECK constraint, review the Transact-SQL
statement, select it, and then click Execute.
9. Review the results, and note that the insert fails because the order date is yesterday's date.
10. In the query window, under the comment Add DEFAULT constraint to
Person.CustomerDemographics, review the Transact-SQL statement, select it, and then click
Execute.
11. In the query window, under the comment Test DEFAULT constraint, review the Transact-SQL
statement, select it, and then click Execute.
12. Review the results, and note that the insert succeeds and the default value ‘Unknown’ is entered.
13. In the query window, under the comment Add UNIQUE constraint to Person.Employee, review the
Transact-SQL statement, select it, and then click Execute.
Database Objects 6-5
14. In the query window, under the comment Test UNIQUE constraint, review the Transact-SQL
statement, select it, and then click Execute.
15. Review the results, and note that the first insert succeeds, but the second fails because it contains the
duplicate SSN value ‘123’.
Lesson 2
Views
Contents:
Question and Answers 7
Demonstration: Creating and Using Views 7
Database Objects 6-7
( ) True
( ) False
Answer:
(√) True
( ) False
2. In the User Account Control dialog box, click Yes, and wait for setup to complete.
3. Open Microsoft SQL Server Management Studio, and then connect to the MIA-SQL instance of
the database engine by using Windows Authentication.
4. On the File menu, point to Open, click File, browse to D:\Demofiles\Mod06, click Create Views.sql,
and then click Open.
5. Under the comment Create a view, review the Transact-SQL statements, select them, and then click
Execute.
6. Under the comment Query the view, review the Transact-SQL statements, select them, and then click
Execute.
7. Under the comment Query the view with a WHERE clause, review the Transact-SQL statements,
select them, and then click Execute.
8. Under the comment Create an index on the view, review the Transact-SQL statements, select them,
and then click Execute.
9. In Object Explorer, expand Databases, expand Demo, expand Views, and note the view that you
created in step 5.
10. Expand dbo.VW_CustomerOrders, expand Indexes, and note the index that you created in step 8.
Lesson 3
Stored Procedures, Triggers, and Functions
Contents:
Question and Answers 9
Demonstration: Creating and Using a Stored Procedure 9
Database Objects 6-9
( ) A stored procedure.
( ) A view.
( ) An indexed view.
Answer:
( ) A view.
( ) An indexed view.
(√) A multistatement user-defined table-valued function.
2. In the User Account Control dialog box, click Yes, and then wait for the script to complete.
3. Open Microsoft SQL Server Management Studio, and then connect to the MIA-SQL instance of
the database engine by using Windows Authentication.
4. On the File menu, point to Open, click File, browse to D:\Demofiles\Mod06, click Create Stored
Procedure.sql, and then click Open.
5. Under the comment Create the stored procedure, review the Transact-SQL statements, select them,
and then click Execute.
6. Under the comment Execute the stored procedure, review the Transact-SQL statement, select it,
and then click Execute.
7. Under the comment View the results, review the Transact-SQL statement, select it, and then click
Execute.
8. Review the results, noting that a row has been added to the Person.Customer table with the values
specified in the EXEC statement.
Answer: You would need to modify the procedure by removing the @OrderID input parameter
and the reference to the OrderID column in the INSERT statement.
Question: You created a stored procedure to insert new rows into the Sales.Orders table. What else would
you need to do to ensure that new orders are recorded in full, including details of the products in each
order, prices, and so on?
Answer: You would need to update the Sales.LineItems table with the appropriate details for
each order. You could extend the existing stored procedure to do this.