From Relational Calculus to Relational Algebra

Slides:



Advertisements
Similar presentations
Relational Calculus and Datalog
Advertisements

10 October 2006 Foundations of Logic and Constraint Programming 1 Unification ­An overview Need for Unification Ranked alfabeths and terms. Substitutions.
CSE 636 Data Integration Conjunctive Queries Containment Mappings / Canonical Databases Slides by Jeffrey D. Ullman.
CPSC 504: Data Management Discussion on Chandra&Merlin 1977 Laks V.S. Lakshmanan Dept. of CS UBC.
Chapter 3 Tuple and Domain Relational Calculus. Tuple Relational Calculus.
1 541: Relational Calculus. 2 Relational Calculus  Comes in two flavours: Tuple relational calculus (TRC) and Domain relational calculus (DRC).  Calculus.
A Semantic Characterization of Unbounded-Nondeterministic Abstract State Machines Andreas Glausch and Wolfgang Reisig 1.
Relational Calculus Ameetinder Singh CS 157A. Tuple Relational Calculus  non-procedural query language as compared to relational algebra that is procedural.
SPRING 2004CENG 3521 E-R Diagram for the Banking Enterprise.
Monadic Predicate Logic is Decidable Boolos et al, Computability and Logic (textbook, 4 th Ed.)
EE1J2 - Slide 1 EE1J2 – Discrete Maths Lecture 6 Limitations of propositional logic Introduction to predicate logic Symbols, terms and formulae, Parse.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Relational Calculus CS 186, Fall 2003, Lecture 6 R&G, Chapter 4   We will occasionally use this arrow notation unless there is danger of no confusion.
Rutgers University Relational Calculus 198:541 Rutgers University.
CS848: Topics in Databases: Foundations of Query Optimization Topics Covered  Databases  QL  Query containment  More on QL.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
The Relational Model: Relational Calculus
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Section 4.3.
1 CS 430 Database Theory Winter 2005 Lecture 6: Relational Calculus.
The Recursion Theorem Pages 217– ADVANCED TOPICS IN C O M P U T A B I L I T Y THEORY.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Chapter 2 Mathematical preliminaries 2.1 Set, Relation and Functions 2.2 Proof Methods 2.3 Logarithms 2.4 Floor and Ceiling Functions 2.5 Factorial and.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4.
Advanced Topics in Propositional Logic Chapter 17 Language, Proof and Logic.
CS344: Introduction to Artificial Intelligence Lecture: Herbrand’s Theorem Proving satisfiability of logic formulae using semantic trees (from Symbolic.
CS621: Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 28– Interpretation; Herbrand Interpertation 30 th Sept, 2010.
1 Relational Algebra and Calculas Chapter 4, Part A.
From Relational Algebra to SQL CS 157B Enrique Tang.
CS848: Topics in Databases: Information Integration Topics covered  Databases  QL  Query containment  An evaluation of QL.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Module A: Formal Relational.
1 First order theories (Chapter 1, Sections 1.4 – 1.5) From the slides for the book “Decision procedures” by D.Kroening and O.Strichman.
272: Software Engineering Fall 2012 Instructor: Tevfik Bultan Lecture 9: Test Generation from Models.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4, Part B.
Database System Concepts, 6 th Ed. Chapter 6: Formal Relational Query Languages.
Section 8.4 Mathematical Induction. Mathematical Induction In this section we are going to perform a type of mathematical proof called mathematical induction.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
E v a f i c o u g r á v i d a e, a o d a r à l u z u m m e n i n o, d i s s e : " O S e n h o r m e d e u u m f i l h o h o m e m ". E l a d e u à c.
Mathematical Induction. The Principle of Mathematical Induction Let S n be a statement involving the positive integer n. If 1.S 1 is true, and 2.the truth.
CS589 Principles of DB Systems Fall 2008 Lecture 4c: Query Language Equivalence Lois Delcambre
CS589 Principles of DB Systems Fall 2008 Lecture 4b: Domain Independence and Safety Lois Delcambre
CENG 424-Logic for CS Introduction Based on the Lecture Notes of Konstantin Korovin, Valentin Goranko, Russel and Norvig, and Michael Genesereth.
Chapter 1 Logic and Proof.
Relational Calculus Chapter 4, Section 4.3.
CSE202 Database Management Systems
Chapter (6) The Relational Algebra and Relational Calculus Objectives
A First Course in Database Systems
Relational Calculus Chapter 4, Part B
Advanced Algorithms Analysis and Design
Goal for this lecture Demonstrate how we can prove that one query language is more expressive than (i.e., “contained in” as described in the book) another.
Chapter 6: Formal Relational Query Languages
The Relational Algebra and Relational Calculus
Relational Database Models
Chapter 6: Formal Relational Query Languages
Decomposition of relational schemes
Relational Data Base Design in Practice
Chapter 2: Intro to Relational Model
Safety Properties by David Kjerrumgaard
Mathematical Induction
Chapter 6: Formal Relational Query Languages
Sullivan Algebra and Trigonometry: Section 13.4
Advanced Analysis of Algorithms
Chapter 11: Further Topics in Algebra
Copyright © Cengage Learning. All rights reserved.
Chapter 27: Formal-Relational Query Languages
Relational Calculus Chapter 4, Part B 7/1/2019.
Copyright © Cengage Learning. All rights reserved.
Representations & Reasoning Systems (RRS) (2.2)
CS589 Principles of DB Systems Fall 2008 Lecture 4e: Logic (Model-theoretic view of a DB) Lois Delcambre
CSE544 Wednesday, March 29, 2006.
CS589 Principles of DB Systems Fall 2008 Lecture 4b: Domain Independence and Safety Lois Delcambre
Presentation transcript:

From Relational Calculus to Relational Algebra Tuple relational calculus, domain relational calculus, and relational algebra 5/15/2019 CS319 Theory of Databases

Domain Relational Calculus 1 .... reminder Predicate Calculus query languages ... a query = finding values satisfying predicate Two kinds of predicate calculus language primitive objects “tuples”  tuple relational calculus primitive objects “domain values”  domain relational calculus 5/15/2019 CS319 Theory of Databases

Domain Relational Calculus 2 Form of relational calculus expressions Formula is built up using operators of FOPC from atomic clauses of three types: 1. R(s1 s2 … sk), where R is relation name, and each si is domain variable 2. si q uj, where s and u are domain variables, and q is an arithmetic comparison operator (such as <, = etc) 3. si q c, where s is domain variable, c is a constant 5/15/2019 CS319 Theory of Databases

Domain Relational Calculus 3 Semantics of the atomic clauses: 1. R(s1s2 …sk), where R is relation name, and each si is domain variable 2. si q uj, where s and u are domain variables, and q is an arithmetic comparison operator (such as <, = etc) 3. si q c, where s is domain variable, c is a constant 1. “s1s2… sk represents a tuple in R” 2. “domain value represented by si is in relationq to the domain value represented by uj” 3. “domain value represented by si is in relationq to the constant c” 5/15/2019 CS319 Theory of Databases

From tuple to domain relational calculus 1 The definition of safety is expressed in terms of constraints on component values of tuples ... so generalises directly to domain relational calculus Theorem 2: For every safe tuple relational calculus expression there is an equivalent safe domain relational calculus expression. Omit formal proof: essentially a syntactic transformation involving substitution for tuple variables 5/15/2019 CS319 Theory of Databases

From tuple to domain relational calculus 2 An illustrative example Can express RCS in tuple relational calculus as: { w | ($u)($v)(R(u) Ù S(v) Ù f(u,v)) } where f(u,v) º (u[2]=v[1] Ù w[1]=u[1] Ù w[2]=v[2]) in domain relational calculus, this becomes: { w1w2 | ($u1)($u2)($v1)($v2) (R(u1u2) Ù S(v1v2) Ù F(u1,u2,v1,v2)) } where F(u1,u2,v1,v2) º (u2=v1 Ù w1=u1 Ù w2=v2) 5/15/2019 CS319 Theory of Databases

From domain relational calculus to algebra 1 Theorem 3: For every safe expression in domain relational calculus, there is an equivalent relational algebra expression. Proof (sketch only) Use induction on number of operators in y to construct an algebraic expression for { t1t2 ... tn | y(t1, t2, ..., tn) } To simplify the induction, begin with two lemmas: there is a relational algebra exp to represent dom(y) don’t need to consider  and  as independent cases 5/15/2019 CS319 Theory of Databases

From domain relational calculus to algebra 2 Use induction on number of operators in y to construct an algebraic expression for { t1t2 ... tn | y(t1, t2, ..., tn) } By safety, enough to show: for each subformula w of y of the form { t1t2 ... tm | w(t1, t2, ..., tm) } $ a relational algebra expression E whose value is dom(y)* Ç { t1t2 ... tm | w(t1, t2, ..., tm) } where dom(y)* = set of tuples with compts in dom(y) i.e. can restrict attention to tuples in dom(y)* 5/15/2019 CS319 Theory of Databases

From domain relational calculus to algebra 3 Lemma A: If y is any formula in domain relational calculus, there is a relational algebra expression to represent the unary relation dom(y) Note: unary relation º set of 1-tuples º set Proof: Suppose R has arity k. Let D(R) º P1(R) È P2(R) È ... È Pk(R). dom(y) is the union of all D(R)'s over relations R referred to in y together with the set of all constants {a1, a2, ..., an} referred to in y. Thus can take D as an algebraic expression: D = R referred to in y D(R) È {a1, a2, ..., an} 5/15/2019 CS319 Theory of Databases

From domain relational calculus to algebra 4 Lemma B: If y is any formula in domain relational calculus, there is a formula y' in domain relational calculus with no occurrences of Ù or " such that { t1t2 ... tn | y(t1, t2, ..., tn) } and { t1t2 ... tn | y'(t1, t2, ..., tn) } are equivalent. This transformation respects safety. Proof: Wherever the operators Ù and " appear in y: replace f Ù r by (f  r). replace ("v)(f(v)) by ($v)(f(v)). Need to show that safety is preserved ... 5/15/2019 CS319 Theory of Databases

From domain relational calculus to algebra 5 Proof of Lemma B: Wherever the operators Ù and " appear in y: replace f Ù r by (f  r). replace ("v)(f(v)) by ($v)(f(v)). To show that safety is preserved ... Observe that dom(y) = dom(y'): this takes care of the first safety condition. Note also that if ("v)(f(v)) safe v Ï dom(f)* Þ f(v) true Þ f(v) false Hence ($v)(f(v)) is also safe 5/15/2019 CS319 Theory of Databases

From domain relational calculus to algebra 6 Proof of Theorem 3 (cont.) Consider relation defined by { t1t2...tn | y(t1, t2, ..., tn) } where y is a safe relational calculus expression By Lemmata: can assume neither Ù or " occurs in y enough to show for each subformula w of y of form { t1t2...tm | w(t1, t2, ..., tm) } $ a relational algebra expression E whose value is dom(y)* Ç { t1t2...tm | w(t1, t2, ..., tm)} where dom(y)* = set of tuples with compts in dom(y) Prove this by induction on the number of operators in y. 5/15/2019 CS319 Theory of Databases

From domain relational calculus to algebra 7 I.e. prove for all subformulae w of y  in particular for y itself  by induction on number of operators N in w. N=0: consider the relation defined by dom(y)* Ç { t1t2 ... tm | w(t1, t2, ..., tm) } where w is an atomic formula. Let D be relational algebra expression for dom(y). Two cases: 1. w(ti, tj) = ti q tj or w(ti) = ti q c where q is an arithmetic comparison operator 2. w(t1, t2, ..., tm) = R(ti(1)ti(2) ... ti(k)) 5/15/2019 CS319 Theory of Databases

From domain relational calculus to algebra 8 Proof of Theorem 3: Base of induction (cont.) 1. w(ti, tj) = ti q tj or w(ti) = ti q c where q is an arithmetic comparison operator 2. w(t1, t2, ..., tm) = R(ti(1)ti(2) ... ti(k)) For case 1: use expression E º siqj(D´D). For case 2: have w(t1, t2, ..., tm) = R (ti(1)ti(2) ... ti(k)) By safety, every index r, where 1rm, must be an index i(j) for some j. Define algebraic expression E º Õj(1), j(2), ..., j(m)(sC(R)) where C is conjunction of relations r=s over pairs (r,s) such that i(r)=i(s) and j(r) is an index such that i(j(r))=r. 5/15/2019 CS319 Theory of Databases

From domain relational calculus to algebra 9 Illustrative example for Case 2: Take domain relational calculus expression { t1t2t3 | R(t3t2t1t2) } Consider indices j for which i(j) = r: this defines a pattern j=1 j=2 j=3 j=4 r=1: • r=2: • • r=3: • Suitable expression is E º Õ3, 4,1(s2=4(R)) 5/15/2019 CS319 Theory of Databases

From domain relational calculus to algebra 10 Proof of Thm 3: The induction step Three cases to consider in the induction step ... Assume form of w(t1, t2, ..., tm) is 1. f(u1, u2, ..., up) Ú r(v1, v2, ..., vr) 2. f(t1, t2, ..., tm) 3. ($t) (f(t1, t2, ..., tm, t)) 5/15/2019 CS319 Theory of Databases

From domain relational calculus to algebra 11 Proof of Thm 3: The induction step Case 1: Assume form of w(t1, t2, ..., tm) is f(u1, u2, ..., up) Ú r(v1, v2, ..., vr) Can assume (by safety) the variables u1, u2, ..., up, v1, v2, ..., vr include all the variables t1, t2, ..., tm variables in { u1, u2, ..., up } are distinct variables in { v1, v2, ..., vr } are distinct 5/15/2019 CS319 Theory of Databases

From domain relational calculus to algebra 12 Proof of Thm 3: The induction step for operator Ú Illustrative example shows principle w(t1, t2, ..., tm)  f(u1, u2, ..., up) Ú r(v1, v2, ..., vr) where, in particular case of m =4, p=3, r=2: w(t1, t2, t3, t4) º f(t1, t3, t4) Ú r(t2, t4) Let F and G be relational algebra expressions for { t1t2t3 | f(t1, t2, t3) } and { t1t2 | r(t1, t2) } respectively .... Need to write down a relational algebra expression for dom(y)* Ç { t1t2t3t4 | w(t1, t2, t3, t4) } which is also dom(y)* Ç { t1t2t3t4 | f(t1, t3, t4) Ú r(t2, t4) } ... to do this, must use expression D for dom(y) 5/15/2019 CS319 Theory of Databases

From domain relational calculus to algebra 13 Proof of Thm 3: The induction step for operator Ú (cont.) Need a relational algebra expression for dom(y)* Ç { t1t2t3t4 | f(t1, t3, t4) Ú r(t2, t4) } Set of tuples t1t2t3t4 satisfying f(t1, t3, t4) is constrained so that t1t3t4 is a tuple in the relation defined by algebraic expression F. If D is the algebraic expression for dom(y) then F´D defines tuples t1t3t4t2 satisfying f(t1, t3, t4) within dom(y)*. Hence Õ1, 4, 2, 3(F´D) defines tuples t1t2t3t4 satisfying f(t1, t3, t4) within dom(y)*. 5/15/2019 CS319 Theory of Databases

From domain relational calculus to algebra 14 Proof of Thm 3: The induction step for operator Ú (cont.) Need a relational algebra expression for dom(y)* Ç { t1t2t3t4 | f(t1, t3, t4) Ú r(t2, t4) } Õ1, 4, 2, 3(F´D) defines tuples t1t2t3t4 satisfying f(t1, t3, t4) in dom(y)*. Similarly G ´ D ´ D defines tuples t2t4t1t3 satisfying r(t2, t4) within dom(y)* and Õ3, 1, 4, 2(G ´ D ´ D) defines tuples t1t2t3t4 satisfying r(t2, t4) within dom(y)*. Hence can take E º Õ1, 4, 2, 3(F´D)ÈÕ3, 1, 4, 2(G´D´D) 5/15/2019 CS319 Theory of Databases

From domain relational calculus to algebra 15 Case 2: w(t1, t2, ..., tm) is f(t1, t2, ..., tm) If F is an algebraic expression for dom(y)* Ç { t1t2 ... tm | f(t1, t2, ..., tm) } and D is an algebraic expression for dom(y) then D ´ D ´ ... ´ D - F m times represents the relation dom(y)* - { t1t2 ... tm | f(t1, t2, ..., tm) } = dom(y)* - (dom(y)* - {t1t2 ... tm | f(t1, t2, ..., tm) }) = dom(y)* Ç { t1t2 ... tm | f(t1, t2, ..., tm) } 5/15/2019 CS319 Theory of Databases

From domain relational calculus to algebra 16 Case 3: w(t1, t2, ..., tm) is (t) (f(t1, t2, ..., tm, t)) By induction, have an algebraic expression F for dom(y)* Ç { t1t2 ... tmtm+1 | f(t1, t2, ..., tm, tm+1) } Since y is safe: t satisfies f(t1, t2, ..., tm, t) Þ t Î dom(y)* Hence Õ1, 2, ..., m(F) represents the required relation: dom(y)* Ç { t1t2 ... tm | (t) (f(t1, t2, ..., tm, t)) } 5/15/2019 CS319 Theory of Databases

From domain relational calculus to algebra 17 Have proved the equivalence of relational algebra and domain / tuple relational calculus ... Theorems 1, 2 and 3 together prove relational algebra domain relational calculus tuple relational calculus all have the same expressive power. Thus A query language is complete if and only if it has the expressive power of one of these formalisms. 5/15/2019 CS319 Theory of Databases

To follow … Mathematical foundations and features of SQL 5/15/2019 CS319 Theory of Databases