You are on page 1of 47

Abstract Query Languages:

Relational Algebra and Relational Calculus

Dr. Sambit Bakshi

NIT Rourkela

February 18, 2019

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 1 / 47
Outline

1 Introduction to Query Languages

2 Introduction to Query Languages

3 Relational Algebra

4 Relational Calculus

5 Tuple Relational Calculus

6 Domain Relational Calculus

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 2 / 47
Introduction to Query Languages

Introduction to Query Languages

The schema of a relation defines its structure


The relationships define constraints on the attributes / tuples
What would we do with the schema and constraints if there is no way to manage data in a
relation!
Query languages help in manipulate data in a relation

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 3 / 47
Introduction to Query Languages

Introduction to Query Languages

Database Model Query Model


Relational Model Relational Algebra (Procedural)
and
Relational Calculus (Non-procedural / Declarative)

These Query models are abstract or theoretical.

Relational Algebra and Relational Calculus (with only safe queries) are equivalent.

In practice, Relational model is implemented as RDBMS, and the query model is implemented
as SQL.

A language is called relationally complete if it has capacity to express every query expressible
through relational algebra. SQL is relationally complete.

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 4 / 47
Introduction to Query Languages

Introduction to Query Languages

Cardinality and Degree / Arity of a Relation


Degree / Arity of a relation is the number of attributes / columns of the relation.
It is an intrinsic property of a relation.
Cardinality of a relation is the number of tuples / rows of the relation.
It is not an intrinsic property of a relation, and represents a state of the relation.

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 5 / 47
Relational Algebra

Basic Operations

There are SIX basic operations:


Unary operations:
Selection (σ): Selects a subset of tuples / rows from a relation
Projection (π): Retains a subset of columns from a relation
Rename (ρ): Renames a relation

Binary operations:
Cross Product / Cartesian Product (×): Allows to combine two relations
Difference / Set difference (−): Returns tuples present in one relation but not in the
other
Union (∪): Consolidates tuples in two relations

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 6 / 47
Relational Algebra

More Operations

Intersection (∩): Returns tuples present in both relations


Join (./): Allows to combine two or more relations
There are several types of joins: semi join (o, n), left outer join ( ./), right outer join
(./ ), full outer join ( ./ )
Division (/): Returns those tuples of a relation which subsets tuples of another table
Assignment (←): Assigning the output to a relation

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 7 / 47
Relational Algebra

Selection operation
Selection (σ): selects a subset of tuples / rows from a relation.
Also called as horizontal partitioning.
Syntax: σ<CONDITION> (<RELATION>)

Select details of employees with salary (ESAL)


greater than INR 10000 from the table EMP(EID,
ENAME, ESAL, DEPT) given beside
σESAL > 10000 (EMP)
EID ENAME ESAL DEPT
EMP(EID, ENAME, ESAL, DEPT)
001 ABC 10500 2
EID ENAME ESAL DEPT 003 DEF 11000 4
001 ABC 10500 2 004 DBC 12000 4
002 ABD 9000 4
Select details of employees working in department
003 DEF 11000 4
(DEPT) ’4’ from the table EMP mentioned beside
004 DBC 12000 4
σDEPT = 4 (EMP)
EID ENAME ESAL DEPT
002 ABD 9000 4
003 DEF 11000 4
004 DBC 12000 4

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 8 / 47
Relational Algebra

Selection operation
Selection (σ): selects a subset of tuples / rows from a relation.
Also called as horizontal partitioning.
Syntax: σ<CONDITION> (<RELATION>)

Select details of employees of department (DEPT) ’4’


having salary (ESAL) greater than INR 10000 from
the table EMP mentioned beside
σDEPT = 4 AND ESAL > 10000 (EMP) (cascading conditions)
σDEPT = 4 (σESAL > 10000 (EMP)) (nested conditions)
EMP(EID, ENAME, ESAL, DEPT)
σESAL > 10000 (σDEPT = 4 (EMP)) (nested conditions)
EID ENAME ESAL DEPT EID ENAME ESAL DEPT
001 ABC 10500 2 003 DEF 11000 4
002 ABD 9000 4 004 DBC 12000 4
003 DEF 11000 4 As order of nesting of conditions does not affect the
004 DBC 12000 4 output of Select (σ) operator, it is commutative

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 9 / 47
Relational Algebra

Selection operation

Selection operator works on a single tuple at a time


The CONDITION involved in selection operation MUST be a boolean expression (or a
logical combination of more than one boolean expressions)
Selection operator may reduce the cardinality of a relation when it acts on, i.e.,
|σ<CONDITION> (<RELATION>)| ≤ |<RELATION>|
Selection operator does not alter the arity of a relation
Selection operator can act on a relation or any relational algebra expression resulting on a
table
Selection operator is commutative

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 10 / 47
Relational Algebra

Projection operation
Projection (π): selects a subset of attributes from a relation.
Also called as vertical partitioning.
Syntax: π<ATTRIBUTES> (<RELATION>)

Extract Employee IDs EID and Names ENAME of


employees from the table EMP(EID, ENAME, ESAL,
DEPT) given beside
πEID,ENAME (EMP)
EID ENAME
EMP(EID, ENAME, ESAL, DEPT) 001 ABC
002 ABD
EID ENAME ESAL DEPT 003 DEF
001 ABC 10500 2 004 DBC
002 ABD 9000 4
003 DEF 11000 4 Extract departments DEPT of employees from the
004 DBC 12000 4 table EMP mentioned above.
πDEPT (EMP)
DEPT
2
4

Removes duplicates!
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 11 / 47
Relational Algebra

Projection operation

Is Projection (π) operation commutative?

We know, for a projection operation to work, the attributes it retrieves must be a subset of the
attributes of the input relation.
For π<ATTRIBUTES2 > (π<ATTRIBUTES1 > (<RELATION>)) to be valid, the following condition must be true:
<ATTRIBUTES2 > ⊆ <ATTRIBUTES1 >
Also, for projection to be commutative,
π<ATTRIBUTES2 > (π<ATTRIBUTES1 > (<RELATION>)) = π<ATTRIBUTES1 > (π<ATTRIBUTES2 > (<RELATION>))
However, π<ATTRIBUTES2 > (π<ATTRIBUTES1 > (<RELATION>)) = π<ATTRIBUTES2 > (<RELATION>)
Hence projection is not commutative.

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 12 / 47
Relational Algebra

Projection operation

Projection operator can act on a relation or any relational algebra expression resulting on
a table
The <ATTRIBUTES> involved in selection operation MUST be a subset (may be equal) of
all attributes in <RELATION>
Projection operator may reduce the arity / degree of a relation when it acts on,
i.e., Arity of π<ATTRIBUTES> (<RELATION>) ≤ Arity of <RELATION>
Projection operator may alter the cardinality of a relation as it removes duplicate tuples
from output
i.e., |π<ATTRIBUTES> (<RELATION>)| ≤ |<RELATION>|
If <ATTRIBUTES> is a superkey, then
|π<ATTRIBUTES> (<RELATION>)| = |<RELATION>|
Projection operation defined in abstract relational algebra language eliminates duplicates,
but projection operator in SQL retains duplicates!
Projection operation is not commutative.

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 13 / 47
Relational Algebra

Cascading Selection and Projection operation

Extract Employee IDs EID and Names ENAME of


employees whose salary ESAL is greater than INR
10000 from the table EMP(EID, ENAME, ESAL,
DEPT) given beside
EMP(EID, ENAME, ESAL, DEPT)
TEMP ← σESAL > 10000 (EMP)
EID ENAME ESAL DEPT ANS ← πEID,ENAME (TEMP)
001 ABC 10500 2
OR IT CAN ALSO BE WRITTEN INLINE AS:
002 ABD 9000 4
πEID,ENAME (σESAL > 10000 (EMP))
003 DEF 11000 4
004 DBC 12000 4 EID ENAME
001 ABC
003 DEF
004 DBC

Please note that πEID,ENAME (σESAL > 10000 (EMP)) 6= σESAL > 10000 (πEID,ENAME (EMP)),
i.e., the roles of selection and projection cannot be commutative. Always selection acts
first, and then projection.

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 14 / 47
Relational Algebra

Rename operation

Syntax: Renaming relation name and attributes:


ρ<OUTPUT RELATION>(<OUTPUT ATTRIBUTES>) (<INPUT RELATION>)
Syntax: Renaming relation name only (and not attributes):
ρ<OUTPUT RELATION> (<INPUT RELATION>)
Syntax: Renaming attributes only (and not relation name):
ρ(<OUTPUT ATTRIBUTES>) (<INPUT RELATION>)
The input for rename operator can be a relation or a relational algebra expression
Rename operation is a special type of projection

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 15 / 47
Relational Algebra

Set operations

The set operations Union (∪), Intersection (∩), Difference (−) are binary Operations.

Union (∪): Consolidates tuples in two relations

Intersection (∩): Extracts only common tuples from two relations

Difference (−): Extracts tuples present exclusively in first relation (but not present in second
relation)

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 16 / 47
Relational Algebra

Set operations

Union Compatibility: The two input relations should have (i) same degree and (ii) same domain
for every attribute

Union Compatibility is decided based on intrinsic properties of two relations, not based on it’s
state.

R ∪ S automatically stores the result in first operand, i.e., R.

Union eliminates duplicates from the output and returns the common tuples only once in
output, and intersection cannot have duplicates.

Intersection is a derived operation, i.e.


R ∩ S = R − (R − S) = S − (S − R)

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 17 / 47
Relational Algebra

Set operations

Union and intersection operations are commutative, i.e.


R ∪ S = S ∪ R and R ∩ S = S ∩ R

Union and intersection operations are associative, i.e.


(R ∪ S) ∪ T = R ∪ (S ∪ T) and (R ∩ S) ∩ T = R ∩ (S ∩ T)

Difference / minus is not commutative, i.e.,


R − S 6= S − R

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 18 / 47
Relational Algebra

Cardinality analysis of set operations

Operation Venn Diagram Minimum Maximum


↓ Representation ↓ Cardinality Cardinality
Inputs: A having
cardinality m, B having A∪B max(m,n) m+n
cardinality n
A∩B 0 min(m,n)

A−B 0 m

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 19 / 47
Relational Algebra

Cross Product / Cartesian Product

Cross product / Cartesian product (×) is a binary Operation.

The input relations need not be union compatible for participating in cross product.

Arity of R × S = (Arity of R + Arity of S)

Cardinality of R × S = (Cardinality of R × Cardinality of S)

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 20 / 47
Relational Algebra

Cross Product / Cartesian Product

R(A, B, C)
P(A, B, C, D, E) ← R(A, B, C) × S(D, E)
A B C
a1 b1 c1 A B C D E
a2 b2 c2 a1 b1 c1 d1 e1
a3 b3 c3 a1 b1 c1 d2 e2
a2 b2 c2 d1 e1
S(D, E)
a2 b2 c2 d2 e2
D E a3 b3 c3 d1 e1
d1 e1 a3 b3 c3 d2 e2
d2 e2
Cross product is commutative, i.e, R × S = S × R
Cross product is associative, i.e., (R × S) × T = R × (S × T)
In most practical cases, such unconditional linking of every tuple of a relation with every
tuple of another relation is meaningless.

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 21 / 47
Relational Algebra

Join

Syntax: P ← R ./<join condition> S

R(A, B, C)
A B C
a1 b1 c1
P(A, B, C, D, E) ← R(A, B, C) ./A = D S(D, E)
a2 b2 c2
a3 b3 c3 A B C D E
a1 b1 c1 a1 b1
S(D, E)
a2 b2 c2 a2 b2
D E
a1 b1
a2 b2
Join product is commutative, i.e, R ./<join condition> S = S ./<join condition> R
Arity of R ./ S = (Arity of R + Arity of S)
Minimum Cardinality of R ./ S = 0
Maximum Cardinality of R ./ S = (Cardinality of R × Cardinality of S)

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 22 / 47
Relational Algebra

Join

<join condition> is always a boolean condition.

<join condition> is generally not a condition where check on a attributes on a single


participating relation is checked. Such conditions can always be applied before join. Why would
we apply such conditions while join!

<join condition> is usually a condition where check between attributes of both participating
relations is performed.

<join condition> is hence denoted as Ri θ Sj where Ri and Sj are two attributes from the
relations respectively, and θ is the binary boolean operation between them. θ can be checking if
they are equal, or unequal, or one is greater than other etc.

As the operation between attributes from different relations is classically represented by θ, it is


also called theta join.

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 23 / 47
Relational Algebra

Join

<join condition> is very common in some practical cases, as we may check relation bewteen
two related attributes.

For this, a special type of join operation, called natural join (∗) is used where
<join condition> is not explicitly written. An equality check between the attribute pairs
having same name is done, and accordingly the result of natural join is computed.

R(A, B, C)
A B C
a1 b1 c1
P(A, B, C) ← R(A, B, C) ∗ S(A)
a2 b2 c2
a3 b3 c3 A B C
a1 b1 c1
S(A)
a2 b2 c2
A
a1
a2

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 24 / 47
Relational Algebra

Join

R(A, B, C)
A B C
a1 b1 c1
a1 b2 c1
P(A, B, C, D) ← R(A, B, C) ∗ S(A, B, D)
a2 b1 c1
a2 b2 c2 A B C D
a3 b3 c3 a1 b1 c1 d1
a2 b2 c2 d2
S(A, B, D)
A B D
a1 b1 d1
a2 b2 d2

Common attributes are not repeated in resultant relation.

If no common attribute is there for natural join, it will degrade to cross product.

PLEASE NOTE THAT IT WON’T RETURN ZERO TUPLES IN THAT CASE.

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 25 / 47
Relational Algebra

Division

Syntax: P ← R / S

P ← R / S will retain those attributes which are present in R but not in S only for those tuples
where ALL values of common attributes in R matches any tuple of S

R(A, B)
A B
a1 b1
a2 b1 P(B) ← R(A, B) / S(A)
a1 b2
B
S(A) b1
A
a1
a2

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 26 / 47
Relational Algebra

Division

R(A, B)
A B
a1 b1
a2 b1
a3 b1 S(A)
P(B) ← R(A, B) / S(A)
a4 b1
A
a1 b2 B
a1
a3 b2 b1
a2
a2 b3 b4
a3
a3 b3
a4 b3
a1 b4
a2 b4
a3 b4

P × S may not return R.

Division is not commutative, i.e., R / S 6= S / R.

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 27 / 47
Relational Algebra

Division

R(A, B, C)
A B C
a1 b1 c1
a2 b1 c1
a3 b1 c3 S(A, C)
a4 b1 c2 P(B) ← R(A, B, C) / S(A, C)
A C
a1 b2 c1
a1 c1 B
a3 b2 c1
a2 c1 b1
a2 b3 c1
a3 c3
a3 b3 c1
a4 b3 c1
a1 b4 c2
a2 b4 c2
a3 b4 c3

Is division associative? Draw a Venn Diagram of set of three relations and check if R/(S/T) and
(R/S)/T contains same set of attributes.

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 28 / 47
Relational Algebra

Division

Division is not a basic operation, that means, division operation should be expressed in terms of
basic operations.

To express division (T ← R / S) with basic operations:

Step# 1: T1 ← π(R-S) (R)

Step# 2: T2 ← π(R-S) ((S × T1) - R)

Step# 3: T ← T1 - T2

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 29 / 47
Relational Algebra

Outer Join

All join functions learn before are inner join, in which the dangling tuples (those tuples of one
relation not having a referred value in other relation) does not appear in output.

Sometimes we may need those tuples which does not have a match in other relation.

A join providing such tuples in output is called outer join.

left outer join ( R ./ S) retains the dangling tuples of R but not those of S

right outer join ( R ./ S) retains the dangling tuples of S but not those of R

full outer join ( R ./ S) retains the dangling tuples of both R and S

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 30 / 47
Relational Algebra

Outer join

R ./A=B S
A C B D
1 3 1 6
R(A, C)
2 4 NULL NULL
A C
R ./ A=B S
1 3
2 4 A C B D
1 3 1 6
S(B, D)
NULL NULL 5 7
B D
R ./ A=B S
1 6
5 7 A C B D
1 3 1 6
2 4 NULL NULL
NULL NULL 5 7

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 31 / 47
Relational Algebra

A meme I found on internet!

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 32 / 47
Relational Algebra

Semi-join

A semi-join between two relations returns tuples from the one relation where one or more
matches (according to join condition) are found in tuples of the other relation.

Though a tuple in one relation may have match with more than one tuples of the other relation,
the tuple will appear only once in output.

left semi-join (R n S) retains those tuples of R which have a match with S

right semi-join (R o S) retains those tuples of S which have a match with R

semi-join is not commutative.

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 33 / 47
Relational Algebra

Semi-join

R(A, B, C)
A B C P(A, B, C) ← R(A, B, C) nA=D S(D, E, F)
a1 b1 c1
A B C
a1 b2 c1
a1 b1 c1
a2 b1 c1
a1 b2 c1
a3 b2 c2
a2 b1 c1
a4 b3 c3
Q(D, E, F) ← R(A, B, C) oA=D S(D, E, F)
S(D, E, F)
D E F
D E F
a1 e1 f1
a1 e1 f1
a1 e2 f3
a1 e2 f3
a2 e2 f2
a2 e2 f2
a5 e2 f3

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 34 / 47
Relational Algebra

Anti-join

A anti-join or anti semi-join between two relations returns those tuples from the one relation for
which there exists no corresponding matches (according to join condition) in tuples of the other
relation.

left anti-join or called ONLY anti-join (R . S) retains those tuples of R which do not have a
match with S

R . S = R - (R n S)

right anti-join (R / S) retains those tuples of S which do not have a match with R

R / S = S - (R o S)

anti-join is not commutative.

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 35 / 47
Relational Algebra

Anti-join

R(A, B, C)
A B C
a1 b1 c1 P(A, B, C) ← R(A, B, C) .A=D S(D, E, F)
a1 b2 c1
a2 b1 c1 A B C
a3 b2 c2 a3 b2 c2
a4 b3 c3 a4 b3 c3

S(D, E, F) Q(D, E, F) ← R(A, B, C) /A=D S(D, E, F)


D E F D E F
a1 e1 f1 a5 e2 f3
a1 e2 f3
a2 e2 f2
a5 e2 f3

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 36 / 47
Relational Algebra

Cardinality analysis of join operations


Operation Venn Diagram Minimum Maximum
↓ Representation ↓ Cardinality Cardinality
A×B NO CONDITION mn mn

Inputs: A having A ./ B 0 mn
cardinality m, B having
cardinality n AnB 0 m

Note: The table bears


AoB 0 n
representative Venn
diagram, it does not
state which attributes A.B 0 m
will appear on result,
and whether tuples will A/B 0 n
be duplicated on result.
A ./ B m mn

A ./ B n mn

A ./ B max(m,n) mn

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 37 / 47
Relational Algebra

Problems on Relational Algebra

Q. Which of the following query transformations (i.e. replacing the l.h.s. expression by the r.h.s.
expression) are incorrect?
R1 and R2 are relations, C1 and C2 are selection conditions, and A1 and A2 are attributes of R1 .
(a) σC1 (σC2 (R1 )) → σC2 (σC2 (R1 ))
(b) σC1 (πA1 (R1 )) → πA1 (σC1 (R1 ))
(c) σC1 (R1 ∪ R2 ) → σC1 (R1 ) ∪ σC1 (R2 )
(d) πA2 (σC1 (R1 )) → σC1 (πA2 (R1 ))
[GATE1998]

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 38 / 47
Relational Algebra

Problems on Relational Algebra

Q. Suppose R1 (A,B) and R2 (C,D) are two relation schemas. Let r1 and r2 be the corresponding
relation instances. B is a foreign key that refers to C in R2 . If the data in r1 and r2 satisfy
referential integrity constraints, which of the following is ALWAYS TRUE?
(a) πB (r1 ) − πC (r2 ) = φ
(b) πC (r2 ) − πB (r1 ) = φ
(c) πB (r1 ) = πC (r2 )
(d) πB (r1 ) − πC (r2 ) 6= φ
[GATE2012]

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 39 / 47
Relational Algebra

Problems on Relational Algebra

Q. Give a relational algebra expression using only the minimum number of operators from {∪ ,
- } which is equivalent to R ∩ S.
[GATE1994]

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 40 / 47
Relational Algebra

Problems on Relational Algebra

Q. Consider a join of a relation R with a relation S. If R has m tuples and S has n tuples, then the
maximum and minimum sizes of the join respectively are:
(a) m+n and 0
(b) mn and 0
(c) m+n and |m-n|
(d) mn and m+n
[GATE1999]

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 41 / 47
Relational Calculus

Types of Relational Calculus

(a) Tuple Relational Calculus (TRC)


(b) Domain Relational Calculus (DRC)

TRC and DRC are equivalent in power (when only safe queries are concerned), i.e., any query
expressed through TRC do have a parallel query in DRC that serve the same purpose.

TRC and DRC are also equivalent in power with relational algebra.

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 42 / 47
Tuple Relational Calculus

Tuple Relational Calculus

Syntax: {t.attributes | range relation of t AND conditions(t)}

P(A,B,C)
A B C Find those values of As for which C has a
a1 b1 c1 value c1
a2 b1 c1
a3 b1 c3 {t.A | P(t) AND t.C = c1 }
a4 b1 c2
a1 b2 c1 A
a3 b2 c2 a1
a2 b3 c1 a2
a3 b3 c1 a3
a4 b3 c1 a4
a1 b4 c2

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 43 / 47
Tuple Relational Calculus

Tuple Relational Calculus

Composite expressions:
Atomic expressions:
Range relation:
Range relation:
R1 (t1 ) ∧ R2 (t2 ) ∧ . . . ∧ Rn (tn )
R(t)
Conditions:
Conditions:
(i) with free variables:
t.A θ constant
F1 ∧ F2 , F1 ∨ F2 , ¬ F1
t1 .A θ t2 .B
(ii) with bounded variables using quantifiers: ∀ t(F), ∃ t(F)

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 44 / 47
Tuple Relational Calculus

Tuple Relational Calculus

List the names and addresses of all employees who work for department named ‘CSE’. The
database schema is as following:
EMP(ENAME, EADDRESS, EDOB, ESAL, DNO)
DEPT(DNO, DNAME, DMGR)
EMP.DNO is foreign key to DEPT.DNO

{t.ENAME,t.EADDRESS | EMP(t) ∧ (∃ d)(DEPT(d) ∧ d.DNAME=‘CSE’ ∧ d.DNO=t.DNO)}

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 45 / 47
Domain Relational Calculus

Domain Relational Calculus

Syntax: {<attributes> | domain constraint of attributes AND conditions on attributes}

P(A,B,C)
A B C Find those values of As for which C has a
a1 b1 c1 value c1
a2 b1 c1
a3 b1 c3 {<a> | ∃b ∃c (P(a,b,c) AND c = c1 )}
a4 b1 c2
a1 b2 c1 A
a3 b2 c2 a1
a2 b3 c1 a2
a3 b3 c1 a3
a4 b3 c1 a4
a1 b4 c2

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 46 / 47
Domain Relational Calculus

Domain Relational Calculus


Formal Definition: An expression in the domain relational calculus is of the form
{<x1 ,x2 ,. . .,xn > | P(x1 ,x2 ,. . .,xn )}
where x1 ,x2 ,. . .,xn represent domain variables, P represents a formula composed of atoms, as
was the case in the tuple relational calculus.
An atom in the domain relational calculus has one of the following forms:
<x1 ,x2 ,. . .,xn > ∈ r , where r is a relation on n attributes and x1 ,x2 ,. . .,xn are domain
variables or domain constants.
x θ y, where x and y are domain variables and θ is a comparison operator. We require
that attributes x and y have domains that can be compared by θ.
x θ c, where x is a domain variable, θ is a comparison operator, and c is a constant in
the domain of the attribute for which x is a domain variable.

We build up formulae from atoms by using the following recursively applied rules:
An atom is a formula.
If P is a formula, then so are ¬P and (P).
If P1 and P2 are formulae, then so are P1 ∧ P2, P1 ∨ P2, and P1 ⇒ P2
If P(x) is a formula in x, where x is a domain variable, then: ∃x(P(x)) and ∀x(P(x)) are
also formulae.

Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 47 / 47

You might also like