21
Relational Relational Calculus Calculus Ameetinder Singh Ameetinder Singh CS 157A CS 157A

Relational Calculus Ameetinder Singh CS 157A. Tuple Relational Calculus non-procedural query language as compared to relational algebra that is procedural

  • View
    224

  • Download
    1

Embed Size (px)

Citation preview

Relational CalculusRelational Calculus

Ameetinder SinghAmeetinder Singh

CS 157ACS 157A

Tuple Relational CalculusTuple Relational Calculus

non-procedural query language as compared to non-procedural query language as compared to relational algebra that is procedural. relational algebra that is procedural.

only requires the user to specify what only requires the user to specify what information is required and not how that information is required and not how that information should be obtained.information should be obtained.

A query in tuple relational calculus expressed asA query in tuple relational calculus expressed as {t | P(t)}{t | P(t)} i.e. set of all tuples t such that P is true for t i.e. set of all tuples t such that P is true for t

Query Examples Query Examples

To find the To find the branch-name, loan-number, branch-name, loan-number, andand amount amount for for loanloans over $1000:s over $1000:

{t | t {t | t loanloan t[ t[amountamount] > 1000} ] > 1000} To find only the To find only the loan-numberloan-number attribute, rather attribute, rather

than all the attributes of the than all the attributes of the loanloan relation: relation:

{t | {t | s s loanloan ( t ( t[loan-number[loan-number] = s[] = s[loan-loan-numbernumber] ] s[ s[amountamount] > 1000)} ] > 1000)}

This query will retrieve those tuples in This query will retrieve those tuples in loanloan--number such that there is a tuple in number such that there is a tuple in loanloan with with the amount attribute > 1000 the amount attribute > 1000

Examples (Cont’d)Examples (Cont’d)

To find the names of all customers who borrowed a To find the names of all customers who borrowed a loanloan from the Downtown branch. from the Downtown branch.

{t | {t | s s borroweborrower ( t[r ( t[customer-namecustomer-name] = s[] = s[customer-customer-namename] ] u u loanloan (u[ (u[loan-numberloan-number] = s[] = s[loan-numberloan-number] ] u[u[branch-namebranch-name] = “Downtown”))}] = “Downtown”))}

requires two “there exists” clauses since it involves two requires two “there exists” clauses since it involves two relations relations

retrieves the set of all (retrieves the set of all (customer-namecustomer-name) tuples for ) tuples for which the customer has a which the customer has a loanloan from the Downtown from the Downtown branch branch

Examples (Cont’d)Examples (Cont’d)

To find customers who have a To find customers who have a loanloan, an account, or both at the , an account, or both at the bank bank

{t | {t | s s borroweborrower ( t[r ( t[customer-namecustomer-name] = s[] = s[customer-namecustomer-name]) ]) u u depositordepositor (t[ (t[customer-namecustomer-name] = u[] = u[customercustomer--namename] )} ] )}

If a customer has both account and a If a customer has both account and a loanloan from the bank, the from the bank, the customer-name customer-name only appears once only appears once

Changing (Changing () to () to () in the previous query will generate results with ) in the previous query will generate results with the set of customer-name tuples who have both an account and a the set of customer-name tuples who have both an account and a loanloan at the bank. at the bank.

Examples (Cont’d)Examples (Cont’d)

To find all the customers who have an account To find all the customers who have an account at the bank but do not have a at the bank but do not have a loanloan from the from the bank, we use the not (bank, we use the not () symbol in the ) symbol in the previous expression previous expression

{t | {t | s s borroweborrower ( t[r ( t[customer-namecustomer-name] = ] = s[s[customer-namecustomer-name]) ]) u u depositordepositor ( (

t[t[customer-namecustomer-name] = u[] = u[customercustomer--namename] )} ] )}

Examples (Cont’d)Examples (Cont’d)

Some queries require the use of Some queries require the use of implication (implication (). P). PQ means, “if P is true, Q means, “if P is true, then Q must be true.” then Q must be true.”

To find all customers who have an To find all customers who have an account at all branches located in account at all branches located in Brooklyn, we write a query using “for all” Brooklyn, we write a query using “for all” (() construct as follows:) construct as follows:

Examples (Cont’d)Examples (Cont’d)

{t | {t | r r customercustomer ( r[ ( r[customer-namecustomer-name] = t[] = t[customer-customer-namename]) ]) ( ( u u branch (u[branch-city] branch (u[branch-city] = “Brooklyn” = “Brooklyn” s s depositordepositor (t[ (t[customer-namecustomer-name] = s[] = s[customercustomer--namename] ] w w accountaccount (w[ (w[account-numberaccount-number] = ] = s[s[account-numberaccount-number] ] w[ w[branch-namebranch-name] = s[] = s[branch-branch-namename]))))} ]))))}

this is interpreted as “the set of all customers such that, this is interpreted as “the set of all customers such that, for all tuples u in the for all tuples u in the branchbranch relation, if the value of u relation, if the value of u on attribute on attribute branch-citybranch-city is Brooklyn, then the customer is Brooklyn, then the customer has an account at the branch whose name appears in has an account at the branch whose name appears in the the branch-namebranch-name attribute of u.” attribute of u.”

Formal DefinitionFormal Definition

A tuple-relational-calculus expression is A tuple-relational-calculus expression is in the formin the form

{t | P(t)} {t | P(t)}

where P is a formula where P is a formula

The formula in tuple-relational-calculus is The formula in tuple-relational-calculus is built up out of atoms. built up out of atoms.

Formal Definition (Cont’d)Formal Definition (Cont’d)

An atom can have one of the following forms:An atom can have one of the following forms:

• s s r, where s is a tuple variable in relation r r, where s is a tuple variable in relation r

• s[x] s[x] u[y], where s and u are tuple variables, x is an attribute on 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 which s is defined, y is an attribute on which u is defined, and is is a comparison operator (a comparison operator (,,,,,,,,,,); it is required that x and y ); it is required that x and y have domains whose members can be compared by have domains whose members can be compared by ..

• s[x] s[x] c, where s is a tuple variable, x is and attribute on which s is c, where s is a tuple variable, x is and attribute on which s is defined, defined, is a comparison operator, and c is a constant in the is a comparison operator, and c is a constant in the domain of attribute x. domain of attribute x.

Formal Definition (Cont’d)Formal Definition (Cont’d)

Formulae can be built up from atoms by using Formulae can be built up from atoms by using the following rules:the following rules:

• an atom is a formula an atom is a formula

• If P1 is a formula, then so are If P1 is a formula, then so are P1 and (P1) P1 and (P1)

• If P1 and P2 are formulae, then so are P1 If P1 and P2 are formulae, then so are P1 P2, P1P2, P1 P2, and P1 P2, and P1P2P2

Safety of ExpressionsSafety of Expressions

A tuple-relational-calculus expression may generate an A tuple-relational-calculus expression may generate an infinite relation. infinite relation.

{t | {t | ( t ( t loanloan)} )}

there are infinitely many tuples that are not in there are infinitely many tuples that are not in loanloan. .

Domain of P is the set of all values referenced by P. Domain of P is the set of all values referenced by P. the expression {t | P(t)} is safe if all values in the result the expression {t | P(t)} is safe if all values in the result are in the domain of P are in the domain of P

Domain Relational CalculusDomain Relational Calculus

Closely related to tuple-relational-Closely related to tuple-relational-calculus calculus

Uses domain variables that take on Uses domain variables that take on values from an attributes domain, rather values from an attributes domain, rather than values for an entire tuple than values for an entire tuple

Formal DefinitionFormal Definition

An expression in the domain relational calculus An expression in the domain relational calculus is in the formis in the form

{<x1 , x2 ,……. xn> | P(x1 , x2 ,……. xn)}{<x1 , x2 ,……. xn> | P(x1 , x2 ,……. xn)}

where x1 , x2 ,……. xn represent domain where x1 , x2 ,……. xn represent domain variables. P represents formula composed of variables. P represents formula composed of atoms.atoms.

Formal Definition (Cont’d)Formal Definition (Cont’d)

An atom in the domain relational calculus has one of An atom in the domain relational calculus has one of the following forms:the following forms:

• <x1 , x2 ,……. xn> <x1 , x2 ,……. xn> r, where r is a relation on n r, where r is a relation on n attributes and x1 , x2 ,…xn are domain variables. attributes and x1 , x2 ,…xn are domain variables.

• x x y, where x and y are domain variables and y, where x and y are domain variables and is a is a comparison operator (comparison operator (,,,,,,,,,,); it is required that ); it is required that x and y have domains that can be compared by x and y have domains that can be compared by

• x x c, where x is a domain variable, c, where x is a domain variable, is a comparison is a comparison operator, and c is a constant in the domain of attribute operator, and c is a constant in the domain of attribute for which x is a domain variable for which x is a domain variable

Formal Definition (Cont’d)Formal Definition (Cont’d)

Formulae can be built up from atoms by Formulae can be built up from atoms by using the following rules:using the following rules:

an atom is a formula.an atom is a formula. If P1 is a formula, then so are If P1 is a formula, then so are P1 and P1 and

(P1)(P1) If P1 and P2 are formulae, then so are If P1 and P2 are formulae, then so are

P1 P1 P2, P1 P2, P1 P2, and P1 P2, and P1P2P2Same as tuple-relational-calculusSame as tuple-relational-calculus

Example queriesExample queries

To find the To find the branch-name, loan-number, branch-name, loan-number, andand amount amount for for loanloans over $1000:s over $1000:

{<l,b,a> | <l,b,a> {<l,b,a> | <l,b,a> loanloan a > 1000} a > 1000}

To find only the To find only the loan-numberloan-number attribute, rather attribute, rather than all the attributes of the than all the attributes of the loanloan relation: relation:

{< l > | {< l > | b,a (<l,b,a> b,a (<l,b,a> loanloan a > 1000)} a > 1000)}

Examples (Cont’d)Examples (Cont’d)

To find the names of all customers who To find the names of all customers who borrowed a borrowed a loanloan from the Downtown branch from the Downtown branch and find the loan amount:and find the loan amount:{<c,a> | {<c,a> | l (<c,l> l (<c,l> borroweborrower r b (<l,b,a> b (<l,b,a> loanloan b = “Downtown”))} b = “Downtown”))}

To find all customers who have an account at To find all customers who have an account at all branches located in Brooklyn:all branches located in Brooklyn:{<c> | {<c> | n (<c,n> n (<c,n> customer)customer) x,y,z(<x,y,z> x,y,z(<x,y,z> branch branch y = “Brooklyn” y = “Brooklyn” a,b (<a,x,b> a,b (<a,x,b> accountaccount <c,a> <c,a> depositordepositor)})}

Examples (Cont’d)Examples (Cont’d)

interpreted the set of all (interpreted the set of all (customer name) customer name) tuples c such that, for all (tuples c such that, for all (branch-name, branch-name, branch-city, assetsbranch-city, assets) tuples x, y, z, if the ) tuples x, y, z, if the branch-branch-citycity is Brooklyn, then the following conditions is Brooklyn, then the following conditions hold:hold:

There exists a tuple in the relation There exists a tuple in the relation accountaccount with with account number a and branch name xaccount number a and branch name x

There exists a tuple in the relation There exists a tuple in the relation depositor depositor with customer c and account number awith customer c and account number a

Safety of expressionsSafety of expressions

Expressions may generate an infinite relation Expressions may generate an infinite relation

To overcome this problem we define safety of To overcome this problem we define safety of domain relational calculus expressions. domain relational calculus expressions.

An expression such asAn expression such as

{<l,b,a> | {<l,b,a> | <l,b,a> <l,b,a> loanloan )} )}

generates result with values that are not in the generates result with values that are not in the domain of the expression. domain of the expression.

Safety of expressions Safety of expressions (Cont’d)(Cont’d)

An expression {<x1 , x2 ,…xn> | P(x1 , x2 ,…. xn)} is An expression {<x1 , x2 ,…xn> | P(x1 , x2 ,…. xn)} is safe if all of the following conditions hold:safe if all of the following conditions hold:

all values that appear in tuples of the expression are all values that appear in tuples of the expression are values from domain of P.values from domain of P.

for every “there exists” sub formula of the form for every “there exists” sub formula of the form x (P1 x (P1 (x)) , the subformula is true if and only if there is a (x)) , the subformula is true if and only if there is a value x in the domain P1 such that P1 (x) is true. value x in the domain P1 such that P1 (x) is true.

for every “for all” sub formula of the form for every “for all” sub formula of the form x (P1 (x)) , x (P1 (x)) , the subformula is true if and only if P1 (x) is true for all the subformula is true if and only if P1 (x) is true for all values x from domain of P1 values x from domain of P1