Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 2 (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

3 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 3 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

4 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 4 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

5 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 5 Right Outer Join Left/right outer join: watch for the sequence of tables 5

6 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 6 (from Chapter 1, Figure 1-3) 6

7 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 7 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

8 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 8 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

9 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall For each customer who placed an order, what is the customers 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

10 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SELECT CUSTOMER_T.CUSTOMER_ID, ORDER_T.CUSTOMER_ID, CUSTOMER_NAME, ORDER_ID FROM CUSTOMER_T, ORDER_T WHERE 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_T FROM CUSTOMER_T INNER JOIN ORDER_T ON 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_T FROM CUSTOMER_T INNER JOIN ORDER_T USING CUSTOMER_ID; 10

11 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 11 For each customer who placed an order, what is the customers name and order number? Join involves multiple tables in FROM clause 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

12 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 12 List the customer name, ID number, and order number for all customers. Include customer information even for customers that do have an order. 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)

13 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 13 Results Unlike INNER join, this will include customer rows with no matching order rows EVERY row in CUST table, PLUS Those matching rows in ORDER

14 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 14 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

15 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 15 Customer_T Order_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

16 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 16 Assemble all information necessary to create an invoice for order number 1006 Four tables involved in this join Each pair of tables requires an equality-check condition in the WHERE clause, matching primary keys against foreign keys Ref Slide #17 16

17 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 17 N tables, N-1 join conditions Join condi- tions from Slide #16

18 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 18 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

19 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 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

20 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 20 Subquery–placing an inner query (SELECT statement) inside an outer query Options: 1. In a condition of the WHERE clause 2. As a table of the FROM clause 3. Within the HAVING clause 4. (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)

21 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 21 What are the name and address of the customer who placed order #1008? SELECT CUSTOMER_NAME, CUSTOMER_ADDRESS FROM CUSTOMER_T WHERE 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

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

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

24 Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 24 Show all customers who have placed an order SELECT CUSTOMER_NAME FROM CUSTOMER_T 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 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

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


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

Similar presentations


Ads by Google