04 - Relational Algebra and Calculus
04 - Relational Algebra and Calculus
(DSYS-210)
BIT/CSS Year 2 – 2020/2021
Lecture
Lecture01:
04:Database Administration
Relational Algebra Basics
and Calculus
2
Introduction
4
Procedural Query language
5
Non-procedural query language
6
Relational Algebra
1. Basic
• SELECT, PROJECT, UNION, CROSS PRODUCT, SET
DIFFERENCE and RENAME operations
2. Derived
• JOIN operations
3. Extended
9
SELECT Operation
11
PROJECT Operation
12
PROJECT Operation (cont.)
Π<attribute list>(R)
13
UNION Operation
• 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.
16
SET DIFFERENCE Operation
17
CARTESIAN PRODUCT Operation
R1 X R2
19
RENAME Operation
ρRn(R1) or
R1 ∩ R2
22
DIVISION Operation
R1 ÷ R2
23
JOIN Operations
• Types of joins:
1. Natural Join
2. Theta Join
3. SEMIJOIN operation
4. ANTIJOIN operation
24
Natural Join Operation
R1 ⋈ R2
SID SName EYear SID Location
101 Benjamin 2002 101 Bangwe
102 Wezzie 1999 102 Chilobwe
104 Majorie 2000 103 Mbayani
STUDENTS ⋈ LOCATIONS
SID SName EYear Location
101 Benjamin 2002 Bangwe
102 Wezzie 1999 Chilobwe
26
Theta Join Operation
28
Theta/Equijoin Operation (cont.)
• When theta join uses only equality comparison operator,
it is said to be equijoin.
R1 ⋉ R2
30
ANTIJOIN Operation
R1 ▷ R2
31
Extended Operations
1. Generalized projection
2. Aggregation operations
32
Aggregation operation
34
Tuple Relational Calculus
{t | COND(t)}
35
Tuple Relational Calculus (cont.)
{t | COND(t)}
36
Tuple Relational Calculus (cont.)
• Example
• 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