25
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Embed Size (px)

Citation preview

Page 1: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall1

CHAPTER 7:ADVANCED SQL(PART I)

Modern Database Management11th Edition

Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Page 2: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall2

OBJECTIVES

(Part I in bigger font) Define terms Write single and multiple table SQL

queries Define and use three types of joins Write noncorrelated and correlated subqueries Establish referential integrity in SQL Understand triggers and stored procedures

2

Page 3: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall3

PROCESSING MULTIPLE TABLES–JOINS

Join–a relational operation that causes two or more tables with a common domain to be combined into a single table or view Equi-join–a join in which the joining

condition is based on equality between values in the common columns; common columns appear redundantly in the result table

Natural join–an equi-join in which one of the duplicate columns is eliminated in the result table

The common columns in joined tables are usually the primary key of the dominant table and the foreign key of the dependent table in 1:M relationships

Page 4: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall4

PROCESSING MULTIPLE TABLES–JOINS

Outer join–a join in which rows that do not have matching values

in common columns are nonetheless included in the result table (as opposed to inner join, in which

rows must have matching values in order to appear in the result table)

Union join–includes all columns from each table in the join, and an instance for each row of each table

Illustration using “abstract examples”

Page 5: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall5

FIG 7-2: VISUALIZATION OF DIFFERENT JOIN TYPES WITH RESULTS RETURNED IN SHADED AREA

Right Outer Join

Left/right outer join: watch for the sequence of tables

5

Page 6: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall6

THE FOLLOWING SLIDES CREATE TABLES FOR THIS ENTERPRISE DATA MODEL

(from Chapter 1, Figure 1-3)

6

Page 7: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall7

• Customer_T and Order_T are 1:M• These tables are used in queries that

follow

Figure 7-1 Pine Valley Furniture Company Customer_T and Order_T tables with pointers from customers to their orders

Page 8: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall8

SPECIFYING JOINS IN SQL

Join may be specified in WHERE clause:

FROM clause:

In either case, each contains a column that shares a common domain with the other

There should be one ON (or WHERE) specification for each pair of tables being joined

Page 9: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall

EQUI-JOIN EXAMPLE For each customer who placed an

order, what is the customer’s name and order number?

9

Customer ID appears twice in the result

For the purpose/level of our course, we can treat equi-join and inner join as roughly the same

Those fields that appear in only one

table does not need

table indication

Page 10: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall

EQUI-JOIN (THREE METHODS) SELECT CUSTOMER_T.CUSTOMER_ID,

ORDER_T.CUSTOMER_ID, CUSTOMER_NAME, ORDER_IDFROM CUSTOMER_T, ORDER_TWHERE CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID;

SELECT CUSTOMER_T.CUSTOMER_ID, ORDER_T.CUSTOMER_ID, CUSTOMER_NAME, ORDER_ID FROM CUSTOMER_T, ORDER_TFROM CUSTOMER_T INNER JOIN ORDER_T ONCUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID;

SELECT CUSTOMER_T.CUSTOMER_ID, ORDER_T.CUSTOMER_ID, CUSTOMER_NAME, ORDER_ID FROM CUSTOMER_T, ORDER_TFROM CUSTOMER_T INNER JOIN ORDER_T USINGCUSTOMER_ID; 10

Page 11: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall11

For each customer who placed an order, what is the customer’s name and order number?

Join involves multiple tables in FROM clause

NATURAL JOIN EXAMPLE

ON clause performs the equality check for common columns of the two tables

Note: from Fig. 7-1, you see that only 10 Customers have links with orders

Only 10 rows will be returned from this INNER join

Page 12: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall12

List the customer name, ID number, and order number for all customers. Include customer information even for customers that do have an order.

OUTER JOIN EXAMPLE

LEFT OUTER JOIN clause causes customer data to appear even if there is no corresponding order data

Unlike INNER join, this will include customer rows with no matching order rows

Why Customer data? Why not Order data? (--Fig 7-2)

Page 13: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall13

Results

Unlike INNER join, this will include customer rows with no matching order rows

EVERY row in CUST table, PLUSThose

matching rows

in ORDER

Page 14: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall14

FIGURE 7-2 (PORTION) - LEFT OUTER JOIN: WATCH FOR THE SEQUENCE OF TABLE

CUSTOMER_T is the outer table – ”all records returned from it” even there is no matching: those who did not place an order …

Had we reversed the order the tables were listed, …

Customer_T Order_T

14

Page 15: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall15

FIGURE “7-2” (EXTENDED) - RIGHT OUTER JOIN: WATCH FOR THE SEQUENCE OF TABLE

Customer_TOrder_T RIGHT OUTER JOIN

Also see P. 295, first half

Logically, A LEFT join

B=

B RIGHT join A:

All rows in A, plus

matching rows in B

15

Page 16: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall16

Assemble all information necessary to create an invoice for order number 1006

Four tables involved in this join

MULTIPLE TABLE JOIN EXAMPLE

Each pair of tables requires an equality-check condition in the WHERE clause, matching primary keys against foreign keys

Ref Slide #17

16

Page 17: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall17

MULTIPLE TABLE JOIN EXAMPLE

N tables, N-1 join conditions

Join cond

i-tion

s from Slid

e #16

Page 18: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall18

Figure 7-4 Results from a four-table join (edited for readability)

From CUSTOMER_T table

From ORDER_T table From PRODUCT_T table

Derived from ORDER_T and PRODUCT_T table:OrderQuantity * ProductStandartdPrice

18

Page 19: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall

SELF-JOIN EXAMPLE

19

Self-joins are usually used on tables with unary relationships

Note which one is which one!

19

The same table is used on both

sides of the join (remember the

ERD? – and think about tables); distinguished

using table aliases

Page 20: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall20

PROCESSING MULTIPLE TABLES USING SUBQUERIES

Subquery–placing an inner query (SELECT statement) inside an outer query

Options:1. In a condition of the WHERE clause2. As a “table” of the FROM clause3. Within the HAVING clause4. (all the above because query results …)

Subqueries can be: Noncorrelated–executed once for the

entire outer query Correlated–executed once for each row

returned by the outer query (in Chap 7-Part 2)

Page 21: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall21

What are the name and address of the customer who placed order #1008?

SELECT CUSTOMER_NAME, CUSTOMER_ADDRESS

FROM CUSTOMER_TWHERE CUSTOMER_T.CUSTOMER_ID =

(SELECT CUSTOMER_ID FROM ORDER_T

WHERE ORDER_ID = 1008);

Note: the value for ORDER_ID does NOT appear in the query result; it is used as the selection criterion in the inner query

Data from a subquery cannot be included in the final results

SUBQUERY EXAMPLE

Page 22: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall22

SUBQUERY EXAMPLE (CONT) – JOIN TABLES VS SUBQUERY

Page 23: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall23

SUBQUERY EXAMPLE (CONT) – JOIN TABLES VS SUBQUERY

Page 24: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall24

Show all customers who have placed an order

SELECT CUSTOMER_NAME FROM CUSTOMER_TWHERE CUSTOMER_ID IN

(SELECT DISTINCT CUSTOMER_ID FROM ORDER_T);

SUBQUERY – “IN”

Subquery is embedded in parentheses. In this case it returns a list that will be used in the WHERE clause of the outer query

The IN operator will test to see if the CUSTOMER_ID value of a row is included in the list returned from the subquery

NOT/ANY/ALL may be used in front of IN; logical operators =,<, > can be used

Page 25: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall25

SUBQUERY – “NOT IN”