Relational Algebra and Relational Calcul
Relational Algebra and Relational Calcul
OUTLINE
• Introduction
• Relational Algebra
• Set Theory operations
• Commutativity and associativity of operations
• Relational Calculus
• Tuple Relational Calculus
• Domain Relational Calculus
OBJECTIVES
• Understand the concept of Relational Calculus and Relational Algebra
RELATIONAL ALGEBRA
Relational Algebra is a formal description of how a database based on the relational data model
operates. This formal language forms the basis of the widely used Sequential Query Language
(SQL) language.
The relational algebra is a procedural query language. It consists of a set of operations that take
one or two relations as input and produce a new relation as their result. That is, the relations are
operands and the result is another relation. The resultant relation can be further manipulated using
more relational algebra operations.
Note: The set of operations including Select, Projection, Union, Difference, and Cartesian product
are called a complete set because any other relational algebra expression can be expressed by a
combination of the five operations.
Remember
a. A relation is a table with columns and rows. Only applies to logical structure of the database,
not the physical structure.
b. An attribute is a named column/field of a relation.
c. A tuple is a row in a relational database
d. A domain is a set of allowable values for one or more attributes.
e. A relational database is a collection of normalized relations.
1
1.1 Union
The result of this operation is a relation that includes all tuples that are either in R, or in S, or in
both R and S. Thus, duplicate tuples are removed.
For two operand relations R (R1, R2, ..., Rn) and S (S1, S2, ..., Sn) to be type compatible:
By convention, the resulting relation from the union operation (even intersection and difference
operations), has the same attribute names as the first operand relation R.
Example:
RS
S1 S2 Kamau 71
R1 R
2 Kamau 71 Mwend 72
e
Kamau 71 Fatum 72
a Okelo 73
Mwend 72
e Okelo 73 Masai 74
Wafula 74 Wafula 75
Okelo 73
Masai 74 Fatuma 72
Wafula 75 Wafula 74
R1 R
2
Outer union
The outer union operation was developed to handle the union of tuples for cases where the two
relations are NOT union compatible.
Consider two relations R (X, Y) and S (X, Z) where only the attributes X are compatible, whereas Y
and Z are not compatible.
Example:
Consider that:
• R is the relation Employee (Name, IDNum, Referee),
• S is the relation Director (Name, IDNum, Section),
• The attributes Name are compatible, the attributes IDNum are compatible, and attributes
Referee and Section are not compatible, and
• T is the result relation of the outer union.
R
Name IDNum Refere
e
Wekesa 1292301
S
Name IDNum Section
Wekesa Research
Nzuki Marketing
Result relation, T
Name IDNum Referee Section
(N.B: In simple terms. When the columns in Table R are combined with columns in Table S, the
resulting table T is formed)
Note that tuples from the two relations are matched based on having the same combination of
values of the compatible attributes. If an employee is also a director, both Referee and Section will
have a value; otherwise, one of these two attributes will be null.
1.2 Intersection
The result of this operation is a relation that includes all tuples that are in both R and S.
Example:
RS
Example:
RSR–S
R1 R R1 R
2 S1 S2 2
Question: What is S – R?
1.4 Cartesian or cross product
This operation is used to combine tuples from two relations in a combinatorial fashion.
In general, the result of R(A1, A2, . . ., An) x S(B1, B2, . . ., Bm) is a relation T with n + m
attributes, T(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order.
The resulting relation T has one tuple for each combination of tuples, one from R and one from S.
Hence, if R has r tuples (denoted as |R| = r ), and S has s tuples, then | R x S | will have r * s tuples
Example:
R
R1 R
2
Kamau 71
Mwend 72
e
Okelo 73
Masai 74
Wafula 75
S
S1 S2
Kamau 71
Fatum 72
a
Okelo 73
Wafula 74
RXS
R1 R S1 S2
2
Kamau 71 Kamau 71
Kamau 71 Fatum 72
a
Kamau 71 Okelo 73
Kamau 71 Wafula 74
Mwend
Mwende
Mwen 73
Mwende Wafula 74
Okelo 73 Kamau 71
Okelo 73
Fatum
a 72
Okelo Okelo 73
Okelo Wafula 74
Masai 74
Kamau
71
Masai 74 Fatuma
Masai 74 Okelo 73
Masai 74 Wafula 74
Wafula 75
Kamau
71
Wafula 75
Fatum
a 72
Wafula Okelo 73
Wafula Wafula 74
5
RELATIONAL CALCULUS
A Relational calculus is a non-procedural query language that tells the system what data to be
retrieved but doesn’t tell how to retrieve it.
Relational calculus query specifies what is to be retrieved rather than how to retrieve it. That is, no
description of how to evaluate a query.
A language that produces a relation that can be derived using relational calculus is relationally
complete
This is a nonprocedural query language. It describes the desired information without giving a
specific procedure for obtaining that information.
A query in the tuple relational calculus is expressed as: {t| P(t)} where
t=Tuple/(Team)
p=predicate which is a truth-valued function. Is an expression that evaluates to TRUE, FALSE, or
UNKNOWN.
A=Attribute
When we substitute values for the arguments, function yields an expression, called a proposition,
which can be either true or false.
N.B:
• t[A] is used to denote the value of tuple t on attribute A,
• t∈r denotes that tuple t is in relation r.
• (𝖠) denotes and
6
Example Queries
a. Find the IDNo, Name, Dept name, Salary for lecturers whose salary is greater than Kshs.
100,000. The query will be as follows
Suppose that we want only the IDNo attribute, rather than all attributes of the Lecturer relation.
This means there exists a tuple (row) t in relation (table) r such that predicate Q(t) is true. So the
query “Find the Lecturer IDNo for each lecturer with a salary greater than Kshs 100,000” will be as
follows
This simply means, the set of all tuples (rows) t such that there exists a tuple (row) s in relation
Lecturer for which the values of t and s for the IDNo attribute are equal, and the value of s for the
salary attribute is greater than Kshs 100,000.
b. Find the names of all Lecturers whose department is in the N block building. This query is
slightly more complex than query no. i, since it involves two relations: Lecturer and
department. This therefore requires “there exists” clauses connected by and (𝖠). The query
thus is as follows:
June
Kioko
Example
a. Find the Lecturer IDNo, name, dept_name, and salary for Lecturers whose salary is greater
than Kshs. 100,000:
i.e {< idno, name, dept_name, salary > | < idno, name, dept_name, salary > ∈ lecturer 𝖠 salary >
100000}
b. Find all lecturer IDNo for lecturers whose salary is greater than Kshs. 100,000: