26
Faloutsos - Pavlo CMU - 15-415/615 1 CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 – DB Applications C. Faloutsos & A. Pavlo Lecture#5: Relational calculus CMU SCS Faloutsos - Pavlo CMU SCS 15-415/615 #2 General Overview - rel. model • history • concepts Formal query languages – relational algebra rel. tuple calculus – rel. domain calculus CMU SCS Faloutsos - Pavlo CMU SCS 15-415/615 #3 Overview - detailed rel. tuple calculus – why? – details – examples – equivalence with rel. algebra – more examples; safetyof expressions rel. domain calculus + QBE CMU SCS Faloutsos - Pavlo CMU SCS 15-415/615 #4 Motivation Q: weakness of rel. algebra? A: procedural – describes the steps (ie., how) – (still useful, for query optimization)

General Overview - rel. model Dept. of Computer Science 15

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Faloutsos - Pavlo CMU - 15-415/615

1

CMU SCS

Carnegie Mellon Univ. Dept. of Computer Science

15-415/615 – DB Applications

C. Faloutsos & A. Pavlo Lecture#5: Relational calculus

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #2

General Overview - rel. model

•  history •  concepts •  Formal query languages

–  relational algebra –  rel. tuple calculus –  rel. domain calculus

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #3

Overview - detailed

•  rel. tuple calculus – why? –  details –  examples –  equivalence with rel. algebra – more examples; ‘safety’ of expressions

•  rel. domain calculus + QBE

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #4

Motivation

•  Q: weakness of rel. algebra? •  A: procedural

–  describes the steps (ie., ‘how’) –  (still useful, for query optimization)

Faloutsos - Pavlo CMU - 15-415/615

2

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #5

Solution: rel. calculus

–  describes what we want –  two equivalent flavors: ‘tuple’ and ‘domain’

calculus –  basis for SQL and QBE, resp. – Useful for proofs (see query optimization, later)

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #6

Rel. tuple calculus (RTC)

•  first order logic

)}(|{ tPt

}|{ STUDENTtt ∈

‘Give me tuples ‘t’, satisfying predicate P - eg:

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #7

Details

•  symbols allowed:

•  quantifiers

≥≤≠=<>

⇒¬∨∧

),(,,,,,,,

,,,

∃∀,

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #8

Specifically

•  Atom

'...

attrsattrtconstattrt

TABLEt

≤≥

≤≥

Faloutsos - Pavlo CMU - 15-415/615

3

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #9

Specifically

•  Formula: –  atom –  if P1, P2 are formulas, so are –  if P(s) is a formula, so are

...21;21 PPPP ∨∧

))(())((sPssPs

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #10

Specifically

•  Reminders: – DeMorgan –  implication: –  double negation:

))(())(( sPTABLEssPTABLEs ¬∈¬∃≡∈∀

)21(21 PPPP ¬∨¬¬≡∧

2121 PPPP ∨¬≡⇒

‘every human is mortal : no human is immortal’

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #11

Reminder: our Mini-U db

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

CLASSc-id c-name units15-413 s.e. 215-412 o.s. 2

TAKESSSN c-id grade

123 15-413 A234 15-413 B

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #12

Examples

•  find all student records

}|{ STUDENTtt ∈

output tuple of type ‘STUDENT’

Faloutsos - Pavlo CMU - 15-415/615

4

CMU SCS

(Goal: evidence that RTC = RA)

•  Full proof: complicated •  We’ll just show examples of the 5 RA

fundamental operators, and how RTC can handle them

•  (Quiz: which are the 5 fundamental op’s?)

Faloutsos - Pavlo CMU SCS 15-415/615 #13

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #14

•  selection •  projection •  cartesian product MALE x FEMALE •  set union •  set difference R - S

FUNDAMENTAL Relational operators

)(Rconditionσ)(Rlistatt−π

R U S

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #15

Examples

•  (selection) find student record with ssn=123

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #16

Examples

•  (selection) find student record with ssn=123

}123.|{ =∧∈ ssntSTUDENTtt

•  σ selection •  π projection •  X cartesian product •  U set union •  - set difference

Faloutsos - Pavlo CMU - 15-415/615

5

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #17

Examples

•  (projection) find name of student with ssn=123

}123.|{ =∧∈ ssntSTUDENTtt

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #18

Examples

•  (projection) find name of student with ssn=123

)}..123.(|{

namesnametssnsSTUDENTst

=

∧=∈∃

‘t’ has only one column

•  σ selection •  π projection •  X cartesian product •  U set union •  - set difference

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #19

‘Tracing’

)}..123.(|{

namesnametssnsSTUDENTst

=

∧=∈∃

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

s

Nameaaaa….jones…zzzz

t

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #20

Examples cont’d

•  (union) get records of both PT and FT students

•  σ selection •  π projection •  X cartesian product •  U set union •  - set difference

Faloutsos - Pavlo CMU - 15-415/615

6

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #21

Examples cont’d

•  (union) get records of both PT and FT students

}__|{STUDENTPTtSTUDENTFTtt

∨∈

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #22

Examples

•  difference: find students that are not staff

(assuming that STUDENT and STAFF are union-compatible)

•  σ selection •  π projection •  X cartesian product •  U set union •  - set difference

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #23

Examples

•  difference: find students that are not staff

}|{STAFFtSTUDENTtt

∧∈

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #24

Cartesian product

•  eg., dog-breeding: MALE x FEMALE •  gives all possible couples

MALEnamespikespot

FEMALEnamelassieshiba

x = M.name F.namespike lassiespike shibaspot lassiespot shiba

Faloutsos - Pavlo CMU - 15-415/615

7

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #25

Cartesian product

•  find all the pairs of (male, female)

}....

|{

namefnameftnamemnamemt

FEMALEfMALEmt

=−

∧=−

∈∃

∧∈∃

•  σ selection •  π projection •  X cartesian product •  U set union •  - set difference

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #26

‘Proof’ of equivalence

•  rel. algebra <-> rel. tuple calculus

•  σ selection •  π projection •  X cartesian product •  U set union •  - set difference

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #27

Overview - detailed

•  rel. tuple calculus – why? –  details –  examples –  equivalence with rel. algebra – more examples; ‘safety’ of expressions

•  re. domain calculus + QBE

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #28

More examples

•  join: find names of students taking 15-415

Faloutsos - Pavlo CMU - 15-415/615

8

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #29

Reminder: our Mini-U db

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

CLASSc-id c-name units15-413 s.e. 215-412 o.s. 2

TAKESSSN c-id grade

123 15-413 A234 15-413 B

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #30

More examples

•  join: find names of students taking 15-415

)}41515...

..(|{

−=−

∧=

∧=∈∃∧

∈∃

idcenamesnamet

ssnessnsTAKESeSTUDENTst

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #31

More examples

•  join: find names of students taking 15-415

)}41515...

..(|{

−=−

∧=

∧=∈∃∧

∈∃

idcenamesnamet

ssnessnsTAKESeSTUDENTst

projection

selection

join

(Remember: ‘SPJ’ )

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #32

More examples

•  3-way join: find names of students taking a 2-unit course

Faloutsos - Pavlo CMU - 15-415/615

9

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #33

Reminder: our Mini-U db

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

CLASSc-id c-name units15-413 s.e. 215-412 o.s. 2

TAKESSSN c-id grade

123 15-413 A234 15-413 B

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #34

More examples

•  3-way join: find names of students taking a 2-unit course

)}2.....

..(|{

=

∧=

∧−=−

∧=∈∃

∈∃∧∈∃

unitscnamesnametidccidce

ssnessnsCLASScTAKESeSTUDENTst

selection

projection

join

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #35

More examples

•  3-way join: find names of students taking a 2-unit course - in rel. algebra??

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #36

More examples

•  3-way join: find names of students taking a 2-unit course - in rel. algebra??

))(( 2 CLASSTAKESSTUDENTunitsname ◃▹◃▹=σπ

Faloutsos - Pavlo CMU - 15-415/615

10

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #37

Even more examples:

•  self -joins: find Tom’s grandparent(s)

PCp-id c-idMary TomPeter MaryJohn Tom

PCp-id c-idMary TomPeter MaryJohn Tom

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #38

Even more examples:

•  self -joins: find Tom’s grandparent(s)

)}"".....(

|{

TomidcqidptidppidpqidcpPCqPCpt

=−

∧−=−

∧−=−

∈∃∧∈∃

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #39

Hard examples: DIVISION

•  find suppliers that shipped all the ABOMB parts

SHIPMENTs# p#s1 p1s2 p1s1 p2s3 p1s5 p3

ABOMBp#p1p2

BAD_Ss#s1÷ =

! CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #40

Hard examples: DIVISION

•  find suppliers that shipped all the ABOMB parts

{t |∀p(p∈ ABOMB⇒ (∃s∈ SHIPMENT (t.s# = s.s# ∧s.p# = p.p# )))}

!

Faloutsos - Pavlo CMU - 15-415/615

11

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #41

General pattern

•  three equivalent versions: –  1) if it’s bad, he shipped it –  2)either it was good, or he shipped it

–  3) there is no bad shipment that he missed

))}(((|{ tPABOMBppt ⇒∈∀

))}(((|{ tPABOMBppt ∨∉∀

))}(((|{ tPABOMBppt ¬∧∈¬∃

! CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #42

General pattern

•  three equivalent versions: –  1) if it’s bad, he shipped it –  2)either it was good, or he shipped it

–  3) there is no bad shipment that he missed

))}(((|{ tPABOMBppt ⇒∈∀

{t |∀p(p∉ ABOMB∨ (P(t))}

))}(((|{ tPABOMBppt ¬∧∈¬∃

a ⇒ b

¬ a ∨ b

!

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #43

General pattern

•  three equivalent versions: –  1) if it’s bad, he shipped it –  2)either it was good, or he shipped it

–  3) there is no bad shipment that he missed

))}(((|{ tPABOMBppt ⇒∈∀

{t |∀p(p∉ ABOMB∨ (P(t))}

))}(((|{ tPABOMBppt ¬∧∈¬∃€

∀x(P(x)) ≡¬∃x(¬P(x))

! CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #44

a ⇒ b is the same as ¬a ∨ b

•  If a is true, b must be true for the implication to be true. If a is true and b is false, the implication evaluates to false.

•  If a is not true, we don’t care about b, the expression is always true.

a T

F

T F b

T

T T

F

Faloutsos - Pavlo CMU - 15-415/615

12

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #45

More on division

•  find (SSNs of) students that take all the courses that ssn=123 does (and maybe even more) find students ‘s’ so that if 123 takes a course => so does ‘s’

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #46

More on division

•  find students that take all the courses that ssn=123 does (and maybe even more)

)})..1

..1(1

)123.((|{

ssnossntidctidct

TAKEStssntTAKEStto

=

∧−=−

∈∃

⇒=∧∈∀

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #47

Safety of expressions

•  FORBIDDEN: It has infinite output!! •  Instead, always use

}|{ STUDENTtt ∉

}....|{ TABLESOMEtt −∈

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #48

Overview - conclusions

•  rel. tuple calculus: DECLARATIVE –  dfn –  details –  equivalence to rel. algebra

•  rel. domain calculus + QBE

Faloutsos - Pavlo CMU - 15-415/615

13

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #49

General Overview •  relational model •  Formal query languages

–  relational algebra –  rel. tuple calculus –  rel. domain calculus

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #50

Rel. domain calculus (RDC)

•  Q: why? •  A: slightly easier than RTC, although

equivalent - basis for QBE. •  idea: domain variables (w/ F.O.L.) - eg: •  ‘find STUDENT record with ssn=123’

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #51

Rel. Dom. Calculus

}123,,|,,{ =∧>∈<>< sSTUDENTansans

•  find STUDENT record with ssn=123’

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #52

Details

•  Like R.T.C - symbols allowed:

•  quantifiers

≥≤≠=<>

⇒¬∨∧

),(,,,,,,,

,,,

∃∀,

Faloutsos - Pavlo CMU - 15-415/615

14

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #53

Details

•  but: domain (= column) variables, as opposed to tuple variables, eg:

STUDENTans >∈< ,,

ssn name address

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #54

Reminder: our Mini-U db

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

CLASSc-id c-name units15-413 s.e. 215-412 o.s. 2

TAKESSSN c-id grade

123 15-413 A234 15-413 B

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #55

Examples

•  find all student records

}|{ STUDENTtt ∈

},,|,,{ STUDENTansans >∈<><

RTC:

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #56

Examples

•  (selection) find student record with ssn=123

Faloutsos - Pavlo CMU - 15-415/615

15

CMU SCS

(‘Proof’ of RDC = RA)

•  Again, we show examples of the 5 fundamental operators, in RDC

Faloutsos - Pavlo CMU SCS 15-415/615 #57

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #58

Examples

•  (selection) find student record with ssn=123

}123.|{ =∧∈ ssntSTUDENTttRTC:

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #59

Examples

•  (selection) find student record with ssn=123

}123.|{ =∧∈ ssntSTUDENTttRTC:

},,123|,,123{ STUDENTanan >∈<><

or

}123,,|,,{ =∧>∈<>< sSTUDENTansans

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #60

Examples

•  (projection) find name of student with ssn=123

},,123|{ STUDENTann >∈<><

Faloutsos - Pavlo CMU - 15-415/615

16

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #61

Examples

•  (projection) find name of student with ssn=123

}),,123(|{ STUDENTanan >∈<∃><

need to ‘restrict’ “a”

)}..123.(|{

namesnametssnsSTUDENTst

=

∧=∈∃RTC:

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #62

Examples cont’d

•  (union) get records of both PT and FT students

}__|{STUDENTPTtSTUDENTFTtt

∨∈RTC:

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #63

Examples cont’d

•  (union) get records of both PT and FT students

}_,,_,,|,,{

STUDENTPTansSTUDENTFTansans

>∈<

∨>∈<><

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #64

Examples

•  difference: find students that are not staff

RTC:

}|{STAFFtSTUDENTtt

∧∈

Faloutsos - Pavlo CMU - 15-415/615

17

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #65

Examples

•  difference: find students that are not staff

},,,,|,,{STAFFans

STUDENTansans>∉<

∧>∈<><

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #66

Cartesian product

•  eg., dog-breeding: MALE x FEMALE •  gives all possible couples

MALEnamespikespot

FEMALEnamelassieshiba

x = M.name F.namespike lassiespike shibaspot lassiespot shiba

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #67

Cartesian product

•  find all the pairs of (male, female) - RTC:

}....

|{

namefnameftnamemnamemt

FEMALEfMALEmt

=−

∧=−

∈∃

∧∈∃

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #68

Cartesian product

•  find all the pairs of (male, female) - RDC:

Faloutsos - Pavlo CMU - 15-415/615

18

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #69

Cartesian product

•  find all the pairs of (male, female) - RDC:

{< m, f > | < m >∈MALE ∧< f >∈FEMALE }

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #70

‘Proof’ of equivalence

•  rel. algebra <-> rel. domain calculus <-> rel. tuple calculus

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #71

Overview - detailed

•  rel. domain calculus – why? –  details –  examples –  equivalence with rel. algebra – more examples; ‘safety’ of expressions

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #72

More examples

•  join: find names of students taking 15-415

Faloutsos - Pavlo CMU - 15-415/615

19

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #73

Reminder: our Mini-U db

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

CLASSc-id c-name units15-413 s.e. 215-412 o.s. 2

TAKESSSN c-id grade

123 15-413 A234 15-413 B

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #74

More examples

•  join: find names of students taking 15-415 - in RTC

)}41515...

..(|{

−=−

∧=

∧=∈∃∧

∈∃

idcenamesnamet

ssnessnsTAKESeSTUDENTst

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #75

More examples

•  join: find names of students taking 15-415 - in RDC

)},41515,,,(|{TAKESgs

STUDENTansgasn>∈−<∧

>∈<∃∃∃><

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #76

Sneak preview of QBE:

)},41515,,,(|{TAKESgs

STUDENTansgasn>∈−<∧

>∈<∃∃∃><

STUDENTSsn Name Address_x P.

TAKESSSN c-id grade_x 15-415

Faloutsos - Pavlo CMU - 15-415/615

20

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #77

Glimpse of QBE:

STUDENTSsn Name Address_x P.

TAKESSSN c-id grade_x 15-415

•  very user friendly •  heavily based on RDC •  very similar to MS Access interface and

pgAdminIII

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #78

More examples

•  3-way join: find names of students taking a 2-unit course - in RTC:

)}2.....

..(|{

=

∧=

∧−=−

∧=∈∃

∈∃∧∈∃

unitscnamesnametidccidce

ssnessnsCLASScTAKESeSTUDENTst

selection

projection

join

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #79

Reminder: our Mini-U db

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

CLASSc-id c-name units15-413 s.e. 215-412 o.s. 2

TAKESSSN c-id grade

123 15-413 A234 15-413 B

_x .P

_x _y

_y 2

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #80

More examples

•  3-way join: find names of students taking a 2-unit course

}2,,,,,,

.............|{

CLASScncTAKESgcsSTUDENTans

n

>∈<

∧>∈<

∧>∈<

><

Faloutsos - Pavlo CMU - 15-415/615

21

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #81

More examples

•  3-way join: find names of students taking a 2-unit course

)}2,,,,,,

(,,,,|{

CLASScncTAKESgcsSTUDENTanscngcasn

>∈<

∧>∈<

∧>∈<

∃><

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #82

Even more examples:

•  self -joins: find Tom’s grandparent(s)

PCp-id c-idMary TomPeter MaryJohn Tom

PCp-id c-idMary TomPeter MaryJohn Tom

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #83

Even more examples:

•  self -joins: find Tom’s grandparent(s)

)}"".....(

|{

TomidcqidptidppidpqidcpPCqPCpt

=−

∧−=−

∧−=−

∈∃∧∈∃

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #84

Even more examples:

•  self -joins: find Tom’s grandparent(s)

)}"".....(

|{

TomidcqidptidppidpqidcpPCqPCpt

=−

∧−=−

∧−=−

∈∃∧∈∃

)}"",,(|{PCTompPCpgpg

>∈<

∧>∈<∃><

Faloutsos - Pavlo CMU - 15-415/615

22

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #85

Even more examples:

•  self -joins: find Tom’s grandparent(s)

)}"",,(|{PCTompPCpgpg

>∈<

∧>∈<∃><

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #86

Hard examples: DIVISION

•  find suppliers that shipped all the ABOMB parts

SHIPMENTs# p#s1 p1s2 p1s1 p2s3 p1s5 p3

ABOMBp#p1p2

BAD_Ss#s1÷ =

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #87

Hard examples: DIVISION

•  find suppliers that shipped all the ABOMB parts

{t |∀p(p∈ ABOMB⇒ (∃s∈ SHIPMENT (t.s# = s.s# ∧s.p# = p.p# )))}

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #88

Hard examples: DIVISION

•  find suppliers that shipped all the ABOMB parts

{< s >|∀p(< p >∈ ABOMB⇒< s, p >∈ SHIPMENT )}

{t |∀p(p∈ ABOMB⇒ (∃s∈ SHIPMENT (t.s# = s.s# ∧s.p# = p.p# )))}

Faloutsos - Pavlo CMU - 15-415/615

23

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #89

More on division

•  find students that take all the courses that ssn=123 does (and maybe even more)

)})..1

..1(1

)123.((|{

ssnossntidctidct

TAKEStssntTAKEStto

=

∧−=−

∈∃

⇒=∧∈∀

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #90

More on division

•  find students that take all the courses that ssn=123 does (and maybe even more)

))})',,('),,123((|{

TAKESgcsgTAKESgcgcs

∈><∃

⇒>∈<∃∀><

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #91

Safety of expressions

•  similar to RTC •  FORBIDDEN:

{< s,n,a >|< s,n,a >∉ STUDENT }

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #92

Overview - detailed

•  rel. domain calculus + QBE –  dfn –  details –  equivalence to rel. algebra

Faloutsos - Pavlo CMU - 15-415/615

24

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #93

Fun Drill:Your turn … •  Schema:

Movie(title, year, studioName) ActsIn(movieTitle, starName) Star(name, gender, birthdate, salary)

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #94

Your turn … •  Queries to write in TRC:

–  Find all movies by Paramount studio –  … movies starring Kevin Bacon –  Find stars who have been in a film w/Kevin Bacon –  Stars within six degrees of Kevin Bacon* –  Stars connected to K. Bacon via any number of

films**

* Try two degrees for starters ** Good luck with this one!

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #95

Answers …

•  Find all movies by Paramount studio

{M | M∈Movie ∧ M.studioName = ‘Paramount’}

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #96

Answers …

•  Movies starring Kevin Bacon

{M | M∈Movie ∧ ∃A∈ActsIn(A.movieTitle = M.title ∧

A.starName = ‘Bacon’))}

Faloutsos - Pavlo CMU - 15-415/615

25

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #97

Answers … •  Stars who have been in a film w/Kevin

Bacon {S | S∈Star ∧ ∃A∈ActsIn(A.starName = S.name ∧ ∃A2∈ActsIn(A2.movieTitle = A.movieTitle ∧

A2.starName = ‘Bacon’))}

movie star A2:

S: name …

‘Bacon’

movie star A:

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #98

Answers …

•  Stars within six degrees of Kevin Bacon

{S | S∈Star ∧ ∃A∈ActsIn(A.starName = S.name ∧ ∃A2∈ActsIn(A2.movieTitle = A.movieTitle ∧ ∃A3∈ActsIn(A3.starName = A2.starName ∧ ∃A4∈ActsIn(A4.movieTitle = A3.movieTitle ∧ A4.starName = ‘Bacon’))}

two

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #99

Two degrees:

S: name …

‘Bacon’ movie star A4:

movie star A3:

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #100

Two degrees:

S: name …

‘Bacon’

movie star A2:

movie star A:

movie star A4:

movie star A3:

Faloutsos - Pavlo CMU - 15-415/615

26

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #101

Answers …

•  Stars connected to K. Bacon via any number of films

•  Sorry … that was a trick question – Not expressible in relational calculus!!

•  What about in relational algebra? – No – RA, RTC, RDC are equivalent

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #102

Expressive Power

•  Expressive Power (Theorem due to Codd): – Every query that can be expressed in relational

algebra can be expressed as a safe query in RDC / RTC; the converse is also true.

•  Relational Completeness: Query language (e.g., SQL) can express every query

that is expressible in relational algebra/calculus. (actually, SQL is more powerful, as we will see…)

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #103

Summary •  The relational model has rigorously defined query

languages — simple and powerful. •  Relational algebra is more operational/procedural

–  useful as internal representation for query evaluation plans

•  Relational calculus is declarative –  users define queries in terms of what they want, not in

terms of how to compute it.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 #104

Summary - cnt’d •  Several ways of expressing a given query

–  a query optimizer chooses best plan. •  Algebra and safe calculus: same expressive power

=> relational completeness.