Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 *** The “Online” Edition *** Introduction to Data Management Lecture #12 (Relational Languages II) Instructor: Mike Carey [email protected] SQL
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1
*** The “Online” Edition ***
Introduction to Data Management
Lecture #12(Relational Languages II)
Instructor: Mike Carey [email protected]
SQL
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2
Today’s Notices
v HW notes:§ HW #1 now graded (see Gradescope for details)§ HW #3 due 11 PM Friday w/usual 24-hr late window
v Web resource(s) for FDs and normalization:§ http://www.ict.griffith.edu.au/~jw/normalization/ind.php#1NFTo3NF§ http://www.ict.griffith.edu.au/~jw/normalization/ind.php#resources
v Midterm info:§ Relational algebra won’t appear on Midterm 1! (J)§ Old sample exam + solution available on the wiki§ You might (still) consider making a “cheat sheet”§ Piazza geo-poll result: Exam @ 4PM PST MONDAY!
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3
Quick Time Check!
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4
A Few Notes On Honesty...
v Dishonest engineers can severely injure their employers, e.g.,§ Volkswagen’s “benchmark special” ($14.7B!)§ Uber’s self-driving car case ($245M)
v Stanford exams given under an Honor Code§ Faculty can’t even be in the room!
v Be guided by the UCI CS122a Honor Code!§ I am going to trust that you are all mature, grown-up
students who want to learn the material§ What goes around comes around (pretty quickly)§ I am not the police... (Not this quarter for sure!)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5
Divisionv Not a primitive operator, but extremely useful for
expressing queries like: Find sailors who have reserved all boats.
v Let A have 2 fields, x and y, while B has one field y, so we have relations A(x,y) and B(y):§ A/B contains the x tuples (e.g., sailors) such that for every
y tuple (e.g., boat) in B, there is an xy tuple in A.§ Or: If the set of y values (boats) associated with an x value
(sailor) in A contains all y values in B, the x value is in A/B.
v In general, x and y can be any lists of fields; y is the list of fields in B, and x y is the list of fields of A.È
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6
Examples of Division A/B
sno pnos1 p1s1 p2s1 p3s1 p4s2 p1s2 p2s3 p2s4 p2s4 p4
pnop2
pnop2p4
pnop1p2p4
snos1s2s3s4
snos1s4
snos1
A
B1B2
B3
A/B1 A/B2 A/B3
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7
Expressing A/B Using Basic Operators(Advanced Topic – Just FYI J)
v Division not an essential op; just a useful shorthand. (Also true of joins, but joins are so common and important that relational database systems implement joins specially.)
v Idea: For A(x,y)/B(y), compute all x values that are not “disqualified” by some y value in B.§ x value is disqualified if by attaching a y value from B,
we obtain an xy tuple that does not appear in A.
Disqualified x values (D):
A/B:
p px x A B A(( ( ) ) )´ -
p x A( ) - D
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8
Ex: Wisconsin Sailing Club Database
sid sname rating age
22 Dustin 7 45.029 Brutus 1 33.031 Lubber 8 55.532 Andy 8 25.558 Rusty 10 35.064 Horatio 7 35.071 Zorba 10 16.074 Horatio 9 35.085 Art 4 25.595 Bob 3 63.5
sid bid date
22 101 10/10/9822 102 10/10/9822 103 10/8/9822 104 10/7/9831 102 11/10/9831 103 11/6/9831 104 11/12/9864 101 9/5/9864 102 9/8/9874 103 9/8/93
bid bname color
101 Interlake blue102 Interlake red103 Clipper green104 Marine red
Sailors Reserves Boats
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9
Find names of sailors who’ve reserved boat #103
v Solution 1: π sname((σ bid=103Reserves) Sailors)
v Solution 2: r s( , Re )Temp servesbid1 103=
r ( , )Temp Temp Sailors2 1!"
p sname Temp( )2
v Solution 3: π sname(σ bid=103(Reserves▹◃ Sailors))
Sailors(sid, sname, rating, age) Reserves(sid, bid, day)Boats(bid, bname, color)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10
Find names of sailors who’ve reserved boat #103
v Solution 1: π sname((σ bid=103Reserves) Sailors)
v Solution 2: Temp1= σ bid=103Reserves
Temp2= Temp1▹◃ Sailorsp sname Temp( )2
v Solution 3: π sname(σ bid=103(Reserves▹◃ Sailors))
Sailors(sid, sname, rating, age) Reserves(sid, bid, day)Boats(bid, bname, color)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11
Ex: Wisconsin Sailing Club Database
sid bid date
22 103 10/8/9831 103 11/6/9874 103 9/8/93
σ bid=103Reserves
sname rating age
Dustin 7 45.0Lubber 8 55.5Horatio 9 35.0
sid bid date
22 103 10/8/9831 103 11/6/9874 103 9/8/93
(σ bid=103Reserves) Sailors
sname
DustinLubberHoratio
π sname((σ bid=103Reserves) Sailors)
(Solution 1)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12
Find names of sailors who’ve reserved a red boat
v Information about boat color only available in Boats; so need to do another join:
p ssname color red Boats serves Sailors(( ' ' ) Re )=
!" !"
v A more “efficient” solution:
p p p ssname sid bid color red Boats s Sailors( (( ' ' ) Re ) )=
!" !"
A query optimizer will find the latter, given the 1st query!
Sailors(sid, sname, rating, age) Reserves(sid, bid, day)Boats(bid, bname, color)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13
Find sailors who’ve reserved a red or a green boat
v Can identify all red or green boats, then find sailors who’ve reserved one of these boats:r s( , ( ' ' ' ' ))Tempboats color red color green Boats= Ú =
p sname Tempboats serves Sailors( Re )!" !"
v Could also define Tempboats using union! (Q: How?)
v What happens if is replaced by in this query?Ú Ù
Sailors(sid, sname, rating, age) Reserves(sid, bid, day)Boats(bid, bname, color)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14
Find sailors who’ve reserved a red and a green boat
v Previous form won’t work! Must identify sailors who’ve reserved red boats and sailors who’ve reserved green boats, then find their intersection (notice that sid is a key for Sailors!):
r p s( , (( ' ' ) Re ))Tempred sid color red Boats serves=
!"
p sname Tempred Tempgreen Sailors(( ) )Ç !"
r p s( , (( ' ' ) Re ))Tempgreen sid color green Boats serves=
!"
Sailors(sid, sname, rating, age) Reserves(sid, bid, day)Boats(bid, bname, color)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15
The RelaX “Calculator”
https://gist.github.com/idleft/9aa882b7295d64737b8330978c3e0658
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 16
The RelaX “Calculator” (cont.)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 17
Find sailors who’ve reserved a red and a green boat
v Previous approach won’t work! Must identify sailors who’reserved red boats and sailors who’ve reserved green boats, then find their intersection (notice that sid is a key for Sailors!):
r p s( , (( ' ' ) Re ))Tempred sid color red Boats serves=
!"
p sname Tempred Tempgreen Sailors(( ) )Ç !"
r p s( , (( ' ' ) Re ))Tempgreen sid color green Boats serves=
!"
Sailors(sid, sname, rating, age) Reserves(sid, bid, day)Boats(bid, bname, color)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 18
Find the names of sailors who’ve reserved all boats
v Uses division; schemas of the input relations feeding the / operator must be carefully chosen:
ρ (Tempsids, (π sid,bidReserves) / (π bidBoats))
p sname Tempsids Sailors( )!"
v To find sailors who’ve reserved all �Interlake� boats:
/ ( ' ' )p sbid bname Interlake Boats=.....
Sailors(sid, sname, rating, age) Reserves(sid, bid, day)Boats(bid, bname, color)
/
/
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 19
Find the names of sailors who’ve reserved all boats
v Uses division; schemas of the input relations feeding the / operator must be carefully chosen:
r p p( , ( , Re ) / ( ))Tempsids sid bid serves bid Boats
p sname Tempsids Sailors( )!"
v To find sailors who’ve reserved all �Interlake� boats:
/ ( ' ' )p sbid bname Interlake Boats=.....
Sailors(sid, sname, rating, age) Reserves(sid, bid, day)Boats(bid, bname, color)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 20
Find the names of sailors who’ve reserved all boats
v Uses division; schemas of the input relations feeding the / operator must be carefully chosen:
r p p( , ( , Re ) / ( ))Tempsids sid bid serves bid Boats
p sname Tempsids Sailors( )!"
v To find sailors who’ve reserved all �Interlake� boats:
/ ( ' ' )p sbid bname Interlake Boats=.....
Sailors(sid, sname, rating, age) Reserves(sid, bid, day)Boats(bid, bname, color)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 21
PS: RelaX Renaming Example...
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 22
Relational Algebra Summary
v The relational model has (several) rigorously defined query languages that are both simple and powerful in nature.
v Relational algebra is more operational; very useful as an internal representation for queryevaluation plans.
v Several ways of expressing a given query; a query optimizer should choose the most efficient version. (Take CS122C...! J)
v We’ll add a few more operators later on…v Next up for us: Relational Calculus