0% found this document useful (0 votes)
17 views4 pages

4 Tuple Relational Calculus

Relational Calculus (RC) is derived from first order predicate calculus and includes tuple relational calculus (TRC) and domain relational calculus (DRC), with TRC being the focus here. TRC expressions are defined using tuple variables and formulas that can include free and bound variables, allowing for operations such as union, difference, and selection on relations. The document provides examples of TRC expressions for various relational algebra operations, illustrating how to express queries in TRC format.

Uploaded by

Turjo Sarker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

4 Tuple Relational Calculus

Relational Calculus (RC) is derived from first order predicate calculus and includes tuple relational calculus (TRC) and domain relational calculus (DRC), with TRC being the focus here. TRC expressions are defined using tuple variables and formulas that can include free and bound variables, allowing for operations such as union, difference, and selection on relations. The document provides examples of TRC expressions for various relational algebra operations, illustrating how to express queries in TRC format.

Uploaded by

Turjo Sarker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Relational Calculus (RC) does not imply any connection with the branch of mathematics

usually called ‘Calculus’; rather RC comes from the first order predicate calculus from
the field of Logic. There are two types of RCs – tuple relational calculus (TRC) and
domain relational calculus (DRC). Here the tuple relational calculus is discussed.

In TRC, expressions are of the form {t / (t)}, where t is a tuple variable, i.e. a variable
denoting a tuple of some fixed length and  is a formula built from atoms and a
collection of operators to be defined.

The atoms of formulas  are of 3 types


:

i. R(s), where R is a relation name and s is a tuple variable. This atom stands for
the assertion that s is a tuple in relation R.

ii. s[i]  u[j], where u and s are tuple variables and  is an arithmetic comparison
operator (<, >, =). This atom stands for the assertion that ith component of s
stands in relation  to the jth component of u.

iii. s[i]  a or a  s[i] are same as above; only difference is that a is a constant.

Free and Bound variable : Informally, an occurrence of a variable in a formula is


‘bound’ if that variable has been introduced by a ‘for all’ or ‘there exists’ quantifier, and
we say that the variable is ‘free’ if not.

The notion of a ‘free variable’ is analogous to that of a global variable in a programming


language i.e. a variable defined outside the current procedure. On the other hand, a
‘bound variable’ is like a local variable, one that is defined in the procedure at hand and
can’t be referenced from the outside.

Formulas and free and bound occurrences of tuple variable in these formulas are defined
recursively as follows:

i. Every atom is a formula. All occurrences of tuple variables mentioned in the


atom are free in this formula.

ii. If 1 and 2 are formulas, then 1  2 , 1  2 and -1 are formulas


asserting ‘1 and 2 are both true’, ‘1 or 2 or both true’ and ‘1 is not
true’ respectively. Occurrences of tuple variables are free or bound in 1  2
, 1  2 and -1 as they are free or bound in 1 or 2 depending on where
they occur. Note that an occurrence of a variable s could be bound in 1 while
another occurrence of s is free in 2 or conversely.

iii. If  is a formula then (s)( ) is a formula. The symbol  is a quantifier. The


only other quantifier used is , ‘for all’ described in iv. Below. Occurrences
of s that are free in  are bound to (s) in (s)( ). The formula (s)( )
asserts that there exists a value of s such that when we substitute this value for
all free occurrences of s in , the formula  becomes true. eg. (s)(R(s)) says
that relation R is not empty i.e. there exists a tuple s in R.

iv. If  is a formula then (s)( ) is a formula. Free occurrences of s in  are


bound to (s) in (s)( ). The formula (s)( ) asserts that whatever value
of the appropriate arity we substitute for free occurrences of s in , the
formula  becomes true.

v. Parenthesis may be placed around formulas as needed. We assume the order


of precedence is : arithmetic comparison operators highest, then the
quantifiers  and , then , , and  in that order.

vi. Nothing else is a formula.

 is psi.
Recall {t/ (t)} is a tuple relational calculus expression where t is the only free variable.

Representation of five basic algebraic operators in TRC :

Union : {t/R(t)  S(t)}  The set of tuples t such that t is in R or is in S

Union only makes sense if R and S have the same arity. The variable t is assumed to have
some fixed length.

Difference : {t/ R(t)   S(t)}

Cartesian Product : {t(r+s)/ (u(r))(v(s))(R(u)  S(v) t[1] = u[1]  . . . t[r] = u[r]


t[r+1] = v[1]  . . .  t[r+s] = v[s])}

It means R X S is the set of tuples t of length (r+s) such that there exists u and v, with u
in R, v in S, the first r components of t form u and the next s components of t form v.

Projection : {t(k) / (u)(R(u)  t[1] = u[i1]  . . .  t[k] = u[ik])}

The equivalent RA expression is --  i1, i2, . . . ik (R)

Selection : The selection F (R) is expressed by {t/ R(t)  F} where F is the formula F
with each operand I denoting the ith component replaced by t[I]

Example Schema 1:

TEACHER (tname,dept,tel_no, sub_title)


STUDENT (sname, course, hall)
STUDY (sub_title, sname, status, marks)

1. R.A :  tel_no, sub_title (tname = ‘XYZ’ (TEACHER))

TRC : {t2/ ( u (u  TEACHER  t [tel_no] = u [tel_no]  t[sub_title] = u[sub_title]


 u[tname] = ‘XYZ’)}

2. R.A :  sname (sub_title = ‘DBMS’ (STUDY))

TRC : {t1/ (u)(u  STUDY  t[sname] = u[sname]  u[sub_title] = ‘DBMS’)}

3. R.A. :  sname, marks (course = ‘IEP’  sub_title = ‘DBMS’  status = ‘elective’ (STUDENT X STUDY))

TRC : {t2/ u ( u STUDENT  t[sname] = u[sname]  u[course] = ‘IEP’) 


v ( v STUDY  v[sname] = u[sname]  t[marks] = v[marks] 
v[sub_title] = ‘DBMS’  v[status] = ‘elective’)}

4. R.A. :  sname, hall (STUDENT X marks < 35 (STUDY))

TRC : { t2 / u ( u STUDENT  t[sname] = u[sname]  t[hall] = u[hall]


v ( v STUDY  v[sname] = u[sname]  v[marks] < 35)}

Example Schema 2 :

EMP (e_no, e_name, sal)


WORKS_IN (e_no, dname, duration)
DEPT (dname, budget, floor)

1. Find all information about all overpaid employees

TRC : { e / e  EMP  e.sal >10,500}

2. Find names and salaries of all overpaid employees

TRC : {t2 / (e) ( e EMP)  t.sal > 10,500  t. e_name = e.e_name  t.sal = e.sal}

3. Find names of overpaid CS department employees

TRC : {t1/ u  EMP ( u.sal > 10,500  t.ename = u.ename  w  WORKS_IN (


( w.eno = u.eno  w. dname = ‘CS’))}

4. Find names of employees who work in all departments

TRC : {t1/ u  EMP ( t.e_name = u.e_name  d  DEPT (w  WORKS_IN


(w.e_no = u.e_no w.dname = d.dname))}

5. Find names of employees who work in CS department but not in Maths


department.

TRC : {t1/ u  EMP ( t.e_name = u.e_name  w  WORKS_IN (w.dname = ‘CS’


w. e_no = u.e_no)  NOT v  WORKS_IN (v.e_no = u.e_no  v.dname =
‘MATHS’))}

You might also like