carnegie mellon univ. dept. of computer science 15-415 - database applications
DESCRIPTION
Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications. 15-415/Fall ‘2010 Lecture#7: Relational calculus (slides from Christos Faloutsos). General Overview - rel. model. history concepts Formal query languages relational algebra rel. tuple calculus - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/1.jpg)
CMU SCS
Carnegie Mellon Univ.Dept. of Computer Science
15-415 - Database Applications
15-415/Fall ‘2010
Lecture#7: Relational calculus
(slides from Christos Faloutsos)
![Page 2: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/2.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #2
General Overview - rel. model
• history
• concepts
• Formal query languages– relational algebra– rel. tuple calculus– rel. domain calculus
![Page 3: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/3.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #3
Overview - detailed
• rel. tuple calculus– why?– details– examples– equivalence with rel. algebra– more examples; ‘safety’ of expressions
• re. domain calculus + QBE
![Page 4: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/4.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #4
Motivation
• Q: weakness of rel. algebra?
• A: procedural– describes the steps (ie., ‘how’)– (still useful, for query optimization)
![Page 5: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/5.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #5
Solution: rel. calculus
– describes what we want– two equivalent flavors: ‘tuple’ and ‘domain’
calculus– basis for SQL and QBE, resp.
![Page 6: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/6.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #6
Rel. tuple calculus (RTC)
• first order logic
)}(|{ tPt
}|{ STUDENTtt
‘Give me tuples ‘t’, satisfying predicate P - eg:
![Page 7: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/7.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #7
Details
• symbols allowed:
• quantifiers
),(,
,,,,,,
,,,
,
![Page 8: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/8.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #8
Specifically
• Atom
'..
.
attrsattrt
constattrt
TABLEt
![Page 9: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/9.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #9
Specifically
• Formula:– atom– if P1, P2 are formulas, so are– if P(s) is a formula, so are
...21;21 PPPP
))((
))((
sPs
sPs
![Page 10: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/10.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #10
Specifically
• Reminders:– DeMorgan– implication:– double negation:
))(())(( sPTABLEssPTABLEs
)21(21 PPPP
2121 PPPP
‘every human is mortal : no human is immortal’
![Page 11: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/11.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #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
![Page 12: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/12.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #12
Examples
• find all student records
}|{ STUDENTtt
output tuple of type ‘STUDENT’
![Page 13: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/13.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #13
Examples
• (selection) find student record with ssn=123
![Page 14: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/14.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #14
Examples
• (selection) find student record with ssn=123
}123.|{ ssntSTUDENTtt
![Page 15: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/15.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #15
Examples
• (projection) find name of student with ssn=123
}123.|{ ssntSTUDENTtt
![Page 16: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/16.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #16
Examples
• (projection) find name of student with ssn=123
)}..
123.(|{
namesnamet
ssnsSTUDENTst
‘t’ has only one column
![Page 17: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/17.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #17
‘Tracing’
)}..
123.(|{
namesnamet
ssnsSTUDENTst
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
s
Nameaaaa….jones…zzzz
t
![Page 18: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/18.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #18
Examples cont’d
• (union) get records of both PT and FT students
![Page 19: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/19.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #19
Examples cont’d
• (union) get records of both PT and FT students
}_
_|{
STUDENTPTt
STUDENTFTtt
![Page 20: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/20.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #20
Examples
• difference: find students that are not staff
(assuming that STUDENT and STAFF are union-compatible)
![Page 21: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/21.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #21
Examples
• difference: find students that are not staff
}
|{
STAFFt
STUDENTtt
![Page 22: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/22.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #22
Cartesian product
• eg., dog-breeding: MALE x FEMALE
• gives all possible couples
MALEnamespikespot
FEMALEnamelassieshiba
x =M.name F.namespike lassiespike shibaspot lassiespot shiba
![Page 23: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/23.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #23
Cartesian product
• find all the pairs of (male, female)
}..
..
|{
namefnameft
namemnamemt
FEMALEf
MALEmt
![Page 24: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/24.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #24
‘Proof’ of equivalence
• rel. algebra <-> rel. tuple calculus
![Page 25: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/25.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #25
Overview - detailed
• rel. tuple calculus– why?– details– examples– equivalence with rel. algebra– more examples; ‘safety’ of expressions
• re. domain calculus + QBE
![Page 26: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/26.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #26
More examples
• join: find names of students taking 15-415
![Page 27: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/27.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #27
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
![Page 28: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/28.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #28
More examples
• join: find names of students taking 15-415
)}41515.
..
..(
|{
idce
namesnamet
ssnessnsTAKESe
STUDENTst
![Page 29: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/29.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #29
More examples
• join: find names of students taking 15-415
)}41515.
..
..(
|{
idce
namesnamet
ssnessnsTAKESe
STUDENTst
projection
selection
join
![Page 30: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/30.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #30
More examples
• 3-way join: find names of students taking a 2-unit course
![Page 31: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/31.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #31
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
![Page 32: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/32.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #32
More examples
• 3-way join: find names of students taking a 2-unit course
)}2.
..
..
..(
|{
unitsc
namesnamet
idccidce
ssnessnsCLASSc
TAKESeSTUDENTst
selection
projection
join
![Page 33: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/33.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #33
More examples
• 3-way join: find names of students taking a 2-unit course - in rel. algebra??
))(( 2 CLASSTAKESSTUDENTunitsname
![Page 34: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/34.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #34
Even more examples:
• self -joins: find Tom’s grandparent(s)
PCp-id c-idMary TomPeter MaryJohn Tom
PCp-id c-idMary TomPeter MaryJohn Tom
![Page 35: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/35.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #35
Even more examples:
• self -joins: find Tom’s grandparent(s)
)}"".
..
..(
|{
Tomidcq
idptidpp
idpqidcp
PCqPCpt
![Page 36: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/36.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #36
Hard examples: DIVISION
• find suppliers that shipped all the ABOMB parts
SHIPMENTs# p#s1 p1s2 p1s1 p2s3 p1s5 p3
ABOMBp#p1p2
BAD_Ss#s1
![Page 37: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/37.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #37
Hard examples: DIVISION
• find suppliers that shipped all the ABOMB parts
)))}#.#.
#.#.
(
((|{
ppps
ssst
SHIPMENTs
ABOMBppt
![Page 38: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/38.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #38
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
![Page 39: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/39.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #39
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.
aT
F
T Fb
T
T T
F
![Page 40: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/40.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #40
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’
![Page 41: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/41.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #41
More on division
• find students that take all the courses that ssn=123 does (and maybe even more)
)}
)..1
..1
(1
)123.((|{
ssnossnt
idctidct
TAKESt
ssntTAKEStto
![Page 42: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/42.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #42
Safety of expressions
• FORBIDDEN:
It has infinite output!!
• Instead, always use
}|{ STUDENTtt
}....|{ TABLESOMEtt
![Page 43: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/43.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #43
Overview - conclusions
• rel. tuple calculus: DECLARATIVE– dfn– details– equivalence to rel. algebra
• rel. domain calculus + QBE
![Page 44: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/44.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #44
General Overview
• relational model
• Formal query languages– relational algebra– rel. tuple calculus– rel. domain calculus
![Page 45: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/45.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #45
Overview - detailed
• rel. tuple calculus– dfn– details– equivalence to rel. algebra
• rel. domain calculus + QBE
![Page 46: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/46.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #46
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’
![Page 47: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/47.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #47
Rel. Dom. Calculus
}123,,|,,{ sSTUDENTansans
• find STUDENT record with ssn=123’
![Page 48: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/48.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #48
Details
• Like R.T.C - symbols allowed:
• quantifiers
),(,
,,,,,,
,,,
,
![Page 49: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/49.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #49
Details
• but: domain (= column) variables, as opposed to tuple variables, eg:
STUDENTans ,,
ssnname address
![Page 50: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/50.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #50
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
![Page 51: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/51.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #51
Examples
• find all student records
}|{ STUDENTtt
},,|,,{ STUDENTansans
RTC:
![Page 52: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/52.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #52
Examples
• (selection) find student record with ssn=123
![Page 53: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/53.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #53
Examples
• (selection) find student record with ssn=123
}123.|{ ssntSTUDENTttRTC:
},,123|,,123{ STUDENTanan
or
}123,,|,,{ sSTUDENTansans
![Page 54: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/54.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #54
Examples
• (projection) find name of student with ssn=123
},,123|{ STUDENTann
![Page 55: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/55.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #55
Examples
• (projection) find name of student with ssn=123
}),,123(|{ STUDENTanan
need to ‘restrict’ “a”
)}..
123.(|{
namesnamet
ssnsSTUDENTst
RTC:
![Page 56: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/56.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #56
Examples cont’d
• (union) get records of both PT and FT students
}_
_|{
STUDENTPTt
STUDENTFTtt
RTC:
![Page 57: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/57.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #57
Examples cont’d
• (union) get records of both PT and FT students
}_,,
_,,|,,{
STUDENTPTans
STUDENTFTansans
![Page 58: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/58.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #58
Examples
• difference: find students that are not staff
RTC:
}
|{
STAFFt
STUDENTtt
![Page 59: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/59.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #59
Examples
• difference: find students that are not staff
},,
,,|,,{
STAFFans
STUDENTansans
![Page 60: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/60.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #60
Cartesian product
• eg., dog-breeding: MALE x FEMALE
• gives all possible couples
MALEnamespikespot
FEMALEnamelassieshiba
x =M.name F.namespike lassiespike shibaspot lassiespot shiba
![Page 61: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/61.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #61
Cartesian product
• find all the pairs of (male, female) - RTC:
}..
..
|{
namefnameft
namemnamemt
FEMALEf
MALEmt
![Page 62: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/62.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #62
Cartesian product
• find all the pairs of (male, female) - RDC:
}
|,{
FEMALEf
MALEmfm
![Page 63: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/63.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #63
‘Proof’ of equivalence
• rel. algebra <-> rel. domain calculus
<-> rel. tuple calculus
![Page 64: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/64.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #64
Overview - detailed
• rel. domain calculus– why?– details– examples– equivalence with rel. algebra– more examples; ‘safety’ of expressions
![Page 65: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/65.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #65
More examples
• join: find names of students taking 15-415
![Page 66: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/66.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #66
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
![Page 67: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/67.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #67
More examples
• join: find names of students taking 15-415 - in RTC
)}41515.
..
..(
|{
idce
namesnamet
ssnessnsTAKESe
STUDENTst
![Page 68: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/68.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #68
More examples
• join: find names of students taking 15-415 - in RDC
)},41515,
,,(|{
TAKESgs
STUDENTansgasn
![Page 69: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/69.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #69
Sneak preview of QBE:
)},41515,
,,(|{
TAKESgs
STUDENTansgasn
STUDENTSsn Name Address_x P.
TAKESSSN c-id grade_x 15-415
![Page 70: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/70.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #70
Sneak preview 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
![Page 71: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/71.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #71
More examples
• 3-way join: find names of students taking a 2-unit course - in RTC:
)}2.
..
..
..(
|{
unitsc
namesnamet
idccidce
ssnessnsCLASSc
TAKESeSTUDENTst
selection
projection
join
![Page 72: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/72.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #72
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
![Page 73: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/73.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #73
More examples
• 3-way join: find names of students taking a 2-unit course
}2,,
,,
,,
.............|{
CLASScnc
TAKESgcs
STUDENTans
n
![Page 74: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/74.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #74
More examples
• 3-way join: find names of students taking a 2-unit course
)}
2,,
,,
,,
(,,,,|{
CLASScnc
TAKESgcs
STUDENTans
cngcasn
![Page 75: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/75.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #75
Even more examples:
• self -joins: find Tom’s grandparent(s)
PCp-id c-idMary TomPeter MaryJohn Tom
PCp-id c-idMary TomPeter MaryJohn Tom
![Page 76: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/76.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #76
Even more examples:
• self -joins: find Tom’s grandparent(s)
)}"".
..
..(
|{
Tomidcq
idptidpp
idpqidcp
PCqPCpt
![Page 77: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/77.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #77
Even more examples:
• self -joins: find Tom’s grandparent(s)
)}"".
..
..(
|{
Tomidcq
idptidpp
idpqidcp
PCqPCpt
)}"",
,(|{
PCTomp
PCpgpg
![Page 78: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/78.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #78
Even more examples:
• self -joins: find Tom’s grandparent(s)
)}"",
,(|{
PCTomp
PCpgpg
![Page 79: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/79.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #79
Hard examples: DIVISION
• find suppliers that shipped all the ABOMB parts
SHIPMENTs# p#s1 p1s2 p1s1 p2s3 p1s5 p3
ABOMBp#p1p2
BAD_Ss#s1
![Page 80: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/80.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #80
Hard examples: DIVISION
• find suppliers that shipped all the ABOMB parts
)))}#.#.
#.#.
(
((|{
ppps
ssst
SHIPMENTs
ABOMBppt
![Page 81: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/81.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #81
Hard examples: DIVISION
• find suppliers that shipped all the ABOMB parts
)},
(|{
SHIPMENTps
ABOMBpps
)))}#.#.
#.#.
(
((|{
ppps
ssst
SHIPMENTs
ABOMBppt
![Page 82: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/82.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #82
More on division
• find students that take all the courses that ssn=123 does (and maybe even more)
)}
)..1
..1
(1
)123.((|{
ssnossnt
idctidct
TAKESt
ssntTAKEStto
![Page 83: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/83.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #83
More on division
• find students that take all the courses that ssn=123 does (and maybe even more)
))})',,('
),,123((|{
TAKESgcsg
TAKESgcgcs
![Page 84: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/84.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #84
Safety of expressions
• similar to RTC
• FORBIDDEN:
},,|,,{ STUDENTansans
![Page 85: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/85.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #85
Overview - detailed
• rel. domain calculus + QBE– dfn– details– equivalence to rel. algebra
![Page 86: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/86.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #86
Fun Drill:Your turn …
• Schema:Movie(title, year, studioName)
ActsIn(movieTitle, starName)
Star(name, gender, birthdate, salary)
![Page 87: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/87.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #87
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!
![Page 88: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/88.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #88
Answers …
• Find all movies by Paramount studio
{M | MMovie M.studioName = ‘Paramount’}
![Page 89: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/89.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #89
Answers …
• Movies starring Kevin Bacon
{M | MMovie AActsIn(A.movieTitle = M.title
A.starName = ‘Bacon’))}
![Page 90: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/90.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #90
Answers …
• Stars who have been in a film w/Kevin Bacon
{S | SStar AActsIn(A.starName = S.name A2ActsIn(A2.movieTitle = A.movieTitle A2.starName = ‘Bacon’))}
movie starA2:
S: name …
‘Bacon’
movie starA:
![Page 91: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/91.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #91
Answers …
• Stars within six degrees of Kevin Bacon
{S | SStar AActsIn(A.starName = S.name A2ActsIn(A2.movieTitle = A.movieTitle A3ActsIn(A3.starName = A2.starName A4ActsIn(A4.movieTitle = A3.movieTitle A4.starName = ‘Bacon’))}
two
![Page 92: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/92.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #92
Two degrees:
S: name …
‘Bacon’movie starA4
:
movie starA3:
![Page 93: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/93.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #93
Two degrees:
S: name …
‘Bacon’
movie starA2:
movie starA:
movie starA4:
movie starA3:
![Page 94: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/94.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #94
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?– We will be able to answer this question shortly …
![Page 95: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/95.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #95
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 DRC / TRC; 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…)
![Page 96: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/96.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #96
Question:
• Can we express previous query (‘any # steps’) in relational algebra?
• A: If we could, then by Codd’s theorem we could also express it in relational calculus. However, we know the latter is not possible, so the answer is no.
![Page 97: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/97.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #97
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.
![Page 98: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader035.vdocument.in/reader035/viewer/2022062422/568135c5550346895d9d26ac/html5/thumbnails/98.jpg)
CMU SCS
Faloutsos CMU SCS 15-415 #98
Summary - cnt’d• Several ways of expressing a given query
– a query optimizer should choose the most efficient version.
• Algebra and safe calculus have same expressive power– leads to the notion of relational completeness.