0% found this document useful (0 votes)
11 views38 pages

04 - Relational Algebra and Calculus

The document outlines a lecture on Database Administration focusing on relational algebra and calculus, detailing the differences between procedural and non-procedural query languages. It explains various operations in relational algebra, such as SELECT, PROJECT, UNION, and JOIN operations, along with their syntax and examples. Additionally, it introduces relational calculus and its application in querying databases, emphasizing the importance of these concepts in data retrieval and manipulation.

Uploaded by

lawrencechikopa1
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)
11 views38 pages

04 - Relational Algebra and Calculus

The document outlines a lecture on Database Administration focusing on relational algebra and calculus, detailing the differences between procedural and non-procedural query languages. It explains various operations in relational algebra, such as SELECT, PROJECT, UNION, and JOIN operations, along with their syntax and examples. Additionally, it introduces relational calculus and its application in querying databases, emphasizing the importance of these concepts in data retrieval and manipulation.

Uploaded by

lawrencechikopa1
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/ 38

DATABASE SYTEMS

(DSYS-210)
BIT/CSS Year 2 – 2020/2021
Lecture
Lecture01:
04:Database Administration
Relational Algebra Basics
and Calculus

Allan N. Chongwe (Msc. Computer Science, Bsc. Information Technology)


Computer Science and Information Technology Dept
Malawi University of Science and Technology

Room #: 88 (Admin Building)


achongwe[at]must.ac.mw
1
Lesson Outcomes

By the end of this lecture you should be able to:

 Differentiate procedural from non-procedural languages.

 Explain the relational algebra operations.

 Explain the different types of joins in relational algebra

 Write relational algebra and relational calculus queries on


any given relations.

2
Introduction

Now that we are able to design using the relational model,


ER diagrams and normalization, this unit discusses how we
can retrieve data from the relations. We will look at how
the formal languages of relational algebra and relational
calculus are used in relational databases. We will also look at
the basic operators and operations used in relational
algebra. We shall conclude with a brief introduction to the
tuple relational calculus.
3
Introduction to Query Languages

• A query language is a type of language that is used to


store, manipulate and retrieve data from a database.
• A common example of a query language is the SQL.

• Query languages are specialized languages for asking


questions or queries that involve the data in a database.

• There are two query languages used in the relational


model: procedural and non-procedural query languages

4
Procedural Query language

• The user instructs the system to perform a series of


operations to produce the desired results.
• users tells what data to be retrieved from the database and how to
retrieve it.

• Relational algebra is an example of procedural query


language used on relational model.

5
Non-procedural query language

• Users instruct the system to produce the desired result


without specifying the step-by-step process.
• Users specify what data is to be retrieved from database
without specifying how, exactly, to retrieve it.

• Relational calculus is an example of non-procedural query


language used on relational model.

6
Relational Algebra

• The relational algebra operations enable a user to specify


basic retrieval requests as relational algebra expressions.
• The result of a retrieval is a new relation, which may have
been formed from one or more relations.

INPUT OPERATION OUTPUT


(relation) (relation)

• A sequence of relational algebra operations forms a relational


algebra expression
7
Advantages of Relational Algebra

1. It provides a formal foundation for relational model


operations.

2. It is used as a basis for implementing and optimizing


queries in the query processing and optimization
modules that are integral parts of relational database
management systems (RDBMSs).

3. Some of its concepts are incorporated into the SQL


standard query language for RDBMSs.
8
Relational Algebra operations

• There are two general categories of relational algebra


operations;

1. Basic
• SELECT, PROJECT, UNION, CROSS PRODUCT, SET
DIFFERENCE and RENAME operations

2. Derived
• JOIN operations

3. Extended
9
SELECT Operation

• The SELECT operator is denoted by the lowercase Greek


letter sigma (σ) and it is used to find the tuples (or rows)
in a relation (or table) which satisfy the given condition.

• The operation is used to choose a subset of the tuples


from a relation that satisfies a selection condition or a
predicate.

• We can consider the operation to be a filter that keeps


only those tuples that satisfy a given condition.
10
SELECT Operation (cont.)

• The operation has the following syntax:

σ<predicate or selection condition>(R)

• The relation resulting from the SELECT operation has the


same attributes as the input relation R.
SID SName Year
• To get a student whose ID is 105:
103 Enallah 2000

σSID 105 Kettie 2000


= 105(STUDENTS)
106 Orama 2000

11
PROJECT Operation

• ‘Selects’ certain columns from the table and discards the


other columns.
• This is useful when we are interested in only certain
attributes of a relation.

• The relation resulting from the PROJECT operation has


the same tuples as the input relation R.

12
PROJECT Operation (cont.)

• The operation has the following syntax:

Π<attribute list>(R)

• The relation resulting from the PROJECT operation has


the same tuples as the input relation R.

• To get names of students: SName


Enallah
ΠSName(STUDENTS) Kettie
Orama

13
UNION Operation

• The UNION operator is denoted by the symbol ∪ and it


is used to select all the tuples (or rows) from two
relations.

• The operation is used to combine data from two


relations.

• STUDENTS1 STUDENTS2
SID SName Year ID Name EYear
103 Enallah 2000 110 Wezzie 2010
105 Kettie 2000 900 Khumbo 2010
106 Orama 2000 991 Marita 2018
14
UNION Operation (cont.)
• The operation has the following syntax:

R1 ∪ R2
SID SName Year
• The relation resulting from the
103 Enallah 2000
UNION operation has the tuples
105 Kettie 2000
from both R1 and R2.
106 Orama 2000
• To get all students from the 2
110 Wezzie 2010
tables whose ID is 105: 900 Khumbo 2010
991 Marita 2018
STUDENTS1 ∪ STUDENTS2 15
UNION Operation (cont.)
• The rows (tuples) that are present in both the tables will
only appear once in the union set and its schema is
defined to be identical to the schema of the first relation,
R1.

• For the UNION operation to be done, the input


relations must be union-compatible.
• Same number of fields

• Have same domains

16
SET DIFFERENCE Operation

• SET DIFFERENCE (or MINUS) is denoted by the -


symbol.

• The result of this operation is a relation that includes all


tuples that are in relation R1 but not in relation R2.

• It allows us to find data that is in one relation but are


not in another.

17
CARTESIAN PRODUCT Operation

• CARTESIAN PRODUCT (or CROSS PRODUCT) is


denoted by the X symbol.

• It returns a relation instance whose schema contains all


the fields of R1 (in the same order as they appear in R1)
followed by all the fields of R2 (in the same order as
they appear in R2).

• The operation allows us to combine information from


any two relations.
18
CARTESIAN PRODUCT Operation (cont.)

• The operation has the following syntax:

R1 X R2

• It is possible for both R1 and R2 to contain one or more


fields having the same name. In this case the common
field names in the expression or resulting relation are
preceded by the relation name e.g. R1.field_name

19
RENAME Operation

• The RENAME operation is denoted by the lowercase


Greek letter ρ (rho) and it is used to rename a column or
a relation.

• It has the following syntaxes:

ρRn(R1) or

ρ(A1, A2, A3,…,An)(R1) or

ρRn(A1, A2, A3,…,An)(R1)


20
RENAME Operation (cont.)

• For example, to retrieve the SID and SName (as StudID


and StudName respectively) of students who enrolled in
the year 2000, we can write the expression as follows:

ρ(StudID, StudName)(ΠSID, SName (σEYear = 2000(STUDENTS))

SID SName Year


103 Enallah 2000
105 Kettie 2000
106 Orama 2000
21
INTERSECTION Operation

• The INTERSECTION operator is denoted by ∩ symbol


and it is used to select common rows (tuples) from two
relations.

• The operation returns a relation instance containing all


tuples that occur in both R1 and R2.

• The INTERSECTION operation has the following syntax:

R1 ∩ R2
22
DIVISION Operation

• The DIVISION operator is denoted by ÷ symbol and it is


useful in certain kind of queries but it is not implemented
in SQL.

• The operation is applied to two relations R1 ÷ R2, where


the attributes of R2 are a subset of the attributes of R1.

• The DIVISION operation has the following syntax:

R1 ÷ R2
23
JOIN Operations

• A join is a combination of a Cartesian product followed


by a selection and projection process.
• A join operation pairs two tuples from different relations, if
and only if a given join condition is satisfied.

• Types of joins:
1. Natural Join

2. Theta Join

3. SEMIJOIN operation

4. ANTIJOIN operation
24
Natural Join Operation

• The natural join is a binary operation that allows us to


combine certain selections and a Cartesian product into
one operation.

• The natural join operation forms a Cartesian product of


its two arguments or relations, performs a selection
forcing equality on those attributes that appear in both
relation schemas, and finally removes duplicate
attributes.
25
Natural Join Operation (cont.)

• The operation has the following syntax:

R1 ⋈ R2
SID SName EYear SID Location
101 Benjamin 2002 101 Bangwe
102 Wezzie 1999 102 Chilobwe
104 Majorie 2000 103 Mbayani

• To get a list of students and their locations.

STUDENTS ⋈ LOCATIONS
SID SName EYear Location
101 Benjamin 2002 Bangwe
102 Wezzie 1999 Chilobwe
26
Theta Join Operation

• The theta join is an operation that allows us to combine


certain selections and a Cartesian product into one
operation based on the join condition.

• The operator is denoted by the symbol ⋈θ and the


operation has the following syntax:
R1 ⋈θ R2
27
Theta Join Operation (cont.)

SID SName EYear SID Location


101 Benjamin 2002 101 Bangwe
102 Wezzie 1999 102 Chilobwe
104 Majorie 2000 103 Mbayani

• To get a list of students who joined after 1999


STUDENTS ⋈EYear > 1999 LOCATIONS

SID SName EYear Location


101 Benjamin 2002 Bangwe

28
Theta/Equijoin Operation (cont.)
• When theta join uses only equality comparison operator,
it is said to be equijoin.

SID SName EYear SID Location


101 Benjamin 2002 101 Bangwe
102 Wezzie 1999 102 Chilobwe
104 Majorie 2000 103 Mbayani

• To get a list of students staying in “Bangwe”


STUDENTS ⋈Location = “Bangwe” LOCATIONS

SID SName EYear Location


101 Benjamin 2002 Bangwe 29
SEMIJOIN Operation

• The SEMIJOIN is a joining similar to the natural join. The


result is the set of all tuples in R1 for which there is a
tuple in R2 that is equal on their common attribute
names (left semijoin).

• The difference from a natural join is that other columns


of R2 do not appear. The operator is denoted by the
symbol ⋉ and has the following syntax:

R1 ⋉ R2
30
ANTIJOIN Operation

• The ANTIJOIN is a joining similar to the SEMIJOIN. The


result is the set of all tuples in R1 for which there is no
tuple in R2 that is equal on their common attribute
names.

• It is more like an opposite of the semijoin. The


ANTIJOIN operator is denoted by the symbol ▷ and has
the following syntax:

R1 ▷ R2
31
Extended Operations

• Extended relational-algebra operations are those


operations that provide the ability to write queries that
cannot be expressed using the basic and derived
relational-algebra operations.

1. Generalized projection

2. Aggregation operations
32
Aggregation operation

• The aggregate operation denoted by the symbol


(letter G in calligraphic font).

• It allows the use of aggregate functions on sets of values.


Aggregate functions take a collection of values and
return a single value as a result.
• For example, the aggregate function sum takes a collection of
values and returns the sum of the values. Thus, the function
sum applied on the collection {1, 1, 3, 4, 4, 11} returns the
value 24
33
Relational Calculus

• In a relational calculus expression, there is no order of


operations to specify how to retrieve the query result –
only what information the result should contain. This is
the main distinguishing feature between relational
algebra and relational calculus.
• the standard query language (SQL) for RDBMSs has some of its
foundations in it.

34
Tuple Relational Calculus

• Tuple relational calculus is used for selecting those tuples


that satisfy the given condition.
• Each tuple variable usually ranges over a particular database
relation, meaning that the variable may take as its value any
individual tuple from that relation.

• A simple tuple relational calculus query has the following


syntax:

{t | COND(t)}
35
Tuple Relational Calculus (cont.)

• Tuple relational calculus is used for selecting those tuples


that satisfy the given condition.
• Each tuple variable usually ranges over a particular database
relation, meaning that the variable may take as its value any
individual tuple from that relation.

• A simple tuple relational calculus query has the following


syntax:

{t | COND(t)}
36
Tuple Relational Calculus (cont.)

• Example

Get the employee names for all employees in finance


department whose salary is more than 80,000.00

• Solution
{t.Fname, t.Lname | EMPLOYEES(t) AND
t.DeptName=”Finance” AND t.Salary > 80000}

37
Reference and Further Reading

Please consult Unit 4 of the Module Guide. You can find all the
references for further reading at the end of the unit.

38

You might also like