Relational Calculus: Zachary G. Ives
Relational Calculus: Zachary G. Ives
Zachary G. Ives
University of Pennsylvania
CIS 550 – Database & Information Systems
December 7, 2021
Some slide content courtesy of Susan Davidson & Raghu Ramakrishnan
Administrivia
Reminder: Homework 1 due 9/28 (next Monday)
2
The Relational Algebra as “Virtual
Machine” Instructions
Six basic operations:
Projection (R)
Selection (R)
Union R1 [ R2 STUDENT
Difference R 1 – R2
Product R1 £ R2
(Rename) (R)
Takes COURSE
And some other useful ones:
Join R1 ⋈ R2 Calculus
Intersection R1 Å R2
SELECT *
FROM STUDENT, Takes, COURSE
WHERE STUDENT.sid = Takes.sID
AND Takes.cID = cid
3
Our Example Data Instance
STUDENT Takes COURSE
sid name sid exp-grade cid cid subj sem
1 Jill 1 A 550-0109 550-0109 DB F09
2 Qun 1 A 520-1009 520-1009 AI S09
3 Nitin 3 A 520-1009 501-0109 Arch F09
4 Marty 3 C 501-0109
4 C 501-0109
PROFESSOR Teaches
fid name fid cid
11 Ives 11 550-0109
12 Taskar 12 520-1009
18 Martin 18 501-0109
4
Relational Calculus, Variant I:
Domain Relational Calculus (DRC)
Queries have form:
domain variables
{<x1,x2, …, xn>| p}
predicate
Predicate: Boolean expression over x1,x2, …, xn
Precise operations depend on the domain and query
language – may include special functions, etc.
Assume the following at minimum:
<xi,xj,…> R X op Y X op const const op X
where op is , , , , ,
xi,xj,… are domain variables
5
Complex Predicates in the Calculus
Starting with these atomic predicates, build up new
predicates by the following rules:
Logical connectives: If p and q are predicates, then so are p
q, p q, p, and p q
(x>2) (x<4)
(x>2) (x>0)
Existential quantification: If p is a predicate, then so is x.p
x. (x>2) (x<4)
Universal quantification: If p is a predicate, then so is x.p
x.x>2
x. y.y>x
6
Some Examples
Faculty ids
7
Logical Equivalences
There are two logical equivalences that will be
heavily used:
p q p q
(Whenever p is true, q must also be true.)
x. p(x) x. p(x)
(p is true for all x)
The second can be a lot easier to check!
Example:
The highest course number offered
8
Terminology:
Free and Bound Variables
A variable v is bound in a predicate p when p is of
the form v… or v…
A variable occurs free in p if it occurs in a position
where it is not bound by an enclosing or
Examples:
x is free in x > 2
x is bound in x. x > y
9
Can Rename Bound Variables Only
When a variable is bound one can replace it with
some other variable without altering the meaning
of the expression, providing there are no name
clashes
Example: x. x > 2 is equivalent to y. y > 2
10
Safety
Pitfall in what we have done so far – how do we interpret:
{<sid,name>| <sid,name> STUDENT}
Set of all binary tuples that are not students: an infinite set (and
unsafe query)
11
Safety and Termination Guarantees
There are syntactic conditions that are used to
guarantee “safe” formulas
The definition is complicated, and we won’t discuss it; you
can find it in Ullman’s Principles of Database and
Knowledge-Base Systems
The formulas that are expressible in real query languages
based on relational calculus are all “safe”
Many DB languages include additional features, like
recursion, that must be restricted in certain ways to
guarantee termination and consistent answers
12
Mini-Quiz
How do you write:
Which students have taken more than one course from the
same professor?
13
Translating from RA to DRC
Core of relational algebra: , , , x, -
We need to work our way through the structure of an
RA expression, translating each possible form.
Let TR[e] be the translation of RA expression e into DRC.
14
Selection: TR[ R]
Suppose we have (e’), where e’ is another RA
expression that translates as:
TR[e’]= {<x1,x2, …, xn>| p}
Then the translation of c(e’) is
{<x1,x2, …, xn>| p’}
where ’ is obtained from by replacing each
attribute with the corresponding variable
Example: TR[#1=#2 #4>2.5R] (if R has arity 4) is
{<x1,x2, x3, x4>|
< x1,x2, x3, x4> R x1=x2 x4>2.5}
15
Projection: TR[i1,…,im(e)]
16
Union: TR[R1 R2]
R1 and R2 must have the same arity
For e1 e2, where e1, e2 are algebra expressions
TR[e1]={<x1,…,xn>|p} and TR[e2]={<y1,…yn>|q}
Relabel the variables in the second:
TR[e2]={< x1,…,xn>|q’}
This may involve relabeling bound variables in q to
avoid clashes
TR[e1e2]={<x1,…,xn>|pq’}.
Example: TR[R1 R2] = {< x1,x2, x3,x4>|
<x1,x2, x3,x4>R1 <x1,x2, x3,x4>R2
17
Other Binary Operators
Difference: The same conditions hold as for union
If TR[e1]={<x1,…,xn>|p} and TR[e2]={< x1,…,xn>|q}
Then TR[e1- e2]= {<x1,…,xn>|pq}
Product:
If TR[e1]={<x1,…,xn>|p} and TR[e2]={< y1,…,ym>|q}
Then TR[e1 e2]= {<x1,…,xn, y1,…,ym >| pq}
Example:
TR[RS]= {<x1,…,xn, y1,…,ym >|
<x1,…,xn> R <y1,…,ym > S }
18
What about the Tuple Relational Calculus
(TRC)?
We’ve been looking at the Domain Relational Calculus
The Tuple Relational Calculus is nearly the same, but
variables are at the level of a tuple, not an attribute
19
Limitations of the
Relational Algebra / Calculus
Can’t do:
Aggregate operations (average, sum, count)
Track the number of duplicate elements (bag semantics)
Recursive queries
Complex (non-tabular) structures
20
Summary
Can translate relational algebra into relational calculus
DRC and TRC are slightly different syntaxes but equivalent
Given syntactic restrictions that guarantee safety of DRC
query, can translate back to relational algebra
21