16
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #9 (Relational Query Languages, II) Instructor: Mike Carey [email protected] Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2 Announcements v HW#4 is now underway...! § Today we’ll finish the relevant algebra material § Read ahead w.r.t. the relational calculus § Another lateness discount lies ahead.... (J) v Midterm exam is looming! § Tue, May 9 th , from 12:30-1:50 PM § Here (with pre-assigned seating) § Left-handed? à E-mail Taewoo today! § See course wiki for exam scope (!) v Today’s plan: § RELATIONAL ALGEBRA continued!

Introduction to Data Management Lecture #9 (Relational ... · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #9 (Relational

Embed Size (px)

Citation preview

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

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

Introduction to Data Management

Lecture #9 (Relational Query Languages, II)

Instructor: Mike Carey [email protected]

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

Announcements v  HW#4 is now underway...!

§  Today we’ll finish the relevant algebra material §  Read ahead w.r.t. the relational calculus §  Another lateness discount lies ahead.... (J)

v  Midterm exam is looming! §  Tue, May 9th, from 12:30-1:50 PM §  Here (with pre-assigned seating) §  Left-handed? à E-mail Taewoo today! §  See course wiki for exam scope (!)

v  Today’s plan: §  RELATIONAL ALGEBRA continued!

Page 2: Introduction to Data Management Lecture #9 (Relational ... · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #9 (Relational

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

Reminder: Relational Algebra v  Five basic operations:

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

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, all these operations can be composed! (Algebra is “closed”.)

σ

×∪

π

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

Cross-Product v  Each row of S1 is paired with each row of R1. v  Result schema has one field per field of S1 and R1,

with field names “inherited” if possible. §  Conflict: Both S1 and R1 have a field called sid.

ρ ( ( , ), )C sid sid S R1 1 5 2 1 1→ → ×

(sid) sname rating age (sid) bid day22 dustin 7 45.0 22 101 10/10/9622 dustin 7 45.0 58 103 11/12/9631 lubber 8 55.5 22 101 10/10/9631 lubber 8 55.5 58 103 11/12/9658 rusty 10 35.0 22 101 10/10/9658 rusty 10 35.0 58 103 11/12/96

§  Renaming operator:

Result relation name

Source expression (anything!)

Attribute renaming list

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

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

Renaming v  Conflict: S1 and R1 both had sid fields, giving:

v  Several renaming options available:

ρ (S1R1(1→sid1), S1×R1)

(sid) sname rating age (sid) bid day 22 dustin 7 45.0 22 101 10/10/96 … … … … … … … 58 rusty 10 35.0 58 103 11/12/96

ρ (TempS1(sid→sid1), S1)TempS1×R1

(πsid→sid1,sname,rating,age(S1))×R1

Positional renaming Name-based renaming Generalized projection (I like this notation best! J)

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

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

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

R c S c R 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

Page 4: Introduction to Data Management Lecture #9 (Relational ... · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #9 (Relational

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

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▹◃

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

Division

v  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. ∪

Page 5: Introduction to Data Management Lecture #9 (Relational ... · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #9 (Relational

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

Examples of Division A/B

sno pnos1 p1s1 p2s1 p3s1 p4s2 p1s2 p2s3 p2s4 p2s4 p4

pnop2

pnop2p4

pnop1p2p4

snos1s2s3s4

snos1s4

snos1

A

B1 B2

B3

A/B1 A/B2 A/B3

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

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:

π πx x A B A(( ( ) ) )× −

π x A( ) − D

Page 6: Introduction to Data Management Lecture #9 (Relational ... · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #9 (Relational

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

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 12

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 7: Introduction to Data Management Lecture #9 (Relational ... · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #9 (Relational

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

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 14

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 8: Introduction to Data Management Lecture #9 (Relational ... · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #9 (Relational

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

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 16

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 9: Introduction to Data Management Lecture #9 (Relational ... · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #9 (Relational

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

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

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)

Page 10: Introduction to Data Management Lecture #9 (Relational ... · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #9 (Relational

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:

ρ (Tempsids, (π sid,bidReserves) / (π bidBoats))

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

ρ π π( , ( , 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 11: Introduction to Data Management Lecture #9 (Relational ... · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #9 (Relational

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

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 22

PS: RelaX Renaming Example...

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

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

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 24

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 13: Introduction to Data Management Lecture #9 (Relational ... · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #9 (Relational

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

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 26

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 14: Introduction to Data Management Lecture #9 (Relational ... · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #9 (Relational

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

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 28

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 15: Introduction to Data Management Lecture #9 (Relational ... · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #9 (Relational

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

Find ids of sailors who are older than 30.0 or who have a rating under 8 and are named “Horatio”

{ t(sid) | ∃s ∈ Sailors ( (s.age > 30.0 ∨ (s.rating < 8 ∧ s.sname = “Horatio”)) ∧ 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 30

...

Ex: TRC Query Semantics

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

bid bname color

101 Interlake blue

Sailors

sid sname rating age

22 Dustin 7 45.0

nid nname nvalue

1 Pi 3.14159...

sid sname rating age

58 Rusty 10 35.0

Page 16: Introduction to Data Management Lecture #9 (Relational ... · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to Data Management Lecture #9 (Relational

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

Unsafe Queries and Expressive Power

v  It is possible to write syntactically correct calculus queries that have an infinite number of answers! Such queries are called unsafe. §  E.g., s | s ∉ Sailors

v  It is known that every query that can be expressed in relational algebra can be expressed as a safe query in DRC / TRC; the converse is also true.

v  Relational Completeness: Query language (e.g., SQL) can express every query that is expressible in relational algebra/calculus.

∃∀∈∉¬∧∨⇒≤≥≠

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

To Be Continued...

v  We’ll finish the TRC material on Thursday! v  (Meanwhile, read the book’s TRC sections!)