12
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #12 (Relational Algebra II) Instructor: Mike Carey [email protected] Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2 Announcements v HW and exams: § HW #4 info • Came out yesterday a little before 6PM • As always, due one week later (to be on time) § Midterm Exam 1 • Over! (And hopefully easier than you expected) • Grading is commencing – goal is 2-week turnaround • It doesn’t “matter” now, in a “material” sense! (Reset!) v Today’s plan: § Relational query languages, Episode 2 § Relational calculus, Episode 1 (if time)

Introduction to Data Management Lecture #12 … ·  · 2017-02-09Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #12 (Relational

Embed Size (px)

Citation preview

Page 1: Introduction to Data Management Lecture #12 … ·  · 2017-02-09Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #12 (Relational

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

Introduction to Data Management

Lecture #12 (Relational Algebra II)

Instructor: Mike Carey [email protected]

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

Announcements v  HW and exams:

§  HW #4 info • Came out yesterday a little before 6PM • As always, due one week later (to be on time)

§  Midterm Exam 1 • Over! (And hopefully easier than you expected) • Grading is commencing – goal is 2-week turnaround •  It doesn’t “matter” now, in a “material” sense! (Reset!)

v  Today’s plan: §  Relational query languages, Episode 2 §  Relational calculus, Episode 1 (if time)

Page 2: Introduction to Data Management Lecture #12 … ·  · 2017-02-09Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #12 (Relational

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

(Cache Reload J) Relational Query Languages v  Two mathematical Query Languages form

the basis for “real” languages (e.g., SQL), and for their implementation: §  Relational Algebra: More operational, very useful

for representing execution plans. §  Relational Calculus: Lets users describe what they

want, rather than how to compute it. (Non-operational, declarative.)

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

(Cache Reload J) Relational Algebra

v  Basic operations: §  Selection ( ) Selects a subset of rows from relation. §  Projection ( ) Omits unwanted columns from relation. §  Cross-product ( ) Allows us to combine two relations. §  Set-difference ( ) Tuples in reln. 1, but not in reln. 2. §  Union ( ) Tuples in reln. 1 and in reln. 2.

v  Additional operations: §  Intersection, join, division, renaming: Not essential, but

(very!) useful. (I.e., don’t add expressive power, but…)

v  Since each operation returns a relation, operations can be composed! (Algebra is “closed”.)

σ

−×

π

Page 3: Introduction to Data Management Lecture #12 … ·  · 2017-02-09Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #12 (Relational

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

Ex: Wisconsin Sailing Club Database

sid sname rating age

22 Dustin 7 45.0

29 Brutus 1 33.0

31 Lubber 8 55.5

32 Andy 8 25.5

58 Rusty 10 35.0

64 Horatio 7 35.0

71 Zorba 10 16.0

74 Horatio 9 35.0

85 Art 4 25.5

95 Bob 3 63.5

sid bid date

22 101 10/10/98

22 102 10/10/98

22 103 10/8/98

22 104 10/7/98

31 102 11/10/98

31 103 11/6/98

31 104 11/12/98

64 101 9/5/98

64 102 9/8/98

74 103 9/8/93

bid bname color

101 Interlake blue

102 Interlake red

103 Clipper green

104 Marine red

Sailors Reserves Boats

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

Find names of sailors who’ve reserved boat #103

v  Solution 1: π sname((σ bid=103Reserves) Sailors)

v  Solution 2: ρ σ( , Re )Temp servesbid1 103=

ρ ( , )Temp Temp Sailors2 1▹◃

π 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 4: Introduction to Data Management Lecture #12 … ·  · 2017-02-09Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #12 (Relational

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

Find names of sailors who’ve reserved boat #103

v  Solution 1: π sname((σ bid=103Reserves) Sailors)

v  Solution 2: Temp1= σ bid=103Reserves

Temp2= Temp1▹◃ Sailorsπ 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 8

Ex: Wisconsin Sailing Club Database

sid bid date

22 103 10/8/98

31 103 11/6/98

74 103 9/8/93

σ bid=103Reserves

sname rating age

Dustin 7 45.0

Lubber 8 55.5

Horatio 9 35.0

sid bid date

22 103 10/8/98

31 103 11/6/98

74 103 9/8/93

(σ bid=103Reserves) Sailors

sname

Dustin

Lubber

Horatio

π sname((σ bid=103Reserves) Sailors)

(Solution 1)

Page 5: Introduction to Data Management Lecture #12 … ·  · 2017-02-09Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #12 (Relational

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

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:

π σsname color red Boats serves Sailors(( ' ' ) Re )=

▹◃ ▹◃

v  A more “efficient” solution:

π π π σsname 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 10

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:

ρ σ( , ( ' ' ' ' ))Tempboats color red color green Boats= ∨ =

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

Page 6: Introduction to Data Management Lecture #12 … ·  · 2017-02-09Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #12 (Relational

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

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

ρ π σ( , (( ' ' ) Re ))Tempred sid color red Boats serves=

▹◃

π sname Tempred Tempgreen Sailors(( ) )∩ ▹◃

ρ π σ( , (( ' ' ) 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 12

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:

ρ π π( , ( , Re ) / ( ))Tempsids sid bid serves bid Boats

π sname Tempsids Sailors( )▹◃

v  To find sailors who’ve reserved all ‘Interlake’ boats:

/ ( ' ' )π σbid bname Interlake Boats=.....

Sailors(sid, sname, rating, age) Reserves(sid, bid, day) Boats(bid, bname, color)

Page 7: Introduction to Data Management Lecture #12 … ·  · 2017-02-09Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #12 (Relational

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

(Cache Reload) Examples of Division A/B

sno pnos1 p1s1 p2s1 p3s1 p4s2 p1s2 p2s3 p2s4 p2s4 p4

pnop2

pnop2p4

pnop1p2p4

snos1s2s3s4

snos1s4

snos1

R

B1 B2

B3

A/B1 A/B2 A/B3

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

Examples of Division R/B sid bid s1 b1 s1 b2 s1 b3 s1 b4 s2 b1 s2 b2 s3 b2 s4 b2 s4 b4

bid b2

bid b2 b4

bid b1 b2 b4

sid s1 s2 s3 s4

sid s1 s4

sid s1

R

B1 B2

B3

R/B1 R/B2 R/B3

Page 8: Introduction to Data Management Lecture #12 … ·  · 2017-02-09Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #12 (Relational

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

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:

ρ π π( , ( , Re ) / ( ))Tempsids sid bid serves bid Boats

π sname Tempsids Sailors( )▹◃

v  To find sailors who’ve reserved all ‘Interlake’ boats:

/ ( ' ' )π σbid 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 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:

ρ π π( , ( , Re ) / ( ))Tempsids sid bid serves bid Boats

π sname Tempsids Sailors( )▹◃

v  To find sailors who’ve reserved all ‘Interlake’ boats:

/ ( ' ' )π σbid bname Interlake Boats=.....

Sailors(sid, sname, rating, age) Reserves(sid, bid, day) Boats(bid, bname, color)

Page 9: Introduction to Data Management Lecture #12 … ·  · 2017-02-09Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #12 (Relational

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

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

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

Relational Calculus

v  Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC).

v  Calculus has variables, constants, comparison ops, logical connectives and quantifiers. §  TRC: Variables range over (i.e., get bound to) tuples. §  DRC: Variables range over domain elements (= field values). §  Both TRC and DRC are simple subsets of first-order logic.

v  Expressions in the calculus are called formulas. An answer tuple is essentially an assignment of constants to variables that make the formula evaluate to true.

v  TRC is the basis for various query languages (Quel, SQL, OQL, XQuery, …), while DRC is the basis for example-based relational query UIs. We’ll study TRC!

Page 10: Introduction to Data Management Lecture #12 … ·  · 2017-02-09Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #12 (Relational

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

Tuple Relational Calculus v  Query in TRC has the form:

{ t(attrlist) | P(t) }

v  Answer includes all tuples t with (optionally) specified schema (attrlist) that cause formula P(t) to be true.

v  Formula is recursively defined, starting with simple atomic formulas (getting tuples from relations or making comparisons of values), and building up bigger and better Boolean formulas using logical connectives.

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

TRC Formulas v  Atomic formula:

§  r ∈ R, or r ∉ R , or r.a op s.b, or r.a op constant §  op is one of <, >, ≤, ≥, ≠,=

v  Formula: §  an atomic formula, or §  ¬P, P∧Q, P∨Q, where P and Q are formulas, or §  ∃r ∈ R (P(r)), where variable r is free in P(…), or §  ∀r ∈ R (P(r)), where variable r is free in P(…), or §  P ⇒ Q (pronounced “implies”, equivalent to (¬P) ∨ Q))

∃∀∈∉¬∧∨⇒≤≥≠

Page 11: Introduction to Data Management Lecture #12 … ·  · 2017-02-09Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #12 (Relational

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

Free and Bound Variables

v  The use of a quantifier such as ∃t ∈ T or ∀t ∈ T in a formula is said to bind t. §  A variable that is not bound is free.

v  Now let us revisit the definition of a TRC query: §  { t(a1, a2, …) | P(t) }

v  There is an important restriction: the variable t that appears to the left of the | (“such that”) symbol must be the only free variable in the formula P(...).

v  Let’s look at some examples!

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

Find sailors with a rating above 7

{ s | s ∈ Sailors ∧ s.rating > 7 }

v  This is equivalent to the more general form: { t(id, nm, rtg, age) | ∃s ∈ Sailors

( t.id = s.sid ∧ t.nm = s.sname ∧ t.rtg=s.rating ∧ t.age = s.age ∧ s.rating > 7 ) } (Q: See how each one specifies the answer’s schema and values...? Note that the second one’s schema is different, as we’ve specified it.)

Sailors(sid, sname, rating, age) Reserves(sid, bid, day) Boats(bid, bname, color)

Page 12: Introduction to Data Management Lecture #12 … ·  · 2017-02-09Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #12 (Relational

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

Find ids of sailors who are older than 18 or who have a rating under 9 and are named “Joe”

{ t(sid) | ∃s ∈ Sailors ( (s.age > 18

∨ (s.rating < 9 ∧ s.sname = “Joe”)) ∧ t.sid = s.sid ) } v  Things to notice:

§  Again, how result schema and values are specified §  Use of Boolean formula to specify the query constraints §  Highly declarative nature of this form of query language!

Sailors(sid, sname, rating, age) Reserves(sid, bid, day) Boats(bid, bname, color)

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

To Be Continued!

v  A few more TRC query examples next time v  Then we’ll be on to SQL!