0% found this document useful (0 votes)
26 views

Relational Algebra and Relational Calcul

1. Relational algebra is a formal query language used in relational databases that consists of set operations like union, intersection, difference, and Cartesian product applied to tables called relations. 2. The main operations are set theory operations like union, intersection, difference; relational operations like selection, projection, join; and specific operations on rows and columns of a relation. 3. Operations like union and intersection are commutative and associative, while difference is not commutative. The result of operations forms a new relation that can then have additional operations applied to it.

Uploaded by

Bapan Kar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Relational Algebra and Relational Calcul

1. Relational algebra is a formal query language used in relational databases that consists of set operations like union, intersection, difference, and Cartesian product applied to tables called relations. 2. The main operations are set theory operations like union, intersection, difference; relational operations like selection, projection, join; and specific operations on rows and columns of a relation. 3. Operations like union and intersection are commutative and associative, while difference is not commutative. The result of operations forms a new relation that can then have additional operations applied to it.

Uploaded by

Bapan Kar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

ENTITY RELATIONSHIP MODELLING

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.

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

1. Set theory operations

Consider relations R and S:

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

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.

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.

R
Name IDNum Refere
e

Odhiamb 129920 Harris


o 1

Chebet 131233 Fred


4

Wekesa 1292301

S
Name IDNum Section

Odhiamb 129920 Finance


o 1

Chebet 131233 Researc


4 h

Wekesa Research

Nzuki Marketing
Result relation, T
Name IDNum Referee Section

Odhiamb 129920 Harris Finance


o 1

Chebet 131233 Fred Research


4

Wekesa 1292301 Research

Nzuki 121343 (null) Marketin


4 g

(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 operation is denoted by:

The result of this operation is a relation that includes all tuples that are in both R and S.

The two operands must be type compatible.


3

Example:

RS

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 R1 R
2 S1 S2 2

Andy 71 Andy 71 Boris 72

Boris 72 Chris 72 Fred 74

Dan 73 Dan 73 Edga 75


r
Fred 74 Edga 74
r
Edga 75
r

Question: What is S – R?
1.4 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.


4

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

Commutativity and associativity of operations

Both union and intersection are commutative operations i.e.


, and

Both union and intersection are associative operations i.e.


, and

The minus operation is not commutative i.e. R - S is not necessarily equivalent to S - R

Question: Is the minus operation associative?

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

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

{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”))}
Name

June
Kioko

Names of all Lecturers whose department is in the N block building.

ii. 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)}
8

You might also like