Faloutsos CMU - 15-415
CMU SCS CMU SCS
General Overview - rel. model Carnegie Mellon Univ. Dept. of Computer Science • history 15-415 - Database Applications • concepts • Formal query languages – relational algebra Lecture#6: Relational calculus – rel. tuple calculus – rel. domain calculus
Faloutsos CMU SCS 15-415 #2
CMU SCS CMU SCS
Overview - detailed Motivation
• rel. tuple calculus • Q: weakness of rel. algebra? – why? • A: procedural – details – describes the steps (ie., ‘how’) – examples – (still useful, for query optimization) – equivalence with rel. algebra – more examples; ‘safety’ of expressions • re. domain calculus + QBE
Faloutsos CMU SCS 15-415 #3 Faloutsos CMU SCS 15-415 #4
CMU SCS CMU SCS
Solution: rel. calculus Rel. tuple calculus (RTC)
– describes what we want • first order logic – two equivalent flavors: ‘tuple’ and ‘domain’ calculus {t | P(t)} – basis for SQL and QBE, resp. ‘Give me tuples ‘t’, satisfying predicate P - eg:
{t | t STUDENT}
Faloutsos CMU SCS 15-415 #5 Faloutsos CMU SCS 15-415 #6
1 Faloutsos CMU - 15-415
CMU SCS CMU SCS
Details Specifically
• symbols allowed: • Atom , , , t TABLE , , , , , , t.attr const (, ), t.attr s.attr'
• quantifiers ,
Faloutsos CMU SCS 15-415 #7 Faloutsos CMU SCS 15-415 #8
CMU SCS CMU SCS
Specifically Specifically
• Formula: • Reminders: – atom – DeMorgan P1 P2 (P1 P2) – if P1, P2 are formulas, so are P1 P2;P1 P2... – implication: P1 P2 P1 P2 – if P(s) is a formula, so are s(P(s)) – double negation: s(P(s)) sTABLE (P(s)) sTABLE (P(s))
„every human is mortal : no human is immortal‟
Faloutsos CMU SCS 15-415 #9 Faloutsos CMU SCS 15-415 #10
CMU SCS CMU SCS
Reminder: our Mini-U db Examples
STUDENT CLASS • find all student records Ssn Name Address c-id c-name units 123 smith main str 15-413 s.e. 2 234 jones forbes ave 15-412 o.s. 2 {t | t STUDENT}
TAKES SSN c-id grade output 123 15-413 A of type „STUDENT‟ 234 15-413 B tuple
Faloutsos CMU SCS 15-415 #11 Faloutsos CMU SCS 15-415 #12
2 Faloutsos CMU - 15-415
CMU SCS CMU SCS
Examples Examples
• (selection) find student record with ssn=123 • (selection) find student record with ssn=123
{t | t STUDENT t.ssn 123}
Faloutsos CMU SCS 15-415 #13 Faloutsos CMU SCS 15-415 #14
CMU SCS CMU SCS
Examples Examples
• (projection) find name of student with • (projection) find name of student with ssn=123 ssn=123 {t | t STUDENT t.ssn 123} {t | s STUDENT(s.ssn 123 t.name s.name)}
„t‟ has only one column
Faloutsos CMU SCS 15-415 #15 Faloutsos CMU SCS 15-415 #16
CMU SCS CMU SCS
‘Tracing’ Examples cont’d {t | s STUDENT(s.ssn 123 • (union) get records of both PT and FT t.name s.name)} students
Name t aaaa …. STUDENT jones Ssn Name Address … s 123 smith main str zzzz 234 jones forbes ave
Faloutsos CMU SCS 15-415 #17 Faloutsos CMU SCS 15-415 #18
3 Faloutsos CMU - 15-415
CMU SCS CMU SCS
Examples cont’d Examples
• (union) get records of both PT and FT • difference: find students that are not staff students {t | t FT _ STUDENT t PT _ STUDENT}
(assuming that STUDENT and STAFF are union-compatible)
Faloutsos CMU SCS 15-415 #19 Faloutsos CMU SCS 15-415 #20
CMU SCS CMU SCS
Examples Cartesian product
• difference: find students that are not staff • eg., dog-breeding: MALE x FEMALE • gives all possible couples {t | t STUDENT
t STAFF} MALE FEMALE M.name F.name name x name = spike lassie spike lassie spike shiba spot shiba spot lassie spot shiba
Faloutsos CMU SCS 15-415 #21 Faloutsos CMU SCS 15-415 #22
CMU SCS CMU SCS
Cartesian product ‘Proof’ of equivalence
• find all the pairs of (male, female) • rel. algebra <-> rel. tuple calculus
{t | m MALE f FEMALE t.m name m.name t. f name f .name}
Faloutsos CMU SCS 15-415 #23 Faloutsos CMU SCS 15-415 #24
4 Faloutsos CMU - 15-415
CMU SCS CMU SCS
Overview - detailed More examples
• rel. tuple calculus • join: find names of students taking 15-415 – why? – details – examples – equivalence with rel. algebra – more examples; ‘safety’ of expressions • re. domain calculus + QBE
Faloutsos CMU SCS 15-415 #25 Faloutsos CMU SCS 15-415 #26
CMU SCS CMU SCS
Reminder: our Mini-U db More examples
STUDENT CLASS • join: find names of students taking 15-415 Ssn Name Address c-id c-name units 123 smith main str 15-413 s.e. 2 {t | s STUDENT 234 jones forbes ave 15-412 o.s. 2 eTAKES ( s.ssn e.ssn
TAKES t.name s.name SSN c-id grade e.c id 15 415)} 123 15-413 A 234 15-413 B
Faloutsos CMU SCS 15-415 #27 Faloutsos CMU SCS 15-415 #28
CMU SCS CMU SCS
More examples More examples
• join: find names of students taking 15-415 • 3-way join: find names of students taking a 2-unit course {t | s STUDENT eTAKES ( s.ssn e.ssn join t.name s.name projection e.c id 15 415)} selection
Faloutsos CMU SCS 15-415 #29 Faloutsos CMU SCS 15-415 #30
5 Faloutsos CMU - 15-415
CMU SCS CMU SCS
Reminder: our Mini-U db More examples
STUDENT CLASS • 3-way join: find names of students taking a Ssn Name Address c-id c-name units 2-unit course 123 smith main str 15-413 s.e. 2 234 jones forbes ave 15-412 o.s. 2 {t | s STUDENT eTAKES c CLASS( s.ssn e.ssn join TAKES e.c id c.c id SSN c-id grade t.name s.name 123 15-413 A projection 234 15-413 B c.units 2)} selection
Faloutsos CMU SCS 15-415 #31 Faloutsos CMU SCS 15-415 #32
CMU SCS CMU SCS
More examples Even more examples:
• 3-way join: find names of students taking a • self -joins: find Tom’s grandparent(s) 2-unit course - in rel. algebra??
PC p-id c-id Mary Tom name(units2 (STUDENT TAKES CLASS)) Peter Mary John Tom
Faloutsos CMU SCS 15-415 #33 Faloutsos CMU SCS 15-415 #34
CMU SCS CMU SCS
Even more examples: Hard examples: DIVISION
• self -joins: find Tom’s grandparent(s) • find suppliers that shipped all the ABOMB parts SHIPMENT {t | p PC q PC s# p# ABOMB BAD_S s1 p1 p# s# ( p.c id q.p id s2 p1 p1 s1 p.p id t.p id s1 p2 p2 s3 p1 q.c id "Tom")} s5 p3
Faloutsos CMU SCS 15-415 #35 Faloutsos CMU SCS 15-415 #36
6 Faloutsos CMU - 15-415
CMU SCS CMU SCS
Hard examples: DIVISION General pattern
• find suppliers that shipped all the ABOMB • three equivalent versions: parts – 1) if it’s bad, he shipped it {t | p(p ABOMB (P(t))} {t | p( p ABOMB ( – 2)either it was good, or he shipped it s SHIPMENT ( {t | p(p ABOMB (P(t))} t.s# s.s# – 3) there is no bad shipment that he missed s.p# p.p#)))} {t | p(p ABOMB (P(t))}
Faloutsos CMU SCS 15-415 #37 Faloutsos CMU SCS 15-415 #38
CMU SCS CMU SCS
a b is the same as a b More on division b • If a is true, b must be • find (SSNs of) students that take all the T F true for the courses that ssn=123 does (and maybe even implication to be true. more) T T F If a is true and b is a false, the implication find students ‘s’ so that evaluates to false. if 123 takes a course => so does ‘s’ F T T • If a is not true, we don’t care about b, the expression is always true. Faloutsos CMU SCS 15-415 #39 Faloutsos CMU SCS 15-415 #40
CMU SCS CMU SCS
More on division Safety of expressions
• find students that take all the courses that • FORBIDDEN: {t | t STUDENT} ssn=123 does (and maybe even more) {o | t((t TAKES t.ssn 123) It has infinite output!! t1TAKES ( • Instead, always use t1.c id t.c id t1.ssn o.ssn) {t |....t SOME TABLE} )}
Faloutsos CMU SCS 15-415 #41 Faloutsos CMU SCS 15-415 #42
7 Faloutsos CMU - 15-415
CMU SCS CMU SCS
Overview - conclusions General Overview
• rel. tuple calculus: DECLARATIVE • relational model – dfn • Formal query languages – details – relational algebra – equivalence to rel. algebra – rel. tuple calculus • rel. domain calculus + QBE – rel. domain calculus
Faloutsos CMU SCS 15-415 #43 Faloutsos CMU SCS 15-415 #44
CMU SCS CMU SCS
Overview - detailed Rel. domain calculus (RDC)
• rel. tuple calculus • Q: why? – dfn • A: slightly easier than RTC, although – details equivalent - basis for QBE. – equivalence to rel. algebra • idea: domain variables (w/ F.O.L.) - eg: • rel. domain calculus + QBE • ‘find STUDENT record with ssn=123’
Faloutsos CMU SCS 15-415 #45 Faloutsos CMU SCS 15-415 #46
CMU SCS CMU SCS
Rel. Dom. Calculus Details • find STUDENT record with ssn=123’ • Like R.T.C - symbols allowed:
, , , , , , , , , { s,n,a | s,n,a STUDENT s 123} (, ),
• quantifiers ,
Faloutsos CMU SCS 15-415 #47 Faloutsos CMU SCS 15-415 #48
8 Faloutsos CMU - 15-415
CMU SCS CMU SCS
Details Reminder: our Mini-U db
• but: domain (= column) variables, as STUDENT CLASS opposed to tuple variables, eg: Ssn Name Address c-id c-name units 123 smith main str 15-413 s.e. 2 234 jones forbes ave 15-412 o.s. 2 s,n,a STUDENT TAKES SSN c-id grade 123 15-413 A ssn 234 15-413 B name address
Faloutsos CMU SCS 15-415 #49 Faloutsos CMU SCS 15-415 #50
CMU SCS CMU SCS
Examples Examples
• find all student records • (selection) find student record with ssn=123
{ s,n,a | s,n,a STUDENT}
RTC: {t | t STUDENT}
Faloutsos CMU SCS 15-415 #51 Faloutsos CMU SCS 15-415 #52
CMU SCS CMU SCS
Examples Examples
• (selection) find student record with ssn=123 • (projection) find name of student with ssn=123 {123,n,a |123,n,a STUDENT} { n | 123,n,a STUDENT } or { s,n,a | s,n,a STUDENT s 123}
RTC: {t | t STUDENT t.ssn 123}
Faloutsos CMU SCS 15-415 #53 Faloutsos CMU SCS 15-415 #54
9 Faloutsos CMU - 15-415
CMU SCS CMU SCS
Examples Examples cont’d
• (projection) find name of student with • (union) get records of both PT and FT ssn=123 students { n | a(123,n,a STUDENT)}
need to „restrict‟ “a”
RTC: {t | t FT _ STUDENT RTC: {t | s STUDENT(s.ssn 123 t.name s.name)} t PT _ STUDENT}
Faloutsos CMU SCS 15-415 #55 Faloutsos CMU SCS 15-415 #56
CMU SCS CMU SCS
Examples cont’d Examples
• (union) get records of both PT and FT • difference: find students that are not staff students { s,n,a | s,n,a FT _ STUDENT s,n,a PT _ STUDENT} RTC: {t | t STUDENT t STAFF}
Faloutsos CMU SCS 15-415 #57 Faloutsos CMU SCS 15-415 #58
CMU SCS CMU SCS
Examples Cartesian product
• difference: find students that are not staff • eg., dog-breeding: MALE x FEMALE • gives all possible couples { s,n,a | s,n,a STUDENT
s,n,a STAFF} MALE FEMALE M.name F.name name x name = spike lassie spike lassie spike shiba spot shiba spot lassie spot shiba
Faloutsos CMU SCS 15-415 #59 Faloutsos CMU SCS 15-415 #60
10 Faloutsos CMU - 15-415
CMU SCS CMU SCS
Cartesian product Cartesian product
• find all the pairs of (male, female) - RTC: • find all the pairs of (male, female) - RDC:
{t | m MALE f FEMALE { m, f | m MALE t.m name m.name f FEMALE} t. f name f .name}
Faloutsos CMU SCS 15-415 #61 Faloutsos CMU SCS 15-415 #62
CMU SCS CMU SCS
‘Proof’ of equivalence Overview - detailed
• rel. algebra <-> rel. domain calculus • rel. domain calculus <-> rel. tuple calculus – why? – details – examples – equivalence with rel. algebra – more examples; ‘safety’ of expressions
Faloutsos CMU SCS 15-415 #63 Faloutsos CMU SCS 15-415 #64
CMU SCS CMU SCS
More examples Reminder: our Mini-U db
• join: find names of students taking 15-415 STUDENT CLASS Ssn Name Address c-id c-name units 123 smith main str 15-413 s.e. 2 234 jones forbes ave 15-412 o.s. 2
TAKES SSN c-id grade 123 15-413 A 234 15-413 B
Faloutsos CMU SCS 15-415 #65 Faloutsos CMU SCS 15-415 #66
11 Faloutsos CMU - 15-415
CMU SCS CMU SCS
More examples More examples
• join: find names of students taking 15-415 - • join: find names of students taking 15-415 - in RTC in RDC {t | s STUDENT eTAKES ( s.ssn e.ssn { n | s a g( s,n,a STUDENT t.name s.name s,15 415, g TAKES)} e.c id 15 415)}
Faloutsos CMU SCS 15-415 #67 Faloutsos CMU SCS 15-415 #68
CMU SCS CMU SCS
Sneak preview of QBE: Sneak preview of QBE:
{ n | s a g( s,n,a STUDENT • very user friendly s,15 415, g TAKES)} • heavily based on RDC • very similar to MS Access interface
STUDENT TAKES Ssn Name Address SSN c-id grade _x P. _x 15-415
Faloutsos CMU SCS 15-415 #69 Faloutsos CMU SCS 15-415 #70
CMU SCS CMU SCS
More examples Reminder: our Mini-U db _x .P _y 2 • 3-way join: find names of students taking a STUDENT CLASS 2-unit course - in RTC: Ssn Name Address c-id c-name units 123 smith main str 15-413 s.e. 2 {t | s STUDENT eTAKES 234 jones forbes ave 15-412 o.s. 2 c CLASS( s.ssn e.ssn join e.c id c.c id TAKES SSN c-id grade t.name s.name projection 123 15-413 A c.units 2)} 234 15-413 B selection _x _y
Faloutsos CMU SCS 15-415 #71 Faloutsos CMU SCS 15-415 #72
12 Faloutsos CMU - 15-415
CMU SCS CMU SCS
More examples More examples
• 3-way join: find names of students taking a • 3-way join: find names of students taking a 2-unit course 2-unit course { n |...... { n | s,a,c, g,cn( s,n,a STUDENT s,n,a STUDENT s,c, g TAKES s,c, g TAKES c,cn,2 CLASS} c,cn,2 CLASS )}
Faloutsos CMU SCS 15-415 #73 Faloutsos CMU SCS 15-415 #74
CMU SCS CMU SCS
Even more examples: Even more examples:
• self -joins: find Tom’s grandparent(s) • self -joins: find Tom’s grandparent(s)
{t | p PC q PC PC p-id c-id ( p.c id q.p id Mary Tom Peter Mary p.p id t.p id John Tom q.c id "Tom")}
Faloutsos CMU SCS 15-415 #75 Faloutsos CMU SCS 15-415 #76
CMU SCS CMU SCS
Even more examples: Even more examples:
• self -joins: find Tom’s grandparent(s) • self -joins: find Tom’s grandparent(s)
{t | p PC q PC { g | p( g, p PC { g | p( g, p PC ( p.c id q.p id p,"Tom"PC)} p.p id t.p id p,"Tom"PC)} q.c id "Tom")}
Faloutsos CMU SCS 15-415 #77 Faloutsos CMU SCS 15-415 #78
13 Faloutsos CMU - 15-415
CMU SCS CMU SCS
Hard examples: DIVISION Hard examples: DIVISION
• find suppliers that shipped all the ABOMB • find suppliers that shipped all the ABOMB parts parts SHIPMENT s# p# ABOMB BAD_S {t | p( p ABOMB ( s1 p1 p# s# s SHIPMENT ( s2 p1 p1 s1 t.s# s.s# s1 p2 p2 s3 p1 s.p# p.p#)))} s5 p3
Faloutsos CMU SCS 15-415 #79 Faloutsos CMU SCS 15-415 #80
CMU SCS CMU SCS
Hard examples: DIVISION More on division
• find suppliers that shipped all the ABOMB • find students that take all the courses that parts ssn=123 does (and maybe even more) { s | p( p ABOMB {o | t((t TAKES t.ssn 123) s, p SHIPMENT)} t1TAKES ( t1.c id t.c id t1.ssn o.ssn) )}
Faloutsos CMU SCS 15-415 #81 Faloutsos CMU SCS 15-415 #82
CMU SCS CMU SCS
More on division Safety of expressions
• find students that take all the courses that • similar to RTC ssn=123 does (and maybe even more) • FORBIDDEN:
{ s,n,a | s,n,a STUDENT} { s | c(g(123,c, g TAKES) g'( s,c, g')TAKES))}
Faloutsos CMU SCS 15-415 #83 Faloutsos CMU SCS 15-415 #84
14 Faloutsos CMU - 15-415
CMU SCS CMU SCS Fun Drill:Your turn … Overview - detailed • Schema: Movie(title, year, studioName) • rel. domain calculus + QBE ActsIn(movieTitle, starName) – dfn Star(name, gender, birthdate, salary) – details – equivalence to rel. algebra
Faloutsos CMU SCS 15-415 #85 Faloutsos CMU SCS 15-415 #86
CMU SCS CMU SCS Your turn … Answers … • Queries to write in TRC: • Find all movies by Paramount studio – 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* {M | MMovie – Stars connected to K. Bacon via any number of films** M.studioName = ‘Paramount’}
* Try two degrees for starters ** Good luck with this one!
Faloutsos CMU SCS 15-415 #87 Faloutsos CMU SCS 15-415 #88
CMU SCS CMU SCS Answers … Answers … • Stars who have been in a film w/Kevin • Movies starring Kevin Bacon Bacon {S | S Star {M | MMovie AActsIn(A.starName = S.name AActsIn(A.movieTitle = M.title A2ActsIn(A2.movieTitle = A.movieTitle A2.starName = ‘Bacon’))} A.starName = ‘Bacon’))} S: name …
A: movie star
‘Bacon’ Faloutsos CMU SCS 15-415 #89 Faloutsos A2: movieCMU SCSstar 15- 415 #90
15 Faloutsos CMU - 15-415
CMU SCS CMU SCS Answers … Two degrees: two • Stars within six degrees of Kevin Bacon S: name …
{S | SStar AActsIn(A.starName = S.name A2ActsIn(A2.movieTitle = A.movieTitle A3ActsIn(A3.starName = A2.starName A4ActsIn(A4.movieTitle = A3.movieTitle A3: movie star A4.starName = ‘Bacon’))} A4: movie star ‘Bacon’
Faloutsos CMU SCS 15-415 #91 Faloutsos CMU SCS 15-415 #92
CMU SCS CMU SCS Two degrees: Answers …
S: name … • Stars connected to K. Bacon via any number of films A: movie star • Sorry … that was a trick question A2: movie star – Not expressible in relational calculus!!
A3: movie star • What about in relational algebra? A4: movie star – We will be able to answer this question shortly … ‘Bacon’
Faloutsos CMU SCS 15-415 #93 Faloutsos CMU SCS 15-415 #94
CMU SCS CMU SCS Expressive Power Question: • Expressive Power (Theorem due to Codd): • Can we express previous query (‘any # – Every query that can be expressed in relational algebra can be expressed as a safe query in DRC / steps’) in relational algebra? TRC; the converse is also true. • A: If we could, then by Codd’s theorem we • Relational Completeness: could also express it in relational calculus. Query language (e.g., SQL) can express every query However, we know the latter is not possible, that is expressible in relational algebra/calculus. so the answer is no. (actually, SQL is more powerful, as we will see…)
Faloutsos CMU SCS 15-415 #95 Faloutsos CMU SCS 15-415 #96
16 Faloutsos CMU - 15-415
CMU SCS CMU SCS
Summary Summary - cnt’d • The relational model has rigorously defined query • Several ways of expressing a given query languages — simple and powerful. – a query optimizer should choose the most efficient • Relational algebra is more operational/procedural version. – useful as internal representation for query evaluation • Algebra and safe calculus have same expressive plans power • Relational calculus is declarative – leads to the notion of relational completeness. – users define queries in terms of what they want, not in terms of how to compute it.
Faloutsos CMU SCS 15-415 #97 Faloutsos CMU SCS 15-415 #98
17