Beruflich Dokumente
Kultur Dokumente
Advanced SQL
Modern Database
Management
8th Edition
Jeffrey A. Hoffer, Mary B. Prescott,
Fred R. McFadden
2007 by Prentice Hall
Objectives
Definition of terms
Write multiple table SQL queries
Define and use three types of joins
Write correlated and noncorrelated
subqueries
Establish referential integrity in SQL
Understand triggers and stored procedures
Discuss SQL:1999 standard and its
extension of SQL-92
Chapter 8
Chapter 8
Results
Unlike
INNER
join, this
will
include
customer
rows with
no
matching
order
rows
Chapter 8
Chapter 8
ORDER_T.ORDER_ID =
10
Chapter 8
11
Chapter 8
12
Subquery Example
SELECT CUSTOMER_NAME
WHERE CUSTOMER_ID IN
(SELECT DISTINCT CUSTOMER_ID FROM ORDER_T);
Subquery is embedded in
parentheses. In this case it
returns a list that will be used
in the WHERE clause of the
outer query
Chapter 8
13
SELECT CUSTOMER_NAME
FROM CUSTOMER_T, ORDER_T
WHERE CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID;
Chapter 8
14
Chapter 8
15
Chapter 8
16
Correlated vs.
Noncorrelated Subqueries
Noncorrelated subqueries:
Correlated subqueries:
Chapter 8
17
Figure 8-3a
Processing a
noncorrelated
subquery
1.
The subquery
executes and
returns the
customer IDs
from the
ORDER_T table
2.
Chapter 8
No reference to data
in outer query, so
subquery executes
once only
18
Correlated Subquery
Example
19
Chapter 8
20
PRODUCT_FINISH
UNIT_PRICE
Dining Table
Natural Ash
800
Chapter 8
21
Unit_Price
AvgPrice
Entertainment Center
650
440.625
8-Drawer Dresser
750
440.625
Dining Table
800
440.625
Chapter 8
22
UNION Queries
The UNION clause is used to combine the output from multiple
queries together into a single result table.
To use the UNION clause, each query involved must output the
same number of columns, and they must be UNIONcompatible. This means that the output from each query for
each column should be of compatible data types.
The example in the next slide determines the customer(s) who
has purchased the largest quantity of any Pine Valley
product, and the customer(s) who has purchased the smallest
quantity, and returns the results in one table.
Chapter 8
23
Union Queries
First query
Combine
Second query
Chapter 8
24
Ordered_Quantity Quantity
Contemporary Casuals
Smallest Quantity
Value Furniture
Smallest Quantity
Contemporary Casuals
10
Largest Quantity
Chapter 8
25
Ensuring Transaction
Integrity
COMMIT
ROLLBACK
Chapter 8
26
Chapter 8
27
Triggers
Routines
Chapter 8
28
29
Chapter 8
30
Chapter 8
31
32
33
34
35
Chapter 8
36
Embedded SQL
Dynamic SQL
Chapter 8
37