Download presentation
Presentation is loading. Please wait.
1
M.P. Johnson, DBMS, Stern/NYU, Spring 2005 1 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…
2
M.P. Johnson, DBMS, Stern/NYU, Spring 2005 2 Complex RA Expressions Person Purchase Person Product name='Dick' name='Gizmo' pid ssn seller-ssn=ssnpid=pidbuyer-ssn=Person.ssn name
3
M.P. Johnson, DBMS, Stern/NYU, Spring 2005 3 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)
4
M.P. Johnson, DBMS, Stern/NYU, Spring 2005 4 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)
5
M.P. Johnson, DBMS, Stern/NYU, Spring 2005 5 Translation to SQL SELECT DISTINCT name buyer FROM (SELECT * FROM Person, (the purchases of gadgets from Dick) P2 WHERE Person.ssn = P2.buyer-ssn) Note: the subquery in this version is being given the name P2 We’re pairing our rows from Person with rows from P2
6
M.P. Johnson, DBMS, Stern/NYU, Spring 2005 6 Translation to SQL SELECT DISTINCT name buyer FROM Person, (the purchases of gadgets from Dick) P2 WHERE Person.ssn = P2.buyer-ssn We simplified by combining the two SELECTs
7
M.P. Johnson, DBMS, Stern/NYU, Spring 2005 7 Translation to SQL 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 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
8
M.P. Johnson, DBMS, Stern/NYU, Spring 2005 8 Translation to SQL 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 Now the subquery to find Dick’s ssn is filled in
9
M.P. Johnson, DBMS, Stern/NYU, Spring 2005 9 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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.