Professional Documents
Culture Documents
Chap06 - Relational Calculus
Chap06 - Relational Calculus
Relational Calculus
Content
¡ Introduction
¡ Tuple Relational Calculus (TRC)
¡ Domain Relational Calculus (DRC)
¡ Properties
- Nonprocedural language – declarative language
• Calculus expression specifies what is to be retrieved rather
than how to retrieve
- One declarative expression to specify a retrieval request
• There is no description of how to evaluate query
- A calculus expression may be written in different way
• The way it is written has no bearing on how a query should be
evaluated
Introduction2DB - FIT - HCMUS 3
Introduction
¡ Categories
- Tuple relational calculus – TRC
• SQL
- t is a tuple variable
• Its value is any individual tuple from a relation
• t.A is a value of a tuple t at an attribute A
P(t) P(t)
- t Î EMPLOYEE : TRUE
• If t is an instance of relation EMPLOYEE
- t.SALARY > 30000 : TRUE
• If the attribute SALARY of tuple t has a value being larger than
30000
¡ The result is all tuples t which satisfy:
- t Î EMPLOYEE and t.SALARY > 30000
Introduction2DB - FIT - HCMUS 7
Example 2
¡ Retrieve the SSN and first name of employees
whose salary is larger than 30000
$t Î R (Q(t))
Existing a tuple t of the relation R such that the expression Q(t) is TRUE à the result of the existential
quantifier is TRUE
Introduction2DB - FIT - HCMUS 9
Example 3
¡ Find employees (SSN) who work for the department
Nghien cuu
{ t.SSN | t Î EMPLOYEE Ù
$s Î DEPARTMENT (
s.DNAME = Nghien cuu Ù
s.DNUMBER = t.DNO ) }
Q(s)
{ t.FNAME | t Î EMPLOYEE Ù (
{ t.FNAME | t Î EMPLOYEE Ù (
{ t.FNAME | t Î EMPLOYEE Ù
"t Î R (Q(t))
"s Î PROJECT (
X
s.DNUM = 4 Ù ( $u Î WORKS_ON (
u.PNO = s.PNUMBER Ù
u.ESSN = t.SSN ))) }
PÞQ
If P then Q
"s Î PROJECT (
s.DNUM = 4 Þ ( $u Î WORKS_ON (
u.PNO = s.PNUMBER Ù
u.ESSN = t.SSN ))) }
"s Î PROJECT (
s.DNUM ¹ 4 Ú ( $u Î WORKS_ON (
u.PNO = s.PNUMBER Ù
u.ESSN = t.SSN ))) }
t is a free variable
¡ Bound variable
¡ Formula (i) t Î R
- TRUE value if t is a tuple of the specified relation R
- FALSE value if t does not belong to R
R A B C
a 10 1 t1 = <a, 10, 1> t1 Î R has the TRUE value
R A B C
a 10 1
a 20 1
- $t Î R (P(t)) is a formula
• TRUE when there exists some tuple that makes P(t) TRUE
• FALSE when P(t) is FALSE for all tuples t in R
¡ (iv) P Þ Q = ¬P Ú Q
¡ Unsafe
- Many tuples in the universe that are not EMPLOYEE
tuples
- Even though they do not exist in the database
- The result is infinitely numerous
- The result
• The set of values such that when assigned to variables xi, they
make P TRUE
{ r, s | $x (
<p, q, r, s, t, u, v, x, y, z> Î EMPLOYEE Ù
x > 30000 ) }
{ s | $z (
<p, q, r, s, t, u, v, x, y, z> Î EMPLOYEE Ù
$a, b ( <a, b, c, d> Î DEPARTMENT Ù
a = Nghien cuu Ù b = z )) }
{ p, r, s | $s (
<p, q, r, s, t, u, v, x, y, z> Î EMPLOYEE Ù
¬$a ( <a, b, c, d, e> Î DEPENDENT Ù a = s )) }
¡ (ii) xqy
- x, y are domain variables
- Domains of x and y are identical
- q is comparison operators, eg. < , > , £ , ³ , ¹ , =
¡ (iii) xqc
- c is a constant
- x is a domain variable
- q is comparison operators, eg. < , > , £ , ³ , ¹ , =
Introduction2DB - FIT - HCMUS 40
Discussion
¡ Atoms evaluate to either TRUE or FALSE for a set
of values
- Called the truth values of the atoms
Formula 1 Formula 2
is safe if :
- Values that appear in tuples of the expression must
belong to the domain of P