Chapter 3 Tuple and Domain Relational Calculus Tuple Relational Calculus What is Tuple Relational Calculus? It is a non procedural query language: Describes the desired information without giving a specific procedure for obtaining that information. A query in tuple relational calculus is expresses as: {t | P(t)} This represents a set of all tuples t such that predicate P is true for t. Sample Queries Finding the branch – name, loan – number, and amount for loans of over $ 1200 {t | t € loan ٨ t[amount] > 1200} In English this query would mean: The set of tuples t where t belongs to the loan relation and the loan amount for each t is greater than $ 1200. Sample queries(cont….) Finding the loan – number for each loan of an amount greater than $1200. {t | З s € loan (t[loan – number] = s[loan – number] ٨ s[amount > 1200)} In English we would read the preceding statement as “ The set of all tuples t such that there exists a tuple s in relation loan for which the values of t and s for the loan – number attribute are equal, and the value of s for the amount attribute is greater than $1200.” Sample Queries(cont….) The expression: {t | -] r € customer (r[customer – name] = t[customer – name]) ٨ (Џ u Є branch (u[branch – city] = Brooklyn” => Э s Є depositor (t[customer – name] = s[customer – name] ٨ З w Є account (w[account – number- = s[account – number] ٨ w[branch – name] = u[branch – name]))))} In English, would mean “The set of all customers( i.e customer name tuples t) such that for all tuples u in the branch relation, if the value of u on attribute branch – city is Brooklyn, then the customer has an account at the branch whose name appears in the branch name attribute of u” Formal Definition As shown earlier, a tuple relational calculus expression is of the form {t | P(t)} Where P is a formula. A formula may contain several tuple variables. A tuple variable is said to be a free variable unless it is quantified by a З(there exists) or Џ(for all). Tuple variables that are quantified by З or Џ are called bound variables. For ex, in the expression: t Є loan ٨ З s Є customer(t[branch – name] = s[branch – name]) t is a free variable and s is a bound variable. Formal definition(cont….) 1. 2. 3. A formula in relational calculus is made up of atoms. An atom has one of the following forms. S Є r, where s is a tuple variable and r is a relation. S[x] © u[y], where s and u are tuple variables, x is an attribute on which s is defined, y is an attribute on which u is defined, and © is the comparison operator. It is required that x and y have domains that can be compared using ©. S[x] © c, where s is a tuple variable, x is an attribute on which s is defined, © is a comparison operator, and c is a constant in the domain of x. Building a formula We build up a formula from atoms by using the following rules. An atom is a formula. If P1 is a formula, then so are ¬P1 and (P1). If P1 and P2 are formulae, then so are P1 ٧ P2, P1 ٨ P2, and P1 => P2. If P1(s) is a formula containing a free tuple variable s, and r is a relation, then Э s Є r(P1(s)) and Џ s Є (P1(s)) are also formulae Safety of Expressions – The concept of domain A tuple relational calculus may generate an infinite relation. For ex: {t | ¬(t Є loan)} There are infinitely many tuples that are not in loan. To address this issue, we use the concept of domain of tuple relational formula, P. The domain of P, denoted dom(P) , is a set of all value referenced by P.These include values mentioned in P itself, as well as the values that appear in a tuple of a relation mentioned in P. For ex: dom(t Є loan ٨ t[amount] > 1200) is the set containing 1200 as well as set of all values appearing in loan. Domain Relational Calculus This is the second form of relational calculus Uses domain variables that take on values from an attribute domain, rather than values for an entire tuple. Closely related to tuple relational calculus. Formal Definition A general expression in Domain relational calculus is of the form {<x1, x2,…., xn> | P(x1,x2,….,xn)} where x1, x2,….,xn represent domain variables. P represents a formula composed of atoms, as was the case in tuple relational calculus. An atom in Domain Relational Calculus An atom in domain relational calculus has one of the following forms: <x1,x2,….,xn> Є r, where r is a relation on n attributes and x1, x2, ….,xn are domain values or domain constraints. X © y, where x and y are domain values, and © is a comparison operator (<, <=,=,>,>=). We require that attributes x and y have domains that can be compared by ©. X © c, where x is a domain variable, © is a comparison operator, and c is a constant in the domain of the attribute for which x is a domain variable. Example Queries Find the loan number, branch name, and amount for loans over $1200. {<l,b,a> | <l,b,a> Є loan ٨ a > 1200} Find all loan numbers for loans with an amount > 1200: {<l> | Э b,a (<l,b,a> Є loan ٨ a > 1200)} When we write Э b in domain calculus, b refers not to a tuple, but rather to a domain value. The domain of b is unconstrained(unlike as in relational tuple calculus) until the sub formula <l,b,a> Є loan constraints b to branch names that appear in the loan relation. Example queries(cont….) Find the names of all customers who have an account at all branches located in Brooklyn: {<c> | Э n(<c,n> Є customer) ٨ Џ x,y,z (<x,y,z> Є branch ٨ y = “Brooklyn” => Э a,b(<a,x,b> Є account ٨ <c,a> Є depositor))} In English, we interpret this expression as “The set of all(customer name) tuples c such that, for all (branch – name, branch – city, assets) tuples, x,y,z, if the branch city is Brooklyn, then the following is true: 1. There exists a tuple in the relation account with account number a and branch name x. 2. There exists a tuple in the relation depositor with customer c and account number a.” Safety of Expressions As noted in tuple relational calculus expressions in the domain relational calculus may also generate an infinite relation. For ex: {{<l,b,a> | ¬ (<l,b,a> Є loan)} is unsafe, because it allows values in the result that are not in the domain of the expression. Safety(cont….) An expression: {<x1,x2,….,xn> | P(x1,x2,….,xn)} is safe if all of the following hold. All values that appear in tuples of the expression are values from dom(P). For every “there exists” subformula of the form Э x(P1(x)), the sub formula is true only if and only if there is a value x in dom(P1) such that P1(x) is true. For every “for all” subformula of the form Џx(P1(x)), the subformula is true if and only if P1(x) is true for all values of x from dom(P1).