Professional Documents
Culture Documents
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.
▪ Union
The operation is denoted by:
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:
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.
The operation works as follows:
• The attributes that are compatible are represented only once in the union result. • The
attributes that are not compatible from either relation are also kept in the result relation T (X,
Y, Z).
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.
s Name IDNum Section
Odhiam 129920 Finance
bo 1
Name IDNu Refere Section
Chebet 131233 Resear
m e
4 ch
Odhia 129920 Harris Finance
Wekesa Research
m 1
bo Nzuki Marketing
Chebet 131233 Fred Research R
4
Weke 12923 Research Name IDNum Refere
sa 01 e
Nzuki 121343 (null) Market Odhiamb 129920 Harris
4 in o 1
g
Chebet 131233 Fred
Result relation, T 4
Wekesa 1292301
(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.
Intersection
The operation is denoted by:
The result of this operation is a relation that includes all tuples that are in both R and S.
Thetwo operands must be type compatible.
Example: R S
Example:
RSR–S
R1 R
2 R1 R
S1 S2 2
Andy 71 Boris 72
Andy 71
Boris 72 Fred 74
Chris 72
Dan 73 Edg 75
Dan 73 a
Fred 74 r
Edga 74
Edga 75 r
r
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
The two operands need not be type compatible.
Example:
R1 R R1 R2 S1 S2
2
S1 S2
Kamau 71 Kamau 71 Kamau 71
Kamau 71
Mwend 72 Kamau 71 Fatum a 72
e Fatum 72
a
Okelo 73 Kamau 71 Okelo 73
Okelo 73
Masai 74 Kamau 71 Wafula 74
Wafula 74
Wafula 75 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
74 Okelo 73
Masai
Masai 74 Wafula 74
Wafula 75
Kamau 71
Wafula 75
Fatum a 72
Wafula Okelo 73
Wafula Wafula 74
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.There are two types of relational calculus
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
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:
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: