Introduction to Data Management Lecture #12 (Relational ...€¦ · Introduction to Data Management...

10
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #12 (Relational Languages III) Instructor: Mike Carey [email protected] Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2 Surprise! (L) v These are the slides for our unplanned special episode of “Friday Nights With Databases”. v The good news is that you are now ideally positioned to tackle HW #4, on the Relational Algebra, from start to finish! v Note: There will be NO change in HW #4’s due date, as today’s “oops” didn’t affect the available working time between its release and its deadline.

Transcript of Introduction to Data Management Lecture #12 (Relational ...€¦ · Introduction to Data Management...

Page 1: Introduction to Data Management Lecture #12 (Relational ...€¦ · Introduction to Data Management Lecture #12 (Relational Languages III) Instructor: Mike Carey mjcarey@ics.uci.edu

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Introduction to Data Management

Lecture #12(Relational Languages III)

Instructor: Mike Carey [email protected]

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2

Surprise! (L)

v These are the slides for our unplanned special episode of “Friday Nights With Databases”.

v The good news is that you are now ideally positioned to tackle HW #4, on the Relational Algebra, from start to finish!

v Note: There will be NO change in HW #4’s due date, as today’s “oops” didn’t affect the available working time between its release and its deadline.

Page 2: Introduction to Data Management Lecture #12 (Relational ...€¦ · Introduction to Data Management Lecture #12 (Relational Languages III) Instructor: Mike Carey mjcarey@ics.uci.edu

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3

Joinsv Condition Join:

v Result schema same as that of cross-product.v Fewer tuples than cross-product, so might be

able to compute more efficientlyv Sometimes (often!) called a theta-join.

R c S c R S!" = ´s ( )

(sid) sname rating age (sid) bid day22 dustin 7 45.0 58 103 11/12/9631 lubber 8 55.5 58 103 11/12/96

S1 sid<sid R1

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4

More Joins

v Equi-Join: A special case of condition join where the condition c contains only equalities.

v Result schema similar to cross-product, but only one copy of fields for which equality is specified.

v Natural Join: An equijoin on all commonly named fields.

sid sname rating age bid day22 dustin 7 45.0 101 10/10/9658 rusty 10 35.0 103 11/12/96

S Rsid1 1!"

Page 3: Introduction to Data Management Lecture #12 (Relational ...€¦ · Introduction to Data Management Lecture #12 (Relational Languages III) Instructor: Mike Carey mjcarey@ics.uci.edu

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

Page 4: Introduction to Data Management Lecture #12 (Relational ...€¦ · Introduction to Data Management Lecture #12 (Relational Languages III) Instructor: Mike Carey mjcarey@ics.uci.edu

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7

Expressing A/B Using Basic Operators(Advanced Topic 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/B, 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

Page 5: Introduction to Data Management Lecture #12 (Relational ...€¦ · Introduction to Data Management Lecture #12 (Relational Languages III) Instructor: Mike Carey mjcarey@ics.uci.edu

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)

Page 6: Introduction to Data Management Lecture #12 (Relational ...€¦ · Introduction to Data Management Lecture #12 (Relational Languages III) Instructor: Mike Carey mjcarey@ics.uci.edu

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)

Page 7: Introduction to Data Management Lecture #12 (Relational ...€¦ · Introduction to Data Management Lecture #12 (Relational Languages III) Instructor: Mike Carey mjcarey@ics.uci.edu

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 Can 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 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)

Page 8: Introduction to Data Management Lecture #12 (Relational ...€¦ · Introduction to Data Management Lecture #12 (Relational Languages III) Instructor: Mike Carey mjcarey@ics.uci.edu

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15

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 16

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)

Page 9: Introduction to Data Management Lecture #12 (Relational ...€¦ · Introduction to Data Management Lecture #12 (Relational Languages III) Instructor: Mike Carey mjcarey@ics.uci.edu

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 17

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 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:

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)

Page 10: Introduction to Data Management Lecture #12 (Relational ...€¦ · Introduction to Data Management Lecture #12 (Relational Languages III) Instructor: Mike Carey mjcarey@ics.uci.edu

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 19

PS: RelaX Renaming Example...

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 20

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 query evaluation 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 now: Relational Calculus