Relational Calculus

Relational Calculus

Relational Calculus Dr Paolo Guagliardo University of Edinburgh Fall 2016 First-order logic term t := x (variable) j c (constant) j f(t1; : : : ; tn) (function application) formula ' := P (t1; : : : ; tn) j t1 op t2 with op 2 f=; 6=; >; <; >; 6g j '1 ^ '2 j '1 _ '2 j :' j '1 ! '2 j 9x ' j 8x ' if x 2 free(') free(') = f variables that are not in the scope of any quantifier g Notation: we write 9x19x2 · · · 9xn ' as 9x1; : : : ; xn ' 1 / 24 Relational calculus A relational calculus query is an expression of the form fx¯ j 'g where the set of variables in x¯ is free(') Examples I Q = x; y j 9z R(x; z) ^ S(z; y) I Q = y; x j 9z R(x; z) ^ S(z; y) I Q = fx; x j 8y R(x; y)g Queries without free variables are called Boolean queries Examples I Q = f() j 8x R(x; x)g I Q = f() j 8x 9y R(x; y)g 2 / 24 Q2: Name and age of customers having an account in London y; z j 9x Customer(x; y; z) ^ 9w Account(w; `London'; x) Q3: ID of customers who have an account in every branch x j 9y; z Customer(x; y; z) ^ 8u; w; v Account(u; w; v) ! 9u0 Account(u0; w; x) Examples Customer : ID, Name, Age Account : Number, Branch, CustID Q1: Name of customers younger than 33 or older than 50 y j 9x; z Customer(x; y; z) ^ (z < 33 _ z > 50) 3 / 24 Q1: Name of customers younger than 33 or older than 50 y j 9x; z Customer(x; y; z) ^ (z < 33 _ z > 50) Q3: ID of customers who have an account in every branch x j 9y; z Customer(x; y; z) ^ 8u; w; v Account(u; w; v) ! 9u0 Account(u0; w; x) Examples Customer : ID, Name, Age Account : Number, Branch, CustID Q2: Name and age of customers having an account in London y; z j 9x Customer(x; y; z) ^ 9w Account(w; `London'; x) 3 / 24 Q1: Name of customers younger than 33 or older than 50 y j 9x; z Customer(x; y; z) ^ (z < 33 _ z > 50) Q2: Name and age of customers having an account in London y; z j 9x Customer(x; y; z) ^ 9w Account(w; `London'; x) Examples Customer : ID, Name, Age Account : Number, Branch, CustID Q3: ID of customers who have an account in every branch x j 9y; z Customer(x; y; z) ^ 8u; w; v Account(u; w; v) ! 9u0 Account(u0; w; x) 3 / 24 Standard Name Assumption (SNA) Every constant is intepreted as itself: cI = c Interpretations First-order structure I ∆ non-empty domain of objects (universe) ·I gives meaning to constant/function/relation symbols cI 2 ∆ f I : ∆n ! ∆ RI ⊆ ∆n 4 / 24 Interpretations First-order structure I ∆ non-empty domain of objects (universe) ·I gives meaning to constant/function/relation symbols cI 2 ∆ f I : ∆n ! ∆ RI ⊆ ∆n Standard Name Assumption (SNA) Every constant is intepreted as itself: cI = c 4 / 24 The answer to a Boolean query is either f()g (true) or ? (false) Answers to queries I Fix an underlying domain ∆ under SNA I Fix an underlying interpretation of function symbols =) \interpretations" are just databases Recall: an assignment α maps variables to objects in ∆ The answer to a query Q = fx¯ j 'g on a database D is Q(D) = α(¯x) j α: free(') ! ∆ such that D; α j= ' 5 / 24 Answers to queries I Fix an underlying domain ∆ under SNA I Fix an underlying interpretation of function symbols =) \interpretations" are just databases Recall: an assignment α maps variables to objects in ∆ The answer to a query Q = fx¯ j 'g on a database D is Q(D) = α(¯x) j α: free(') ! ∆ such that D; α j= ' The answer to a Boolean query is either f()g (true) or ? (false) 5 / 24 Bad news Whether a relational calculus query is safe is undecidable Safety A query is safe if it gives a finite answer on all databases Examples of unsafe queries: I x j :R(x) I x; y j R(x) _ R(y) I x; y j x = y Question: Are Boolean queries safe? 6 / 24 Safety A query is safe if it gives a finite answer on all databases Examples of unsafe queries: I x j :R(x) I x; y j R(x) _ R(y) I x; y j x = y Question: Are Boolean queries safe? Bad news Whether a relational calculus query is safe is undecidable 6 / 24 Active domain Adom(R) = f all constants occuring in R g Example 0 R AB 1 Adom@ a1 b1 A = a1; b1; b2 a1 b2 The active domain of a database D is [ Adom(D) = Adom(R) R2D 7 / 24 Active domain and safety For a safe query Q, we have that AdomQ(D) ⊆ Adom(D) Active domain semantics Evaluate queries within Adom(D)= ) safe relational calculus Q(D) = f α(¯x) j α: free(') ! Adom(D) s.t. D; α j= ' For each α: free(') ! Adom(D) (there are finitely many) output α(¯x) whenever D; α j= ' 8 / 24 Algebra ≡ Safe calculus Fundamental theorem of database theory: Relational algebra and Safe relational calculus equally expressive I For every query in safe relational calculus there exists an equivalent query in relational algebra I For every query in relational algebra there exists an equivalent query in safe relational calculus 9 / 24 From algebra to calculus Translate each RA expression E into a FOL formula ' Assumption: the attributes of a relation are ordered (R over A; B; C means the 1st column is A, the 2nd is B, the 3rd is C) Environment η Maps each attribute A in the schema to a variable xA 10 / 24 Example If R is a base relation over A; B η = f A 7! xA;B 7! xB;::: g then R is translated to R(xA; xB) From algebra to calculus Base relation R over A1;:::;An is translated to R η(A1); : : : ; η(An) 11 / 24 From algebra to calculus Base relation R over A1;:::;An is translated to R η(A1); : : : ; η(An) Example If R is a base relation over A; B η = f A 7! xA;B 7! xB;::: g then R is translated to R(xA; xB) 11 / 24 Example If R is a base relation over A; B then ρA!B ρB!C (R) is translated to R(xB; xC ) From algebra to calculus Renaming ρA!B(E) 1. Translate E to ' 2. If there is no mapping for B in η, add fB 7! xBg 3. Replace every occurrence of η(A) in ' by η(B) 12 / 24 From algebra to calculus Renaming ρA!B(E) 1. Translate E to ' 2. If there is no mapping for B in η, add fB 7! xBg 3. Replace every occurrence of η(A) in ' by η(B) Example If R is a base relation over A; B then ρA!B ρB!C (R) is translated to R(xB; xC ) 12 / 24 From algebra to calculus Projection πα(E) is translated to 9X' where I ' is the translation of E I X = free(') − η(α) (attributes that are not projected become quantified) Example If R is a base relation over A; B then πA(R) is translated into 9xB R(xA; xB) 13 / 24 From algebra to calculus Selection σθ(E) is translated to ' ^ η(θ) where I ' is the translation of E I η(θ) is obtained from θ by replacing each attribute A by η(A) Example If R is a base relation over A; B then σA=B(R) is translated into R(xA; xB) ^ xA = xB 14 / 24 From algebra to calculus Cartesian Product, Union, Difference Product E1 × E2 is translated to '1 ^ '2 Union E1 [ E2 is translated to '1 _ '2 Difference E1 − E2 is translated to '1 ^ :'2 where I '1 is the translation of E1 I '2 is the translation of E2 15 / 24 Blackboard time! 9x4 Customer(x1; x2) ^ Account(x3; x4) ^ x1 = x4 Example Customer : CustID, Name Account : Number, CustID Environment η = f CustID 7! x1; Name 7! x2; Number 7! x3 g How do we translate Customer ./ Account? 16 / 24 9x4 Customer(x1; x2) ^ Account(x3; x4) ^ x1 = x4 Example Customer : CustID, Name Account : Number, CustID Environment η = f CustID 7! x1; Name 7! x2; Number 7! x3 g How do we translate Customer ./ Account? Blackboard time! 16 / 24 Example Customer : CustID, Name Account : Number, CustID Environment η = f CustID 7! x1; Name 7! x2; Number 7! x3 g How do we translate Customer ./ Account? Blackboard time! 9x4 Customer(x1; x2) ^ Account(x3; x4) ^ x1 = x4 16 / 24 Active domain in relational algebra For R over attributes A1;:::;An Adom(R) = ρA1!A πA1 (R) [···[ ρAn!A πAn (R) S Adom(D) = R2D Adom(R) AdomA is the relation Adom(D) over attribute A 17 / 24 From calculus to algebra Translate each FOL formula ' into an RA expression E Assumptions (without loss of generality) I No universal quantifiers, implications, double negations I No distinct pair of quantifiers binds the same variable I No variable occurs both free and bound I No variable is repeated within a predicate I No constants in predicates I No atoms of the form x op x or c1 op c2 Environment η Maps each variable x to an attribute Ax 18 / 24 From calculus to algebra Let R be over attributes A1;:::;An Predicate R(x1; : : : ; xn) is translated to ρA1!η(x1);:::;An!η(xn)(R) Example For R over attributes A; B; C, R(x; y; z) is translated into ρA!Ax;B!Ay;C!Az (R) 19 / 24 From calculus to algebra Existential quantification 9x ' is translated to πη(X−{xg)(E) where I E is the translation of ' I X = free(') Example For ' with free variables x; y; z and translation E, 9y ' is translated to πAx;Az (E) 20 / 24 From calculus to algebra Comparisons x op y is translated to ση(x) op η(y) Adomη(x) × Adomη(y) x op c is translated to ση(x) op c Adomη(x) Example x = y is translated to σAx=Ay AdomAx × AdomAy x > 1 is translated to σAx>1 AdomAx 21 / 24 From calculus to algebra Negation :' is translated into ×Adomη(x) − E x2free(') where E is the translation of ' Example For ' with free variables x; y and translation E :' is translated to AdomAx × AdomAy −E 22 / 24 From calculus to algebra Disjunction: '1 _ '2 is translated to E1 × ×Adomη(x) [ E2 × ×Adomη(x) x2X2−X1 x2X1−X2 where, for i 2 f1; 2g, I Ei is the translation of 'i I Xi = free('i) Conjunction: same as disjunction, but use \ instead of [ 23 / 24 Environment η = f x1 7! A; x2 7! B; x3 7! C; x4 7! D g πA;B;C E1 × AdomC × AdomD \ AdomA × AdomB × E2 \ σA=D(AdomA × AdomD) × AdomB × AdomC where I E1 = ρ CustID!A; Name!B(Customer) I E2 = ρ Number!C; CustID!D(Account) Example Customer : CustID, Name Account : Number, CustID Translate 9x4 Customer(x1; x2) ^ Account(x3; x4) ^ x1 = x4 24 / 24 πA;B;C E1 × AdomC × AdomD \ AdomA × AdomB × E2 \ σA=D(AdomA × AdomD) × AdomB × AdomC where I E1 = ρ CustID!A; Name!B(Customer) I E2 = ρ Number!C; CustID!D(Account) Example Customer : CustID, Name Account : Number, CustID Translate 9x4 Customer(x1; x2) ^ Account(x3; x4) ^ x1 = x4 Environment η = f x1 7! A; x2 7! B; x3 7! C; x4 7! D g 24 / 24 Example Customer : CustID, Name Account : Number, CustID Translate 9x4 Customer(x1; x2) ^ Account(x3; x4) ^ x1 = x4 Environment η = f x1 7! A; x2 7! B; x3 7! C; x4 7! D g πA;B;C E1 × AdomC × AdomD \ AdomA × AdomB × E2 \ σA=D(AdomA × AdomD) × AdomB × AdomC where I E1 = ρ CustID!A; Name!B(Customer) I E2 = ρ Number!C; CustID!D(Account) 24 / 24.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    36 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us