1 / 25

Chapter 7: advanced SQL ( Part i )

Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi. Chapter 7: advanced SQL ( Part i ). Objectives. (Part I in bigger font) Define terms Write single and multiple table SQL queries Define and use three types of joins

reese
Download Presentation

Chapter 7: advanced SQL ( Part i )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Modern Database Management 11th Edition Jeffrey A. Hoffer, V. Ramesh, HeikkiTopi Chapter 7:advanced SQL(Part i)

  2. 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

  3. 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

  4. 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”

  5. Fig 7-2: Visualization of different join types with results returned in shaded area Right Outer Join Left/right outer join: watch for the sequenceof tables

  6. The following slides create tables for this enterprise data model (from Chapter 1, Figure 1-3)

  7. Figure 7-1 Pine Valley Furniture Company Customer_T and Order_T tables with pointers from customers to their orders • Customer_T and Order_T are 1:M • These tables are used in queries that follow

  8. 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) specificationfor each pair of tables being joined

  9. For each customer who placed an order, what is the customer’s name and order number? Equi-Join Example Those fields that appear in only one table does not need table indication 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

  10. 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; Equi-join (three methods)

  11. Join involves multiple tables in FROM clause ON clause performs the equality check for common columns of the two tables Natural Join Example • For each customer who placed an order, what is the customer’s name and order number? 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. Outer Join Example • 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. Results EVERY row in CUST table, PLUS Those matching rows in ORDER Unlike INNER join, this will include customer rows with no matching order rows

  14. Figure 7-2 (portion) - LEFT outer join: watch for the sequenceof table Order_T Customer_T 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, …

  15. Figure “7-2” (extended) - RIGHT outer join: watch for the sequenceof table Logically, A LEFT join B = B RIGHT join A: All rows in A, plus matching rows in B Customer_T Order_T RIGHT OUTER JOIN Also see P. 295, first half

  16. 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 Multiple Table Join Example • Assemble all information necessary to create an invoice for order number 1006 Ref Slide #17   

  17. Multiple Table Join Example Join condi-tions from Slide #16 N tables, N-1 join conditions

  18. 18 From CUSTOMER_T table From ORDER_T table Figure 7-4 Results from a four-table join (edited for readability) From PRODUCT_T table Derived from ORDER_T and PRODUCT_T table: OrderQuantity * ProductStandartdPrice

  19. Self-Join Example The same table is used on both sides of the join (remember the ERD? – and think about tables); distinguished using table aliases Note which one is which one! Self-joins are usually used on tables with unary relationships

  20. Processing Multiple Tables Using Subqueries • Subquery–placing an inner query (SELECT statement) inside an outer query • Options: • In a condition of the WHERE clause • As a “table” of the FROM clause • Within the HAVING clause • (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. Subquery Example • 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. Subquery Example (cont) – Join tables vs subquery

  23. Subquery Example (cont) – Join tables vs subquery

  24. Subquery – “IN” • 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); The IN operator will test to see if the CUSTOMER_ID value of a row is included in the list returned from the subquery Subquery is embedded in parentheses. In this case it returns a list that will be used in the WHERE clause of the outer query NOT/ANY/ALL may be used in front of IN; logical operators =,<, > can be used

  25. Subquery – “NOT IN”

More Related