0% found this document useful (0 votes)
46 views47 pages

IST601 20242025 - Relational Algebra-Calculus

The document provides an overview of Relational Algebra, a theoretical language used in database management for data manipulation. It covers fundamental operations such as selection, projection, set operations, and join operations, explaining their functions and providing examples. The document emphasizes the procedural nature of relational algebra and its equivalence to relational calculus, highlighting its significance in forming the basis for higher-level data manipulation languages.

Uploaded by

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

IST601 20242025 - Relational Algebra-Calculus

The document provides an overview of Relational Algebra, a theoretical language used in database management for data manipulation. It covers fundamental operations such as selection, projection, set operations, and join operations, explaining their functions and providing examples. The document emphasizes the procedural nature of relational algebra and its equivalence to relational calculus, highlighting its significance in forming the basis for higher-level data manipulation languages.

Uploaded by

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

IST 601: Database Management

Relational Algebra
(Chapter 5)

Denis L. Nkweteyim

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 1


Outline

Introduction

Relational Algebra

Introduction

Unary Operations

Set Operations

Join Operations

Division Operation

Aggregation and Grouping Operations

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 2


Introduction

Any data model requires a data manipulation
mechanism (i.e., query language) to allow the underlying
data to be retrieved and updated

Two mathematical Query Languages form the basis for
“real” languages (e.g. SQL), and for implementation:

Relational Algebra

Relational Calculus

Both defined by Codd (1971)

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 3


Introduction

Relational algebra

A (high-level) procedural language

More operational, very useful for representing execution plans

Can be used to tell the DBMS how to obtain data

Relational calculus

A declarative language

Lets users describe what they want, rather than how to compute it. (Non-operational, declarative.)

Can be used to formulate which data is to be obtained but not how

Relational algebra and relational calculus are formally equivalent to one another

Every expression in the algebra has an equivalent expression in the calculus (and vice versa)

Both the algebra and the calculus are formal, non-user-friendly languages

They have been used as the basis for other, higher-level Data Manipulation Languages (DMLs) for relational databases

They illustrate the basic operations required of any DML and serve as the standard of comparison for other relational languages

We only consider relational algebra

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 4


Outline

Introduction

Relational Algebra

Introduction

Unary Operations

Set Operations

Join Operations

Division Operation

Aggregation and Grouping Operations

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 5


Relational Algebra

A theoretical language

Both the operands and the results are relations

Original relation(s) unchanged

Output from one operation can become the input to another
operation

Hence, expressions can be nested in other expressions

Based on set theory

Allows for tuples, possibly from several relations, to be
manipulated in one statement without looping

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 6


Relational Algebra

Codd originally proposed 5 fundamental operations

Fundamental operations

Selection, Projection, Cartesian product, Union, and Set difference

These perform most of the data retrieval operations

Other operations proposed later

Join, Intersection, Division and Renaming

These can be expressed in terms of the five basic operations

They are not essential, but are very useful

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 7


Relational Algebra

Selection and Projection operations are unary
operations

They operate on one relation

The other fundamental operations are binary

They operate on pairs of relations

Other operations have been developed ever since

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 8


Outline

Introduction

Relational Algebra

Introduction

Unary Operations

Set Operations

Join Operations

Division Operation

Aggregation and Grouping Operations

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 9


Relational Algebra Operations

In examples that follow

R and S are two relations defined over the attribute sets A
and B

A = (a1, a2, . . . , aN)

B = (b1, b2, . . . , bM)

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 10


Selection

σpredicate(R)

(Unary) Works on a single relation R and defines a relation
that contains only those tuples of R that satisfy the specified
condition (or predicate)

Schema of Selection Result

Identical to schema of input relation

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 11


Selection


Example

List all staff with a salary
greater than £10000

σsalary > 10000(Staff)

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 12


Projection

Πa1 …, an(R)

(Unary) Works on a single relation R and defines a relation
that contains a vertical subset of R, extracting the values of
specified attributes and eliminating duplicates

Schema of Projection Result

Contains exactly the fields in the projection list, with the same
names that they had in the input relation

Note

Projection operator eliminates duplicates!

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 13


Projection


Example

Produce a list of salaries for all
staff, showing only the staffNo,
fName, IName, and salary details

ΠstaffNo, fName, IName, salary(Staff)

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 14


Outline

Introduction

Relational Algebra

Introduction

Unary Operations

Set Operations

Join Operations

Division Operation

Aggregation and Grouping Operations

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 15


Set Operations - Union, Intersection, Set-
Difference

Take two input relations, which must be union-
compatible, i.e., data from the same domain

Same number of fields

`Corresponding’ fields have the same type

Schema of Result

Identical to schema of input relations

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 16


Set Operations

Union

RÈS

Defines a relation that contains all
the tuples of R, or S, or both R
and S

R and S must be union-
compatible

Duplicate tuples are eliminated

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 17


Set Operations - Union

Example

List all cities where there is either a
branch office or a property for rent

Πcity(Branch) È Πcity(PropertyForRent)

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 18


Set Difference

R–S

Defines a relation consisting of
the tuples that are in relation R,
but not in S

R and S must be union-
compatible

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 19


Set Operations - Difference

Example

List all cities where there is a branch
office but no properties for rent

Πcity(Branch) - Πcity(PropertyForRent)

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 20


Intersection

R∩S

Defines a relation consisting of
the set of all tuples that are in
both R and S

R and S must be union-
compatible

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 21


Set Operations - Intersection

Example

List all cities where there is both a branch
office and at least one property for rent

Πcity(Branch) ∩ Πcity(PropertyForRent)

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 22


Cartesian Product

RxS

Defines a relation that is the concatenation of every tuple of
relation R with every tuple of relation S

Resulting relation consists of all possible pairs of tuples
from the two relations

If one relation has I tuples and N attributes and the other has
J tuples and M attributes

Cartesian product relation will contain (I * J) tuples with (N + M)
attributes

If the two relations have attributes with the same name, prefix the
attribute names relation name to maintain the uniqueness of
attribute names within a relation
08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 23
Cartesian Product


Example

List the names and comments of all clients who have viewed a
property for rent

Can start with Cartesian product, then select from rows

(ΠclientNo, fName, IName(Client)) x (ΠclientNo, propertyNo, comment(Viewing))

Results on following slide

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 24


Cartesian Product

Note

Some of the results of cartesian
product are meaningless

Example

The first tuple contains different
clientNo values

To obtain the required list

Carry out a Selection operation
to extract those tuples where
Client.clientNo =
Viewing.clientNo

i.e.,

σClient.clientNo = Viewing. ClientNo
((ΠclientNo, fName, IName(Client)) x
(ΠclientNo, propertyNo, comment(Viewing)))
08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 25
Select from the Cartesian Product

σClient.clientNo = Viewing. ClientNo ((ΠclientNo, fName, IName(Client)) x (ΠclientNo,
propertyNo, comment (Viewing)))

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 26


Decomposing complex operations

Relational algebra operations can be very complex

Can decompose them into a series of smaller relational algebra operations and
give a name to the results of intermediate expressions

Example, from the previous cartesian product

σClient.clientNo = Viewing.ClientNo ((ΠclientNo, fName, Iname(Client)) x (ΠclientNo, propertyNo, comment(Viewing)))

TempViewing(clientNo, propertyNo, comment) ← ΠclientNo, propertyNo, comment(Viewing)

TempClient(clientNo, fName, lName) ← ΠclientNo, fName, Iname(Client)

Comment(clientNo, fName, lName, vclientNo, propertyNo, comment) ← TempClient x
TempViewing

Result ← σclientNo = vclientNo(Comment)

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 27


Decomposing complex operations

Alternatively, use the Rename operation ρ (rho)

ρ gives a name to the result of a relational algebra
operation

Syntax

Either ρS(E) rename relation E as relation s, or

ρS(a1, a2, . . . , an)(E) Rename the attributes as well

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 28


Outline

Introduction

Relational Algebra

Introduction

Unary Operations

Set Operations

Join Operations

Division Operation

Aggregation and Grouping Operations

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 29


Join Operations

Typically, we want only combinations of the Cartesian product that
satisfy certain conditions

A Join operation can be used

Join

Combines two relations to form a new relation

Equivalent to performing a Selection operation over the Cartesian product
of the two operand relations

Several Join operations

Theta join, Equijoin (a particular type of Theta join), Natural join, Outer join,
Semijoin

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 30


Theta Join (θ Join)

R F S

Defines a relation that contains tuples satisfying the predicate F from the
Cartesian product of R and S

Predicate F is of the form R.ai θ S.bi, where θ may be one of the comparison
operators (<, ≤, >, ≥, =, ≠)

Theta Join is equivalent to the following cartesian product

R F S = σF(R x S)

Degree of theta join

Same as degree of cartesian product

i.e., sum of the degrees of the operand relations R and S

Equijoin

Theta join in which the predicate F contains only equality (=)
08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 31
Equijoin Example


Example

List the names and comments of all clients who have
viewed a property for rent

(ΠclientNo, fName, Iname(Client)) Client.clientNo = Viewing.clientNo
(ΠclientNo, propertyNo, comment(Viewing))

Or,

Results = TempClient Client.clientNo = Viewing.clientNo TempViewing

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 32


Natural Join

R S

An Equijoin of R and S over all common attributes x (i.e.,
attributes with the same name)

One occurrence of each common attribute is eliminated from the
result

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 33


Natural Join Example


Example

List the names and comments of all clients who have
viewed a property for rent

(ΠclientNo, fName, Iname(Client)) (ΠclientNo, propertyNo, comment(Viewing))

Or,

Results ← TempClient TempViewing

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 34


Outer Join

Often in joining two relations, a tuple in one relation does not have a
matching tuple in the other relation

i.e., no matching value in the join attributes

But we may still want tuples from one (or both) of the relations to appear in
the result even when there are no matching values in the other relation

Example

Joining Student relation with Fees relation will only bring out students who have paid
some fees

We may want to also know students who have not paid any fees

Outer join preserves tuples that would have been lost by other types
of join

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 35


Left Outer Join

R S

A join in which tuples from R that do not have matching
values in the common attributes of S are also included in the
result relation

Missing values in the second relation are set to null

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 36



Example, Produce a status report on property
viewings. (ΠpropertyNo, street, city(PropertyForRent)) (Viewing))

i.e., a relation consisting of the properties that
have been viewed with comments and those that
have not been viewed

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 37


Right Outer Join and Full Outer Join

Right Outer join

Left (natural) Outer join keeps every tuple in the lefthand
relation in the result

Similarly, Right Outer join keeps every tuple in the right-hand
relation in the result

Missing values in the first relation are set to null

Full Outer join

Keeps all tuples in both relations, padding tuples with nulls
when no matching tuples are found

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 38


Semijoin


Defines a relation that contains the tuples of R that
participate in the join of R with S satisfying the predicate F

Can be rewritten using Projection and Join operations

A is the set of all attributes for R

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 39


Semijoin Example


Example

List complete details of all staff who work at
the branch in Glasgow

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 40


Outline

Introduction

Relational Algebra

Introduction

Unary Operations

Set Operations

Join Operations

Division Operation

Aggregation and Grouping Operations

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 41


Division Operation

See details on pages 177-178 of text

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 42


Outline

Introduction

Relational Algebra

Introduction

Unary Operations

Set Operations

Join Operations

Division Operation

Aggregation and Grouping Operations

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 43


Aggregation and Grouping Operations

Aggregation operations

AL (R)

Applies the aggregate function list, AL, to the relation R to
define a relation over the aggregate list

AL contains one or more (<aggregate_function>, <attribute>)
pairs

The main aggregation functions

COUNT, SUM, AVG, MIN, MAX

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 44



(a) How many properties cost more than
£350 per month to rent?


(b) Find the minimum, maximum, and
average staff salary

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 45


Grouping Operation

GA AL (R)

Groups the tuples of relation R by the grouping attributes, GA

Then applies the aggregate function list AL to define a new
relation

AL contains one or more (<aggregate_function>, <attribute>) pairs.

The resulting relation contains the grouping attributes, GA, along
with the results of each of the aggregate functions

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 46



Find the number of staff working
in each branch and the sum of
their salaries

Steps

Group tuples according to
branchNo

Then use the aggregate functions
COUNT and SUM to produce the
required relation

08/01/25 Denis L. Nkweteyim@UB - IST601 (Database Management - Rel Algebra/Calculus) 47

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy