Relational Algebra - 6
Relational Algebra - 6
Relational Algebra - 6
Reference Book :
Principles of Distributed Database Systems
EDITION THIRD
Author: M. Tamer Ozsu
Course:DDB
Instructor: Ibrar Afzal
1
Relational Algebra
Procedural vs .non-procedural, or declarative
“Pure” languages:
Relational algebra
Tuple relational calculus
Domain relational calculus
The above 3 pure languages are equivalent in computing
power
relational algebra is procedural in that the user is predictable
and use high-level operators
Not turning-machine equivalent
consists of 6 basic operations
2
Relational Algebra Operators
3
Select Operation – selection of rows (tuples)
The SELECT operation (denoted by (sigma)) acts as a filter and
select a subset of the tuples from a relation
Relation r
4
Project Operation – selection of columns (Attributes)
PROJECT Operation is denoted by (pi) and creates a vertical
partitioning for columns (attributes).
Relation r:
A,C (r)
5
Union of two relations
operation, denoted by and attributes must be type compatible (have same or compatible
Relations r, s:
r s:
6
Set difference of two relations
Called MINUS or EXCEPT and result of r – s, is a relation that includes all
tuples that are in r but not in s
Relations r, s:
r – s:
7
Set intersection of two relations
Result of the intersection operation r s, is a relation that
includes all tuples that are in both r and s
Relation r, s:
r s
Note: r s = r – (r – s)
8
Joining two relations -- Cartesian-product
Operation is used to combine tuples from two relations and resulting relation
state has one tuple for each combination of tuples—one from r and one from s.
Relations r, s:
r x s:
9
Cartesian-product – naming issue
Relations r, s: B
r x s: r.B s.B
10
Composition of Operations
Can build expressions using multiple operations
Example: A=C (r x s)
r x s
A=C (r x s)
11
Joining two relations – Natural Join
12
Natural Join Example
13
Notes about Relational Languages
Each Query input is a table (or set of tables)
Each query output is a table.
All data in the output table appears in one of the
input tables
Relational Algebra is not Turning complete
Can we compute:
SUM
AVG
MAX
MIN
14
Thanks
15