Sie sind auf Seite 1von 9

Tuple Relational

Calculus
A formal query language in which we specify a
declarative expression to describe a retrieval re-
quest.
We dont specify how the query is to be evalu-
ated.
We only specify what is to be retrieved.
Tuple Relational Calculus is a nonprocedural
language.
Relational Algebra is a procedural way of writ-
ing the query.
Moreover, the order of operations in a RA expres-
sion, inuences the strategy for evaluating the
query.
1
Tuple Relational Calculus and Relational Algebra
have the same expressive power.
(all queries in TRC can be expressed in RA and
vice versa)
This leads to the concept of a relationally com-
plete query language.
(any query expressible in RA can be expressed in
the query language)
Relational completeness has become a measure of
comparison for the expressive power of high-level
query languages.
Some more sophisticated database operations (e.g.
Aggregate functions, Recursive closure) cannon
be expressed in RA.
Most relational query languages are not only re-
lationally complete, but also contain additional
features like aggregate functions grouping etc.
2
Tuple Variables, Range Relations
In TRC we specify a number of tuple variables.
Each tuple variable ranges over a database table
(can take as values, tuples from the table).
Example: TRC query { t | COND(t) }
t is a tuple variable, COND(t) is a conditional
expression
Result of the query: All tuples t that satisfy
COND(t).
Example: Find all employees whose salary is
more than 50K
{ t | EMPLOYEE(t) and t.SALARY > 50K }
Remark: the above query retrieves all at-
tributes of selected EMPLOYEE tuples.
3
Example: query that retrieves some attributes
only
{t.FNAME, t.LNAME | EMPLOYEE(t) and t.SALARY > 50K}
in SQL:
SELECT T.FNAME, T.LNAME
FROM EMPLOYEE AS T
WHERE T.SALARY > 50000;
In general, the following information must be
specied in a TRC expression:
tuple variable t, its range relation: R(t)
a condition to select particular (combina-
tions of) tuples
set of attributes to be retrieved: requested
attributes
correspondence with SQL:
TRC range relation condition req. attrib.
SQL FROM clause WHERE cl. SELECT cl.
4
Example: Retrieve the birthdate and SIN of
the employee(s) whose last name is Smith
and whose age is less than 30
{t.BDATE, t.SIN | EMPLOYEE(t) and t.LNAME = "Smith"
and t.AGE < 30}
Expressions and Formulas in TRC
An expression in TRC is of the form
{t
1
.A
1
, t
2
.A
2
, . . . , t
n
.A
n
| COND(t
1
, . . . , t
n
, t
n+1
, . . . , t
n+m
)
where:
t
1
, . . . , t
n
, t
n+1
, . . . , t
n+m
are tuple variables
Each A
i
is an attribute of the relation on
which t
i
ranges
COND is a condition or formula in TRC
5
A TRC formula is made up of predicate calculus
atoms, which can be of 3 kinds:
1. R(t
i
), R = relation name, t
i
= tuple variable
ranging over the relation R
2. t
i
.A opt t
j
.B, t
i
, t
j
= tuple variables,
opt = comp. operator {=, =, >, , <, },
A, B attributes of the corresp. relations
3.

t
i
.A opt const.
const opt t
j
.B
const. is a constant value.
Each of these types of atoms evaluates to either
TRUE or FALSE for a specic combination of
tuples. (every atom has a truth value)
A TRC formula is: either an atom or atoms or
other formulas connected via the logical boolean
operators AND, OR, NOT.
The truth values of TRC formulas are computed
using the usual truth tables of AND, OR, NOT
from Logic.
6
Existential & Universal
Quantiers
TRC formulas can contain Existential ( ) &
Universal ( ) Quantiers.
If F is a TRC formula, then

(t)(F) is a formula
(t)(F) is a formula
const.
concept of a free and bound tuple variable
a tuple variable t is bound if it is quantied:
it appears in a t or a t.
Example: free/bound tuple variables:
d.dname = "Research" tuple variable d is free
( t)(d.DNUMBER=t.DNO) tuple variable d is
free, tuple variable t is bound
( d)(d.SIN="123456") tuple variable d is
bound
7
truth values for formulas with quantiers
(t)(F) is TRUE if the formula F evaluates
to TRUE for at least one tuple
(t)(F) is TRUE if the formula F evaluates
to TRUE for every tuple
Example: Existential Quantier I
Retrieve the name and address of all employ-
ees working for the Research department.
{t.NAME, t.ADDRESS | EMPLOY EE(t) and
( d)(DEPARTMENT(d) and d.DNAME = Research
and d.DNUMBER = t.DNO)}
Note: d.DNAME = Research corresponds
to the SELECT of Relational Algebra
Note: d.DNUMBER = t.DNO corresponds to
the JOIN of Relational Algebra
8
Example: Existential Quantier II
Find the names of employees who have no
dependents.
{e.NAME | EMPLOY EE(e) and
(not(d)(DEPENDENT(d) and e.SIN = d.ESIN))}
Example: Existential Quantier III
Find the names of managers who have at
least one dependent.
{e.NAME | EMPLOY EE(e) and
((d)(p)(DEPARTMENT(d) and DEPENDENT(p)
and e.SIN = d.MGRSIN and e.SIN = p.ESIN))}
9

Das könnte Ihnen auch gefallen