
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 STUDENT t.name s.name)} Ssn Name Address students 123 smith main str Name234 jones forbes ave t aaaa …. jones … s zzzz 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 • join: find names of students taking 15-415 {t | s STUDENT eTAKES ( s.ssn e.ssn t.name s.name e.c id 15 415)} Faloutsos CMU SCS 15-415 #27 Faloutsos CMU SCS 15-415 #28 CMU SCS CMU SCS More examples More examples STUDENT• join: find names of studentsCLASS taking 15-415 • 3-way join: find names of students taking a Ssn Name Address c-id c-name units 2-unit course 123{t | smiths STUDENTmain str 15-413 s.e. 2 234 jones forbes ave 15-412 o.s. 2 eTAKES ( s.ssn e.ssn join t.nameTAKES s.name projection e.c SSNid 15c-id 415grade)} 123 15-413 A selection 234 15-413 B 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 • 3-way join: find names of students taking a 2-unit course {t | s STUDENT eTAKES c CLASS( s.ssn e.ssn join e.c id c.c id t.name s.name projection 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 STUDENT• self -joins: find Tom’s grandparent(s)CLASS • find suppliers that shipped all the ABOMB Ssn Name Address c-id c-name units parts 123 smith main str 15-413 s.e. 2 SHIPMENT 234 jones forbes ave 15-412 o.s. 2 {t | p PC q PC s# p# ABOMB BAD_S s1 p1 p# s# ( p.c id q.p id s2 p1 TAKES p1 s1 SSNp.p idc-id t.p gradeid s1 p2 p2 123 15-413 A s3 p1 q.c 234id15-413"TomB")} 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 opposed to tuple variables, eg: s,n,a STUDENT ssn 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 STUDENT• (selection) find student recordCLASS with ssn=123 • (projection) find name of student with Ssn Name Address c-id c-name units ssn=123 {123123smith,n,a |main123 str,n,a 15-413STUDENTs.e.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages17 Page
-
File Size-