You are on page 1of 7

MALLABHUM INSTITUTE OFTECHNOLOGY

NAME:- BAPAN KAR Roll no:- 15800121011


Reg no:- 211580100110009 Dept:- CSE
Paper name:- Database Management Systems
Paper code:- PCC-CS601
Topic for CA2 report writing:- Relational algebra & Relational
calculas
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.

A sequence of relational algebra operations forms a relational algebra expression.

There are two main types of relational algebra operations:

1. Set theory operations: Union, Intersection, Difference and Cartesian product.


2. Specific relational operations: Selection, Projection, Join, Division, Rename

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.

Set theory operations


Consider relations R and S:

▪ 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.

The two operands (R, S) must be type compatible.

For two operand relations R (R1, R2, ..., Rn) and S (S1, S2, ..., Sn) to be type compatible:

• The relations must have the same number of attributes.


• The domains of corresponding attributes must be
compatible i.e. Dom (Ri)=dom(Si) for i=1, 2, ..., n.
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:R S
S1 S2 Kamau 71
R1 R
2 Kamau 71 Mwend 72
e
Kamau 71 Fatum 72
a Okelo 73
Mwend 72
Okelo 73 Masai 74
e
Wafula 74 Wafula 75
Okelo 73
Fatuma 72
Masai 74
Wafula 74
Wafula 75 R1 R
2

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

1.3. Difference or minus


R1 R
2 R1 R
The operation is denoted by: R –
S 2
Kamau 71
S1 S2 Kama 71
Mwend 72 u
e Kamau 71
Okel 73
Okelo 73 Fatum 72 o
a
Masai 74
Okelo 73
Wafula 75
Wafula 74
The result of this operation is a relation that includes all tuples that are in R but that are not in S.

The two operands must be type compatible.

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

Cartesian or cross product

This operation is denoted by: R X S

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. Tuple Relational Calculus


b. Domain Relational Calculus

Tuple Relational Calculus


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
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

{t|t ∈ Lecturer ∧t[salary] > 100000} Lecturer

IDNo Name Dept_Name Salary

201232 June Geospatial 87000

456320 Kioko Land Admin 120000

102547 Aoko ICT 95000

637802 Samila Graphics 100000

Suppose that we want only the IDNo attribute, rather than all attributes of the Lecturer relation.

The following notation is used


∃t ∈ r (Q(t))

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

{t|∃s ∈Lecturer (t[IDNo] = s[IDNo] ∧s[salary] > 100000)}


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:

{t|∃s ∈Lecturer (t[name] = s[name]

∧∃u ∈ department (u[dept_name] = s[dept_name]

∧u[building] =“N block”))}

i. Domain Relational Calculus


This type uses variables that take values from domains instead of tuples of relations. It uses domain
variables that take on values from an attributes domain, rather than values for an entire tuple. The
domain relational calculus, however, is closely related to the tuple relational calculus.

Example

a. Find the Lecturer IDNo, name, dept_name, and salary for Lecturers whose salary is greater
than Kshs. 100,000:

The query is as follows


{< i, n, d, s > | < i, n, d, s > ∈ lecturer 𝖠 s > 100000}

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:

The query is as follows


{< n > | ∃ i, d, s (< i, n, d, s > ∈ lecturer 𝖠 s > 100000)}

You might also like