3. Objectives of the Topic.Objectives of the Topic.
1. Introduction to Relational Algebra.
2. What is Relational Algebra?
3 What is Algebra Operation?
3.Basic Operation of Relation algebra.
i. Selection (A α alpha)
ii. Projection (Π π pi)
iii. Cross-product (X χ chi)
iv. Union
v. Intersection
vi. Set-difference
S S1 2∪
S S1 2∩
S S1 2−
Tuesday, November 10, 2015 3CSE&S
4. Introduction to Relational Algebra
Introduced by E. F.
Codd in 1970.
Codd proposed such
an algebra as a basis
for database query
languages.
Tuesday, November 10, 2015 4CSE&S
5. What is Relational Algebra?
Relational Algebra is formal description of
how relational database operates.
It is a procedural query language, i.e. user
must define both “how” and “what” to
retrieve.
It consists of a set of operators that
consume either one or two relations as
input. An operator produces one relation
as its output.
Tuesday, November 10, 2015 5CSE&S
6. What is Algebra Operations?
Unary Operations - operate on one
relation. These include select, project
and rename operators.
Binary Operations - operate on pairs
of relations. These include union, set
difference, division, cartesian product,
equality join, natural join, join and
semi-join operators.
Tuesday, November 10, 2015 6CSE&S
7. Select Operator (б )
The Select operator selects tuples that satisfies a
predicate; e.g. retrieve the employees whose
salary is 30,000
б Salary = 30,000 (Employee)
Conditions in Selection:
Simple Condition: (attribute)(comparison)(attribute)
(attribute)(comparison)(constant)
Comparison: =,≠,≤,≥,<,>
Condition: combination of simple conditions with
AND, OR, NOT
Tuesday, November 10, 2015 7CSE&S
8. Select Operator Example
Name Age Weight
Nizam 34 80
Sana 28 64
Anwar 29 70
Hamal 54 54
Asad 34 80
Name Age Weight
Nizam 34 80
Hamal 54 54
Asad 34 80
Person бAge≥34(Person)
Name Age Weight
Hamal 54 54
бAge=Weight(Person)
Tuesday, November 10, 2015 8CSE&S
9. Project Operator (∏)
Project (∏) retrieves a column. Duplication
is not permitted.
e.g., name of employees:
∏ name(Employee)
e.g., name of employees earning more
than 80,000:
∏ name(бSalary>80,000(Employee))
Tuesday, November 10, 2015 9CSE&S
10. Project Operator Example
Name Age Salary
Zarak 34 80,000
Fida 28 90,000
Kabeer 29 70,000
Imran 54 54,280
Salma 34 40,000
Name
Zarak
Fida
Kabeer
Imran
Salma
Employee
∏ name(Employee)
Tuesday, November 10, 2015 10CSE&S
11. Project Operator Example
Name Age Salary
Zarak 34 80,000
Fida 28 90,000
Kabeer 29 70,000
Imran 54 54,280
Salma 34 40,000
Name
Fida
Employee бSalary>80,000(Employee)
Name Age Salary
Fida 28 90,000
∏ name(бSalary>80,000(Employee))
Tuesday, November 10, 2015 11CSE&S
12. Cartesian Product (X)
In mathematics, it is a set of all pairs of
elements (x, y) that can be constructed
from given sets, X and Y, such that x
belongs to X and y to Y.
It defines a relation that is the
concatenation of every tuple of relation R
with every tuple of relation S.
Tuesday, November 10, 2015 12CSE&S
13. Cartesian Product Example
Name Age Weight
Nizam 34 80
Sana 28 64
Anwar 29 70
City
Loralai
Khuzdar
Person City
Person X City
Name Age Weight City
Nizam 34 80 Loralai
Sana 28 64 Loralai
Anwar 29 70 Loralai
Nizam 34 80 Khuzdar
Sana 28 64 Khuzdar
Anwar 29 70 Khuzdar
Tuesday, November 10, 2015 13CSE&S
14. Union Operator (U)
The union operation is denoted U as in set
theory. It returns the union (set union) of
two compatible relations.
For a union operation r U s to be legal, we
require that,
r and s must have the same number of
attributes.
The domains of the corresponding
attributes must be the same.
As in all set operations, duplicates are
eliminated.
Tuesday, November 10, 2015 14CSE&S
15. Union Operator Example
F Name L Name
Ahmad Ashraf
Sadam Arif
Niaz Shah
Asif Wajid
Ali Qabool
F Name L Name
Sadam Arif
Kamal Noor
Asif Wajid
Salman Khan
Shabeer Ali
Student
Professor
F Name L Name
Ahmad Ashraf
Sadam Arif
Niaz Shah
Asif Wajid
Ali Qabool
Kamal Noor
Salman Khan
Shabeer Ali
Student U Professor
Tuesday, November 10, 2015
15CSE&S
16. Intersection Operator (∩)
Denoted as ∩ . For relations R and S, intersection
is 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.
Expressed using basic operations:
R ∩ S = R – (R – S)
Tuesday, November 10, 2015 16CSE&S
17. Intersection Operator Example
F Name L Name
Ahmad Ashraf
Sadam Arif
Niaz Shah
Asif Wajid
Ali Qabool
F Name L Name
Sadam Arif
Kamal Noor
Asif Wajid
Salman Khan
Shabeer Ali
Student
Professor
F Name L Name
Sadam Arif
Asif Wajid
Student ∩ Professor
Tuesday, November 10, 2015 17CSE&S
18. Set Difference Operator (–)
For relations R and S,
Set difference R - S, defines a relation
consisting of the tuples that are in relation
R, but not in S.
Set difference S – R, defines a relation
consisting of the tuples that are in relation
S, but not in R.
Tuesday, November 10, 2015 18CSE&S
19. Set Difference Operator Example
F Name L Name
Ahmad Ashraf
Sadam Arif
Niaz Shah
Asif Wajid
Ali Qabool
F Name L Name
Sadam Arif
Kamal Noor
Asif Wajid
Salman Khan
Shabeer Ali
Student Professor
F Name L Name
Ahmad Ashraf
Niaz Shah
Ali Qabool
Student - Professor
F Name L Name
Kamal Noor
Salman Khan
Shabeer Ali
Professor - Student
Tuesday, November 10, 2015 19CSE&S