Presentation is loading. Please wait.

Presentation is loading. Please wait.

M.P. Johnson, DBMS, Stern/NYU, Spring 20051 C20.0046: Database Management Systems Lecture #11 M.P. Johnson Stern School of Business, NYU Spring, 2005.

Similar presentations


Presentation on theme: "M.P. Johnson, DBMS, Stern/NYU, Spring 20051 C20.0046: Database Management Systems Lecture #11 M.P. Johnson Stern School of Business, NYU Spring, 2005."— Presentation transcript:

1 M.P. Johnson, DBMS, Stern/NYU, Spring 20051 C20.0046: Database Management Systems Lecture #11 M.P. Johnson Stern School of Business, NYU Spring, 2005

2 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 2 Last time: Complex RA Expressions Scenario: 1. Purchase(pid, seller-ssn, buyer-ssn, etc.) 2. Person(ssn, name, etc.) 3. Product(pid, name, etc.) Q: Who (give names) bought gizmos from Dick? Where to start? Purchase uses pid, ssn, so must get them…

3 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 3 Complex RA Expressions Person Purchase Person Product  name='Dick'  name='Gizmo'  pid  ssn seller-ssn=ssnpid=pidbuyer-ssn=Person.ssn  name

4 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 4 Translation to SQL We’re converting the tree on the last slide into SQL The result of the query should be the names indicated above One step at a time, we’ll make the query more complete, until we’ve translated the English-language description to an actual SQL query We’ll also simplify the query when possible (the names of the people who bought gadgets from Dick)

5 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 5 Translation to SQL Blue type = actual SQL Black italics = description of subquery Note: the subquery above consists of purchase records, except with the info describing the buyers attached  In the results, the column header for name will be 'buyer' SELECT DISTINCT name buyer FROM (the info, along with buyer names, for purchases of gadgets sold by Dick) SELECT DISTINCT name buyer FROM (the info, along with buyer names, for purchases of gadgets sold by Dick)

6 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 6 Translation to SQL Note: the subquery in this version is being given the name P2 We’re pairing our rows from Person with rows from P2 SELECT DISTINCT name buyer FROM (SELECT * FROM Person, (the purchases of gadgets from Dick) P2 WHERE Person.ssn = P2.buyer-ssn) SELECT DISTINCT name buyer FROM (SELECT * FROM Person, (the purchases of gadgets from Dick) P2 WHERE Person.ssn = P2.buyer-ssn)

7 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 7 Translation to SQL We simplified by combining the two SELECTs SELECT DISTINCT name buyer FROM Person, (the purchases of gadgets from Dick) P2 WHERE Person.ssn = P2.buyer-ssn SELECT DISTINCT name buyer FROM Person, (the purchases of gadgets from Dick) P2 WHERE Person.ssn = P2.buyer-ssn

8 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 8 Translation to SQL P2 is still the name of the subquery It’s just been filled in with a query that contains two subqueries Outer parentheses are bolded for clarity SELECT DISTINCT name buyer FROM Person, (SELECT * FROM Purchases WHERE seller-ssn = (Dick’s ssn) AND pid = (the id of gadget)) P2 WHERE Person.ssn = P2.buyer-ssn SELECT DISTINCT name buyer FROM Person, (SELECT * FROM Purchases WHERE seller-ssn = (Dick’s ssn) AND pid = (the id of gadget)) P2 WHERE Person.ssn = P2.buyer-ssn

9 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 9 Translation to SQL Now the subquery to find Dick’s ssn is filled in SELECT DISTINCT name buyer FROM Person, (SELECT * FROM Purchases WHERE seller-ssn = (SELECT ssn FROM Person WHERE name='Dick') AND pid = (the id of gadget)) P2 WHERE Person.ssn = P2.buyer-ssn SELECT DISTINCT name buyer FROM Person, (SELECT * FROM Purchases WHERE seller-ssn = (SELECT ssn FROM Person WHERE name='Dick') AND pid = (the id of gadget)) P2 WHERE Person.ssn = P2.buyer-ssn

10 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 10 Translation to SQL And now the subquery to find Gadget’s product id is filled in, too Note: the SQL simplified by using subqueries  Not used in relational algebra SELECT DISTINCT name buyer FROM Person, (SELECT * FROM Purchases WHERE seller-ssn = (SELECT ssn FROM Person WHERE name='Dick') AND pid = (SELECT pid FROM Product WHERE name='Gadget')) P2 WHERE Person.ssn = P2.buyer-ssn SELECT DISTINCT name buyer FROM Person, (SELECT * FROM Purchases WHERE seller-ssn = (SELECT ssn FROM Person WHERE name='Dick') AND pid = (SELECT pid FROM Product WHERE name='Gadget')) P2 WHERE Person.ssn = P2.buyer-ssn

11 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 11 New topic: R.A./SQL Set Operators Relations are sets  have set-theoretic ops  Venn diagrams Union: R1  R2 Example:  ActiveEmployees  RetiredEmployees Difference: R1 – R2 Example:  AllEmployees – RetiredEmployees = ActiveEmployees Intersection: R1  R2 Example:  RetiredEmployees  UnionizedEmployees

12 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 12 Set operations - example NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Hamill456 OakM8/8/88 NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Ford345 PalmM7/7/77 R: S: NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Hamill456 OakM8/8/88 Ford345 PalmM7/7/77 R  S:

13 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 13 Set operations - example NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Hamill456 OakM8/8/88 NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Ford345 PalmM7/7/77 R: S: R  S: NameAddressGenderBirthdate Fisher123 MapleF9/9/99

14 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 14 Set operations - example NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Hamill456 OakM8/8/88 NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Ford345 PalmM7/7/77 R: S: R - S: NameAddressGenderBirthdate Hamill456 OakM8/8/88

15 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 15 Set ops in SQL Orthodox SQL has set operators:  UNION, INTERSECT, EXCEPT  Oracle SQL uses MINUS rather than EXCEPT  See the Ullman page on more differencesUllman These ops applied to queries: (SELECT name FROM Person WHERE City = 'New York') INTERSECT (SELECT custname FROM Purchase WHERE store='Kim''s') (SELECT name FROM Person WHERE City = 'New York') INTERSECT (SELECT custname FROM Purchase WHERE store='Kim''s')

16 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 16 Boat examples Reserve(ssn,bmodel,color) Q: Find ssns of sailors who reserved red boats or green boats SELECT DISTINCT ssn FROM reserve WHERE color = 'red' OR color = 'green' SELECT DISTINCT ssn FROM reserve WHERE color = 'red' OR color = 'green'

17 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 17 Boat examples Reserve(ssn,bmodel,color) Q: Find ssns of sailors who reserved red boats and green boats SELECT DISTINCT ssn FROM reserve WHERE color = 'red' AND color = 'green' SELECT DISTINCT ssn FROM reserve WHERE color = 'red' AND color = 'green'

18 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 18 Boat examples Reserve(ssn,bmodel,color) Q: Find ssns of sailors who reserved red boats and green boats SELECT DISTINCT r1.ssn FROM reserve r1, reserve r2 WHERE r1.ssn = r2.ssn AND r1.color = 'red' AND r2.color = 'green' SELECT DISTINCT r1.ssn FROM reserve r1, reserve r2 WHERE r1.ssn = r2.ssn AND r1.color = 'red' AND r2.color = 'green'

19 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 19 Boat examples Reserve(ssn,bmodel,color) Q: Find ssns of sailors who reserved red boats and green boats (SELECT DISTINCT ssn FROM reserve WHERE color = 'red') INTERSECT(SELECT DISTINCT ssn FROM reserve WHERE color = 'green') (SELECT DISTINCT ssn FROM reserve WHERE color = 'red') INTERSECT(SELECT DISTINCT ssn FROM reserve WHERE color = 'green')

20 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 20 Boat examples Reserve(ssn,bmodel,color) Q: Find ssns of sailors who reserved red boats or green boats (SELECT DISTINCT ssn FROM reserve WHERE color = 'red') UNION (SELECT DISTINCT ssn FROM reserve WHERE color = 'green') (SELECT DISTINCT ssn FROM reserve WHERE color = 'red') UNION (SELECT DISTINCT ssn FROM reserve WHERE color = 'green')

21 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 21 Boat examples Reserve(ssn,bmodel,color) Q: Find ssns of sailors who reserved red boats but not green boats (SELECT DISTINCT ssn FROM reserve WHERE color = 'red') EXCEPT (SELECT DISTINCT ssn FROM reserve WHERE color = 'green') (SELECT DISTINCT ssn FROM reserve WHERE color = 'red') EXCEPT (SELECT DISTINCT ssn FROM reserve WHERE color = 'green')

22 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 22 Another set example Acc(name,ssn,balance) Q: Who has the largest balance? First, get two copies of Acc: Acc x  a2 (Acc) Now, consider this:  a2.bal < Acc.bal (Acc x  a2 (Acc)) Q: What does it give us? Now, subtract the names:  name (Acc) -  a2.name (  a2.bal < Acc.bal (Acc x  a2 (Acc)))

23 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 23 (SELECT name, address FROM Cust1) UNION (SELECT name FROM Cust2) (SELECT name, address FROM Cust1) UNION (SELECT name FROM Cust2) Union-Compatibility Situation: Cust1(name,address,…), Cust2(name,…) Want: list of all customer names and addresses (if known) Can’t do: Both tables must have same sequence of types  Applies to all set ops

24 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 24 Union-Compatibility Situation: Cust1(name,address,…), Cust2(name,…) Want: list of all customer names and addresses (if known) But can do: Resulting field names taken from first table (SELECT name, address FROM Cust1) UNION (SELECT name, '(N/A)' FROM Cust2) (SELECT name, address FROM Cust1) UNION (SELECT name, '(N/A)' FROM Cust2) Result(name, address)

25 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 25 First Unintuitive SQLism Looking for R  (S  T) But what happens if T is empty? See transcript of this in Oracle on salestranscript SELECTR.A FROM R, S, T WHERER.A=S.A OR R.A=T.A SELECTR.A FROM R, S, T WHERER.A=S.A OR R.A=T.A

26 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 26 Confession Relations aren’t really sets! They’re bags!


Download ppt "M.P. Johnson, DBMS, Stern/NYU, Spring 20051 C20.0046: Database Management Systems Lecture #11 M.P. Johnson Stern School of Business, NYU Spring, 2005."

Similar presentations


Ads by Google