The Relational Calculus Chapter Outline l Relational Calculus

  • Slides: 40
Download presentation
The Relational Calculus

The Relational Calculus

Chapter Outline l Relational Calculus v. Tuple Relational Calculus v. Domain Relational Calculus Slide

Chapter Outline l Relational Calculus v. Tuple Relational Calculus v. Domain Relational Calculus Slide 6 b-2

Relational Calculus l A relational calculus expression: vcreates a new relation, vwhich is specified

Relational Calculus l A relational calculus expression: vcreates a new relation, vwhich is specified in terms of variables lthat range over rows of the stored database relations (in tuple calculus) lor over columns of the stored relations (in domain calculus). Slide 6 b-3

Relational Calculus l In a calculus expression, there is no order of operations to

Relational Calculus l In a calculus expression, there is no order of operations to specify how to retrieve the query result va calculus expression specifies only what information the result should contain. v. This is the main distinguishing feature between relational algebra and relational calculus. l Relational calculus is considered to be a nonprocedural language. v. This differs from relational algebra, where we must write a sequence of operations to specify a retrieval request; hence relational algebra can be considered as a procedural way of stating a query. Slide 6 b-4

Tuple Relational Calculus l The tuple relational calculus is based on specifying a number

Tuple Relational Calculus l The tuple relational calculus is based on specifying a number of tuple variables. l Each tuple variable usually ranges over a particular database relation, meaning that the variable may take as its value any individual tuple from that relation. l A simple tuple relational calculus query is of the form {t | COND(t)} where t is a tuple variable and COND (t) is a conditional expression involving t. The result of such a query is the set of all tuples t that satisfy COND (t). Slide 6 b-5

Tuple Relational Calculus Example: To find the first and last names of all employees

Tuple Relational Calculus Example: To find the first and last names of all employees whose salary is above $50, 000, we can write the following tuple calculus expression: {t. FNAME, t. LNAME | EMPLOYEE(t) AND t. SALARY>50000} The condition EMPLOYEE(t) specifies that the range relation of tuple variable t is EMPLOYEE. The first and last name (PROJECTION FNAME, LNAME) of each EMPLOYEE tuple t that satisfies the condition t. SALARY>50000 (SELECTION retrieved. SALARY >50000) will be Slide 6 b-6

Rule l General expression: {t 1. Aj…tn. Am|COND(t 1…tn)} l COND is condition or

Rule l General expression: {t 1. Aj…tn. Am|COND(t 1…tn)} l COND is condition or FORMULA l Every atom is Formula l If F 1 and F 2 are a Formula, F 1 AND F 2, F 1 OR F 2, NOT F 1, NOT F 2 also Formula Slide 6 b-7

The Existential and Universal Quantifiers l Two special symbols called quantifiers can appear in

The Existential and Universal Quantifiers l Two special symbols called quantifiers can appear in formulas; these are the universal quantifier ( ) and the existential quantifier ( ). l Informally, a tuple variable t is bound if it is quantified, meaning that it appears in an ( t) or ( t) clause; otherwise, it is free. l If F is a formula, then so is ( t)(F), where t is a tuple variable. The formula ( t)(F) is true if the formula F evaluates to true for some (at least one) tuple assigned to free occurrences of t in F; otherwise ( t)(F) is false. Slide 6 b-8

The Existential and Universal Quantifiers l l If F is a formula, then so

The Existential and Universal Quantifiers l l If F is a formula, then so is ( t)(F), where t is a tuple variable. The formula ( t)(F) is true if the formula F evaluates to true for every tuple (in the universe) assigned to free occurrences of t in F; otherwise ( t)(F) is false. It is called the universal or “for all” quantifier because every tuple in “the universe of” tuples must make F true to make the quantified formula true. Slide 6 b-9

Example Query Using Existential Quantifier l Retrieve the name and address of all employees

Example Query Using Existential Quantifier l Retrieve the name and address of all employees who work for the ‘Research’ department. FNAME, LNAME, ADDRESS( Dname=‘Research’(DEPARTMENT Dnumber=Dno (EMPLOYEE)) Query : {t. FNAME, t. LNAME, t. ADDRESS | EMPLOYEE(t) and ( d) (DEPARTMENT(d) and d. DNAME=‘Research’ and d. DNUMBER=t. DNO) } Slide 6 b-10

l The only free tuple variables in a relational calculus expression should be those

l The only free tuple variables in a relational calculus expression should be those that appear to the left of the bar ( | ). In previous query, t is the only free variable; it is then bound successively to each tuple. If a tuple satisfies the conditions specified in the query, the attributes FNAME, LNAME, and ADDRESS are retrieved for each such tuple. l The conditions EMPLOYEE (t) and DEPARTMENT(d) specify the range relations for t and d. The condition d. DNAME = ‘Research’ is a selection condition and corresponds to a SELECT operation in the relational algebra, whereas the condition d. DNUMBER = t. DNO is a JOIN condition. Slide 6 b-11

Example Query Using Existential Quantifier l Find the names of employees who works on

Example Query Using Existential Quantifier l Find the names of employees who works on some projects controlled by department number 5. Q 3: {e. LNAME, e. FNAME|EMPLOYEE(e) and (( x) ( w)(PROJECT(x) AND (WORKS_ON(w) AND x. DNUM=5 AND w. ESSN = e. SSN and x. PNUMBER = w. PNO))} Slide 6 b-12

Example Query Using Universal Quantifier l Find the names of employees who works on

Example Query Using Universal Quantifier l Find the names of employees who works on all projects controlled by department number 5. Q 3: {e. LNAME, e. FNAME|EMPLOYEE(e) and (( x)(not(PROJECT(x)) or (not(x. DNUM = 5) or (( w)(WORKS_ON(w) and w. ESSN = e. SSN and x. PNUMBER = w. PNO)))))} Slide 6 b-13

l Basic component of the previous query: v. Q: {e. LNAME, e. FNAME|EMPLOYEE(e) and

l Basic component of the previous query: v. Q: {e. LNAME, e. FNAME|EMPLOYEE(e) and F’} v. F’ = ( x)(not(PROJECT(x)) or F 1) v. F 1 = (not(x. DNUM = 5) or F 2) v. F 2 = ( w)(WORKS_ON(w) and w. ESSN = e. SSN and x. PNUMBER = w. PNO) l Must exclude all tuples not of interest from the universal quantification by making the condition TRUE for all such tuples Slide 6 b-14

l Universally quantified variable x must evaluate to TRUE for every possible tuple in

l Universally quantified variable x must evaluate to TRUE for every possible tuple in the universe l In F’, not(PROJECT(x)) makes x TRUE for all tuples not in the relation of interest “PROJECT” l In F 1, not(x. DNUM = 5) makes x TRUE for those PROJECT tuples we are not interested in “whose DNUM is not 5” l F 2 specifies the condition that must hold on all remaining tuples “all PROJECT tuples controlled by department 5” Slide 6 b-15

Safe Expression l Safe expressions in the relational calculus: v. Is guaranteed to yield

Safe Expression l Safe expressions in the relational calculus: v. Is guaranteed to yield a finite number of tuples as its result v. Unsafe expression may yield infinate number of tuples, and the tuples may be of different types. Example: {t|not(EMPLOYEE(t))} v. Yields all non-EMPLOYEE tuples in the universe v. Following the rules for Q discussed above guarantees safe expressions when using universal quantifiers Slide 6 b-16

l Using transformations from universal to existential quantifiers, can rephrase Q as Q’: v.

l Using transformations from universal to existential quantifiers, can rephrase Q as Q’: v. Q’: {e. LNAME, e. FNAME|EMPLOYEE(e) and (not( x)(PROJECT(x) and (x. DNUM = 5) and (not( w)(WORKS_ON(w) and w. ESSN = e. SSN and x. PNUMBER = w. PNO))))} Slide 6 b-17

Transforming Universal and Existential Quantifier l Well-known transformations from mathematical logic v( x)(P(x)) (not

Transforming Universal and Existential Quantifier l Well-known transformations from mathematical logic v( x)(P(x)) (not x)(not (P(x))) v( x)(P(x)) not ( x)(not(P(x))) v( x)(P(x) and Q(x)) (not x)(not(P(x)) or not (Q(x))) v( x)(P(x) or Q(x)) (not x)(not(P(x)) and not (Q(x))) v( x)(P(x) or Q(x)) not( x)(not(P(x)) and not (Q(x))) v( x)(P(x) and Q(x)) not( x)(not(P(x)) or not (Q(x))) Slide 6 b-18

Transforming Universal and Existential Quantifier l The following is also true, where stands for

Transforming Universal and Existential Quantifier l The following is also true, where stands for implies: v( x)(P(x)) v(not x)(P(x)) not ( x)(P(x)) l The following is not true: vnot( x)(P(x)) (not x)(P(x)) Slide 6 b-19

Another Example Query 6: Find the names of employees without any dependents Q 6:

Another Example Query 6: Find the names of employees without any dependents Q 6: {e. FNAME, e. LNAME|EMPLOYEE(e) and (not( d)(DEPENDENT(d) and e. SSN = d. ESSN))} Slide 6 b-20

Another Example Q 6: {e. FNAME, e. LNAME|EMPLOYEE(e ) and (not( d)(DEPENDENT(d ) and

Another Example Q 6: {e. FNAME, e. LNAME|EMPLOYEE(e ) and (not( d)(DEPENDENT(d ) and e. SSN = d. ESSN))} l Transforming Q 6 into Q 6’ to use universal quantifier v. Q 6’: {e. FNAME, e. LNAME|EMPLOYEE(e) and (( d)(not(DEPENDENT(d)) or not (e. SSN = d. ESSN)))} Slide 6 b-21

l Query 7: List the names of managers who have at least one dependent

l Query 7: List the names of managers who have at least one dependent Q 7: {e. FNAME, e. LNAME|EMPLOYEE(e) and (( d)( p)(DEPARTMENT(d) and DEPENDENT(p) and e. SSN = d. MGRSSN and p. ESSN = e. SSN))} Slide 6 b-22

Languages Based on Tuple Relational Calculus l The language SQL is based on tuple

Languages Based on Tuple Relational Calculus l The language SQL is based on tuple calculus. It uses the basic SELECT <list of attributes> FROM <list of relations> WHERE <conditions> block structure to express the queries in tuple calculus where the SELECT clause mentions the attributes being projected, the FROM clause mentions the relations needed in the query, and the WHERE clause mentions the selection as well as the join conditions. SQL syntax is expanded further to accommodate other operations. Slide 6 b-23

Languages Based on Tuple Relational Calculus l Another language which is based on tuple

Languages Based on Tuple Relational Calculus l Another language which is based on tuple calculus is QUEL which actually uses the range variables as in tuple calculus. Its syntax includes: RANGE OF <variable name> IS <relation name> Then it uses RETRIEVE <list of attributes from range variables> WHERE <conditions> This language was proposed in the relational DBMS INGRES. Slide 6 b-24

The Domain Relational Calculus l Another variation of relational calculus called the domain relational

The Domain Relational Calculus l Another variation of relational calculus called the domain relational calculus, or simply, domain calculus is equivalent to tuple calculus and to relational algebra. l The language called QBE (Query-By-Example) that is related to domain calculus was developed almost concurrently to SQL at IBM Research, Yorktown Heights, New York. v. Domain calculus was thought of as a way to explain what QBE does. Slide 6 b-25

The Domain Relational Calculus l Domain calculus differs from tuple calculus in the type

The Domain Relational Calculus l Domain calculus differs from tuple calculus in the type of variables used in formulas: v rather than having variables range over tuples, the variables range over single values from domains of attributes. v To form a relation of degree n for a query result, we must have n of these domain variables—one for each attribute. l An expression of the domain calculus is of the form {x 1, x 2, . . . , xn | COND(x 1, x 2, . . . , xn+1, xn+2, . . . , xn+m)} where x 1, x 2, . . . , xn+1, xn+2, . . . , xn+m are domain variables that range over domains (of attributes) and COND is a condition or formula of the domain relational calculus. Slide 6 b-26

Example Query Using Domain Calculus l Retrieve the birthdate and address of the employee

Example Query Using Domain Calculus l Retrieve the birthdate and address of the employee whose name is ‘John B. Smith’. Query : {uv | ( q) ( r) ( s) ( t) ( w) ( x) ( y) ( z) (EMPLOYEE(qrstuvwxyz) and q=’John’ and r=’B’ and s=’Smith’)} l Ten variables for the employee relation are needed, one to range over the domain of each attribute in order. Of the ten variables q, r, s, . . . , z, only u and v are free. l Specify the requested attributes, BDATE and ADDRESS, by the free domain variables u for BDATE and v for ADDRESS. l Specify the condition for selecting a tuple following the bar ( | )— namely, that the sequence of values assigned to the variables qrstuvwxyz be a tuple of the employee relation and that the values for q (FNAME), r (MINIT), and s (LNAME) be ‘John’, ‘B’, and ‘Smith’, respectively. Slide 6 b-27

Another Examples l Retrieve the name and address of all employess who work for

Another Examples l Retrieve the name and address of all employess who work for the ‘Research’ department Query : {qsv | ( z) ( l) ( m) (EMPLOYEE(qrstuvwxyz) and DEPARTMENT (lmno) and l=‘Research’ and m=z)} At the query, m=z is a join condition, whereas l=‘Research’ is a selection condition. Slide 6 b-28

Another Examples l Find the names of employees who have no dependent {qs |

Another Examples l Find the names of employees who have no dependent {qs | ( t) (EMPLOYEE(qrstuvwxyz) and (not( l) (DEPENDENT (lmnop) and t=l)))} Slide 6 b-29

QBE: A Query By Example l The language QBE (Query by Example) is a

QBE: A Query By Example l The language QBE (Query by Example) is a query language based on domain Calculus l The first graphical query language l This language is based on the idea of giving an example of a query using example elements. l Recently, be used on metadata, xml Slide 6 b-30

Graphical Query l For every project located in Stafford, list project number, the controlling

Graphical Query l For every project located in Stafford, list project number, the controlling dept num, dept mng’s last name, address and bdate E. Lname, E. address, E. Bdate P. Pnum, P. Dnum E D P P. Dnum=D. Dnumber P. Mgr_ssn=E. Ssn P. Plocation=Stafford Slide 6 b-31

Slide 6 b-32

Slide 6 b-32

Slide 6 b-33

Slide 6 b-33

Slide 6 b-34

Slide 6 b-34

Slide 6 b-35

Slide 6 b-35

Slide 6 b-36

Slide 6 b-36

Slide 6 b-37

Slide 6 b-37

Slide 6 b-38

Slide 6 b-38

Slide 6 b-39

Slide 6 b-39

Exercises l Specify queries (a), (b), (c), (e), (f), (i), and (j) of the

Exercises l Specify queries (a), (b), (c), (e), (f), (i), and (j) of the Exercise of previous topic (relational algebra) in both the tuple relational calculus and the domain relational calculus. Slide 6 b-40