
WEEK 4 EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 1 Relational Calculus Chapter 7 EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 2 1 Query Languages Ease of Comprehension QBE SQL Algebr a FOP C Power of Language EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 3 Relational Calculus v Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC). v Calculus has variables, constants, comparison ops, logical connectives and quantifiers. – TRC: Variables range over (i.e., get bound to) tuples. – DRC: Variables range over domain elements (= field values). – Both TRC and DRC are simple subsets of first-order logic. v Expressions in the calculus are called formulas. An answer tuple is essentially an assignment of constants to variables that make the formula evaluate to true. EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 4 2 Relational Calculus v Propositional logic v Predicate Calculus ® Relational Calculus X: It is raining today Y: Picnic is cancelled – X Ú Y – X ® Y IF X then Y Logical implication EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 5 Relational Calculus v X, Y Symbols (propositional) v Ø Ù Ú ® Connectives (according to precedence rule) <exp>::= <prop symbol> X|Y|Z|R ::= <constant> T|F ::= Ø(exp) ::= (<exp> < binary op > <exp>) Ú|Ù|®|« EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 6 3 Some Rules of Inference 1) Modus Ponens Rule If X and (X ® Y) then Y (X and (X ® Y)) ®Y 2) Disjunctive Syllogism X ® X Ú Y 3) Resolution (can be proved using truth table) Ø X Ú Y º X ® Y EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 7 Predicate with multiple variables Relations are predicates AGE (name, number) SUP (S#,Sname,Status,City) EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 8 4 Quantifiers 1) Universal Quantifier (") – (a) Every supplier has a unique S# – (b) Every supplier has a unique name. 2) Individual ( Existential ) $ there exists a member with some property. ( " S# ) ( $ Sname ) so that S# ® Sname EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 9 Tuple Relational Calculus v Query has the form: {t | P(t) } where t is a tuple variable, it ranges over some relation v Answer includes all tuples that make the formula (predicate) P be true. Example { t | t Î S Ù t.city = ‘London’} Answer: Find all tuples of S staisfying the above predicate. EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 10 5 Tuple Relational Calculus v Formula is recursively defined, starting with simple atomic formulas (getting tuples from relations or making comparisons of values), and building bigger and better formulas using the logical connectives. EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 11 TRC Formulas v Atomic formula: – t Î R where t is a tuple variable and R is a relation – X op Y, or X op constant, here op is one of< ,>,=,£,³,¹ v Formula: – an atomic formula, or – Ø p, pÙ q, pÚq, p ® q where p and q are formulas – $X ( p(X)) , where variable X is tuple variable – "X ( p(X)), v The use of quantifiers $ X and " X is said to bind X. – A variable that is not bound is free. EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 12 6 TRC Formulas 1) {t | P(t) } Set of all tuples for which predicate P is true t = tuple variable, it ranges over some relation 2) $ t Q(t) There exists a tuple t such that predicate Q(t) is true. 3) " t Q(t) Q(t) is true for all tuple t EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 13 Range definition Range of Sx is S (Sx is variable for tuple of S) Example: Give S# of suppliers who are located in LA and have status = 3 Range of Sx is S (Sx.S#) WHERE Sx.city = CITY(‘LA’) AND Sx.Status = Status(‘3’) EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 14 7 Some valid Formulas v NOT ( Sx.CITY = ‘London’ ) v Sx.S# = SPy.S# AND SPy.P# != Pz.P# v $ SPx ( SPx.S# = Sx.S# AND SPx.P# = ‘P2’) EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 15 Equivalence Rules Let F(x) and G(x) be the formulas (predicates) 1) "x F = Ø$x ØF 2) "x ØF = Ø$x F 3) Ø"x F = $x ØF 4) "x F Ú G = "x [ F Ú G ] 5) "x F Ù G = "x [ F Ù G ] 6) $x F Ú G = $x [ F Ú G ] 7) $x F Ù G = $x [ F Ù G ] 8) "x [ F Ù G ] = "x F Ù "x G 9) $x [ F Ú G ] = $x F Ú $x G 10) Ø( "x F(x) ) = $x ØF(x) 11) Ø($x F(x) ) = "x ØF(x) EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 16 8 Quantifiers and Operators A = { x(1) , x(2) } Domain B = { y(1) , y(2) } Domain 1) "x H(x) = ?? "x H(x) = H( x(1) ) Ù H ( x(2) ) 2) $y G(y) = G( y(1) ) Ú G ( y(2) ) 3) $y "x F(x,y) =? "x $y F(x,y) EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 17 Multiple Operators Ex: F(x,y) is true if item x is sold by dept y. $y ( "x F(x,y) ) º there exists a department that sells all items º For all items, there exists a dept. which sells all of them. "x ($y F(x,y) ) º For every item, there exists a dept. y so that F(x,y) EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 18 9 Multiple Operators Example: SUP(x,y,u,v) predicate Example: Every class held on M at time ‘t’ hour is also held on W and F at the same hour. "x "y Stu-Sch ( x , y , d , t ) where x = name y = class d = day t = time Stu-Sch ( x , y , W , t ) AND Stu-Sch ( x , y , M , t ) AND Stu-Sch ( x , y , F , t ) EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 19 Example Query with two Relations Give S# for suppliers who supply red parts Range of Sx is S Range of SPx is SP Range of Px is P (SPx.S#) where $ Px Px.P# = SPx.P# AND Px.COLOR = COLOR (‘red’) EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 20 10 Example Query with two Relations Give S# for suppliers who supply at least one red part. (Sx.SNAME) WHERE $ SPx (Sx.S# = SPx.S# AND $Px (SPx.P# = Px.P# AND Px.COLOR = COLOR (‘red’ ) ) ) EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 21 Example Query with two Relations Prefix normal form in which all quantifiers appear at the front of the formula. Alternate:- Sx.SNAME where $SPx $Px ( Sx.S# = SPx.S# AND SPx.P# = Px.P# AND Px.COLOR = COLOR (‘red’) ) EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 22 11 Example Query Give supplier names who supply all parts How can we use $ and " quantifiers? EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 23 Example Query Give supplier names who supply all parts. How can we solve without using $ and "quantifiers? EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 24 12 Example Query Give S# for suppliers who supply at least those parts supplied by supplier S2. Use: IF f THEN g ( Øf OR g ) EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 25 Domain Relational Calculus v Query has the form: ì æ öü ï ç ÷ï í x1,x2,...,xn | pç x1,x2,...,xn ÷ý ç ÷ ï è øï î þ v Answer includes all tuples x 1 , x 2 , . , xn that make æ ö the formula (predicate) pç x1,x2,...,xn ÷ be true. ç ÷ è ø v Formula is recursively defined, starting with simple atomic formulas (getting tuples from relations or making comparisons of values), and building bigger and better formulas using the logical connectives. EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 26 13 DRC Formulas v Atomic formula: – x1,x2,...,xn ÎRname , or X op Y, or X op constant – op is one of <,>,=,£,³,¹ v Formula: – an atomic formula, or – Ø p, pÙ q, pÚq, where p and q are formulas, or – $X ( p(X)) , where variable X is free in p(X), or – "X ( p(X)), where variable X is free in p(X) – membership condition. R (pair, pair,…) EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 27 Example of membership condition v SP (S#:S#(‘S1’), P#:P#)’P1)) – condition is true iff there exists a shipment tuple with S# value S1 and P# value P1. v SP (S#:SX, P#:PX)) – condition is true iff there exists a shipment tuple with S# value equal to the current range variable SX and P# value equal to the current value of range variable PX. EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 28 14 Unsafe Queries, Expressive Power v It is possible to write syntactically correct calculus queries that have an infinite number of answers! Such queries are called unsafe. – e.g., { t | Ø (t Î S)} v It is known that every query that can be expressed in relational algebra can be expressed as a safe query in DRC / TRC; the converse is also true.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages15 Page
-
File Size-