You are on page 1of 41

Group 8

Relational
Calculus

Nguyễn Lê Hoàng Nhân

Nguyễn Phước Nguyên Phúc
Nguyễn Trần Minh Phước
What is Relational Calculus ?
Non-procedural query language

Relational Calculus means what result we have to obtain.

It explains what to do but not how to do.

Based on Predicate Calculus


Type of Relational Calculus

Tuple Domain
Relational Relational
Calculus Calculus
TUPLE RELATIONAL
CALCULUS

Based on specifying a number of tuple variables


It describes the desired information without giving a specific a number of tuple variables.

In Tuple Calculus, a query is expressed as :


{t| Condition(t)}
t = tuples variables,
Condition (t) = conditional (Boolean) expression involving t

-For examble to find all employees whose salary is above $50,000:


{t | EMPLOYEE(t) AND t.Salary>50000}

-Each EMPLOYEE tuple t that satisfies the condition t.Salary>50000 will be retrieved ( t.Salary =
t[salary] )
-To retrieve only some of the attributes—say, the first and last names—we write
t.Fname, t.Lname | EMPLOYEE(t) AND t.Salary>50000}
Expressions and Formulas in Tuple Relational Calculus
A general expression of the tuple relational calculus is of the form:
{t_1.A_j , t_2.A_k, ... , t_n.A_m | COND(t_1, t_2, ..., t_n, t_n+1, t_n+2, ..., t_n+m)}

-where t_1, t_2, … , t_n, t_n+1, … , t_n+m are tuple variables

- each Ai is an attribute of the relation on which ti ranges

-COND is a condition or formula of the tuple relational calculus. A formula is made up of predicate
calculus atoms, which can be one of the following:

R(t_i), where R is a relation name and t_i is a tuple variable.

An atom of the form t_i.A {=, <, ≤, >, ≥, ≠} t_j.B

An atom of the form t_i.A {=, <, ≤, >, ≥, ≠} c , c is a constant value


-
A formula (Boolean condition) is made up of one or more
atoms connected via the logical operators AND , OR and
NOT and is defined by Rules 1 and 2 as follows:

Rule 1: Every atom is a formula.

Rule 2: If F1 and F2 are formulas, then so are (F1


AND F2), (F1 OR F2), NOT(F1), and NOT (F2).
Expressions and Formulas in Tuple Relational Calculus


Universal Quantifiers: The universal quantifier denoted by is read as for all which
means that in a given set of tuples exactly all tuples satisfy a given condition.

Existential Quantifiers: The existential quantifier denoted by is read as for all which
means that in a given set of tuples there is at least one occurrences whose value satisfy
a given condition.

Free and Bound concept tuple variables in a formula

A tuple variable t is bound if it is quantified, meaning that


∃ ∀
it appears in an ( t) or ( t) clause; otherwise, it is free
Formally, we define a tuple variable in a formula
as free or bound according to the following rules:
An occurrence of a tuple variable in a formula F that is an atom is free in F.
An occurrence of a tuple variable t is free or bound in a formula made up of logical connectives—
(F1 AND F2), (F1 OR F2), NOT(F1), and NOT(F2)—depending on whether it is free or bound in F1
or F2 (if it occurs in either)..

All free occurrences of a tuple variable t in F are bound in a formula F′ of the form F′= ( t)(F) or

F′ = ( t)(F). The tuple variable is bound to the quantifier specified in F′. For example, consider
the following formulas:

F1: d.Dname = ‘Research’



F2: ( t)(d.Dnumber = t.Dno)

F3: ( d)(d.Mgr_ssn = ‘333445555’)

The tuple variable d is free in both F1 and F2, whereas it is bound to the ( ) quantifier in F3. Variable

t is bound to the ( ) quantifier in F2.
We can now give Rules 3 and 4 for the definition of a formula we started earlier:
∃ ∃
Rule 3: If F is a formula, then so is ( t)(F). 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.
∀ ∀
Rule 4: If F is a formula, then so is ( t)(F). 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.
Sample Queries in
Tuple Relational Calculus
Query 1: List the name and address of all employees who work for the
"Research" department.
Query 1: List the name and address of all employees who work for the
"Research" department.


{t.Fname, t.Lname, t.Address | EMPLOYEE(t) AND ( d)(DEPARTMENT(d)
AND d.Dname="Research" AND d.Dnumber=t.Dno)}
Query 1: List the name and address of all employees who work for the
"Research" department.


{t.Fname, t.Lname, t.Address | EMPLOYEE(t) AND ( d)(DEPARTMENT(d)
AND d.Dname="Research" AND d.Dnumber=t.Dno)}

t is the only free variable


Query 1: List the name and address of all employees who work for the
"Research" department.


{t.Fname, t.Lname, t.Address | EMPLOYEE(t) AND ( d)(DEPARTMENT(d)
AND d.Dname="Research" AND d.Dnumber=t.Dno)}

specifiy the range relation for t and d


Query 2: For every project located in ‘Stafford’, list the project number, the
con- trolling department number, and the department manager’s last
name, birth date, and address.
Query 2: For every project located in ‘Stafford’, list the project number, the
con- trolling department number, and the department manager’s last
name, birth date, and address.

{p.Pnumber, p.Dnum, m.Lname, m.Bdate, m.Address | PROJECT(p) AND



EMPLOYEE(m) AND p.Plocation=‘Stafford’ AND (( d)(DEPARTMENT(d)
AND p.Dnum=d.Dnumber AND d.Mgr_ssn=m.Ssn))}

2 free tuple variables: p and m


Query 2: For every project located in ‘Stafford’, list the project number, the
con- trolling department number, and the department manager’s last
name, birth date, and address.

{p.Pnumber, p.Dnum, m.Lname, m.Bdate, m.Address | PROJECT(p) AND



EMPLOYEE(m) AND p.Plocation=‘Stafford’ AND (( d)(DEPARTMENT(d)
AND p.Dnum=d.Dnumber AND d.Mgr_ssn=m.Ssn))}

d bound to the existential quantifier


Q8: {e.Fname, e.Lname, s.Fname, s.Lname | EMPLOYEE(e)
AND EMPLOYEE(s) AND e.Super_ssn=s.Ssn}
Query 4: Make a list of project numbers for projects that involve an
employee whose last name is ‘Smith’, either as a worker or as manager of
the controlling department for the project.

∃ ∃
{ p.Pnumber | PROJECT(p) AND ((( e)( w)(EMPLOYEE(e) AND WORKS_ON(w)
AND w.Pno=p.Pnumber
AND e.Lname=‘Smith’ AND e.Ssn=w.Essn) )
OR
∃ ∃
(( m)( d)(EMPLOYEE(m) AND DEPARTMENT(d) AND p.Dnum=d.Dnumber
AND d.Mgr_ssn=m.Ssn AND m.Lname=‘Smith’)))}
Notation for Query
Graphs
Represent simple relational
Query Graph calculus queries
(select-project-join queries)
Example: Query Graph for Q2

Attribute to be retrived
Edges: Select and Join condition

Relation Node

Constant Node
Query graph Query tree
Transforming the Universal and
Existential Quantifiers
∀ ∃ is possible!

Some special cases:


∀x) (P(x)) ≡ NOT (∃x) (NOT (P(x)))
(
∃x) (P(x)) ≡ NOT (∀x) (NOT (P(x)))
(
∀x) (P(x) AND Q(x)) ≡ NOT (∃x) (NOT (P(x)) OR NOT (Q(x)))
(
∀x) (P(x) OR Q(x)) ≡ NOT (∃x) (NOT (P(x)) AND NOT (Q(x)))
(
∃x) (P(x)) OR Q(x)) ≡ NOT (∀x) (NOT (P(x)) AND NOT (Q(x)))
(
∃x) (P(x) AND Q(x)) ≡ NOT (∀x) (NOT (P(x)) OR NOT (Q(x)))
(

For implication
∀x)(P(x)) ⇒ (∃x)(P(x))
NOT (∃x)(P(x)) ⇒ NOT (∀x)(P(x))
Group 8

Tranforming between
Quantifier in Query

Domain Relational Calculus


The formula is made of atoms, but it is slightly different from TRC:

The formula in DRC is also made of atoms, variables, quantifier just like in TRC
Domain Relational Calculus
Example:

Query:
List the birth date and address of the employee whose name is ‘John B. Smith’
Some well-known transformation
It is possible to transform between Universal and Existetial quantifier
=> To get an equivalent expression
EXAMPLE QUERY TRANSFORM BETWEEN QUANTIFIER

Consider a database that has the relations:

Query: List the names of employees who work on all the projects
controlled by department number 5
Query:
List the names of employees who
work on all the projects controlled
by department number 5.

Solution 1: Using Universal Quantifier


Query:
List the names of employees who
work on all the projects controlled
by department number 5.

Solution 2: Transform into Existential Quantifier


Safe expression

What is a safe expression ?


A safe expression is a relational calculus
that can guarantee to yield a finite number
of tuples as its result. Otherwise, it is
unsafe.

Example of unsafe expression?

Safe Unsafe because the result is infinitely

expression numerous since it yield all tuples that


are not in relation EMPLOYEE
Group 8

Domain Relational
Calculus

Domain Relational Calculus


Another type of relational Calculus.
Working with Domain Variables:
Variable ranges over value from domain of attributes.
A relation of degree n for a query result => n domain variables

The expression:
Domain Relational Calculus
The formula is made of atoms, but it is slightly different from TRC:

The formula in DRC is also made of atoms, variables, quantifier just like in TRC
Domain Relational Calculus
Example:

Query:
List the birth date and address of the employee whose name is ‘John B. Smith’
Query: List the birth date and address of the employee whose name is ‘John B. Smith’

Solution:

Rewrite:
THE END

You might also like