Lecture 5
The Relational Algebra


Theoretical Knowledge
      Select Operation
      Project Operation
      Renaming of Attributes
      Union
      Intersection
      Difference
      Cartesian Product
      Join Operation
      Equijoin Operation
      Natural Join
      Division
      Aggregate Functions
Practical Knowledge


Definition: Theoretical language with operators that work on one or two relations to produce another relation

Select Operation- selects all tuples that satisfy the selection condition from a relation

s <selection condition>(r)

<selection condition> ?

  1. <attribute name>
  2. <comparison operations> ? {=, <, ? , >, ? , ? }
  3. <logical operations> ? {? ,? ,? }
R
 
a b c
a b c
d e f
c b a
s b=br
 
a b c
a b c
c b a


Project Operation - produce a new relation with only some of the attributes of r and remove duplicate tuples

p <selection condition>(r)

r
 
a b c
a b c
d e f
c b a
pa,c (r)
 
 a c
a c
d f
c a


Renaming of Attributes

r(<attribute list>) ¬ operation

r
 
 a b c
a b c
d e f
c b a
 

r1(d) ¬ pc(r)
 
 d
c
f
a


Union - Produces a relation that includes all the tuples in r or s or both r and s. r and s must be union compatible - r ? s

Properties:

1. r ?s = s ? r

2. r ? (s ? t) = (r ? s) ? t
 
r s
a b c
a b c
d e f
c b a
 
a b c
b g a
c b a
 
r ? s
 
a
a b c
d e f
c b a
b g a


Intersection - Produces a relation that includes all the tuples in both r and s. R and s must be union compatible - r ? s

Properties:

1. r ? s = s ? r

2. r ? (s ? t) = (r ? s) ? t

 
 
r s
a b c
a b c
d e f
b a
 
a b c
b g a
c b a
 
r ? s
 
a
c b a


Difference - Produces a relation that includes all the tuples in r that are not in s. R and s must be union compatible ® r - s

Property:

1. r - s ? s - r
 
r s
a b c
a b c
d e f
c b a
 
a b c
b g a
c b a
 
r - s
 
 a b c
a b
d e f
 

s - r
 
 a b c
b g a


Cartesian Product - Produces a relation that has the attributes of r and s and includes as tuples all possible combinations of tuples from r and s ® r x s
 
r s
a b c
a b c
d e f
c b a
 
d e f
b g a
c b a
 
r x s
 
a b c d e f
a b c b g a
a b c c b a
d e f b g a
d e f c b a
c b a b g a
c b a c b a


Join Operation - Produce all combinations of tuples from r and s that satisfy the join condition (theta join) ® r  <join condition>s

<join condition> ?


Properties:

1. r s = s r

2. r (s t) = (r s) t
 
r s
a b c
1 2 3
4 5 6
7 8 9
 
d e
3 1
6 2
 
r b<ds
 
a b c e
1 2 3 3 1
1 2 3 6 2
4 5 6 6 2


Equijoin Operation - Produce all combinations of tuples from r and s that satisfy a join condition with only equality comparisons ? r*<join condition>s

<join condition> ?


Natural Join - Same as equijoin except that the join attributes of s are not included in the resulting relation; if the join attributes have the same names, they do not have to be specified at all
 
r s
a b c
1 2 3
4 5 6
 
d e
2 7
3 5
 
r*b=ds
 
 a b c e
1 2 3 7


Relational Algebra Operations

 {s , p , ? , ? , x} - complete set

r ? s = (r ? s) - ((r - s) ? (s-r))

r <condition>s = s <condition>(rxs)


Division - Produce a relation t(x) that includes all tuples t{x} in r(z) that appear in r in combination with every tuple from s(y), where z = x ? y.

t = r + s;

t1¬ p y(r);

t2¬ p y((sxt1) - r);

t ¬ t1 - t2

r
 
a b
a1 b1
a2 b1
a3 b1
a4 b1
a1 b2
a3 b2
a2 b3
a3 b3
a4 b3
a1 b4
a2 b4
a3 b4
 
 
s t
a
a1
a2
a3
 
b
b1
b4
 


Aggregate Functions

function operation = <grouping attributes> f <function list> (relation name)

 

function list = sum | average | maximum |

minimum | count

 

f average_a (r)

r
 
a
5 2 1
6 1 3
2 4 7
3 6 0



 
average_a
4

Practical Knowledge

Three Primary Functions:

select

project

join

 

May be combined in different ways and with different syntax in different implementations.

But must be implemented somehow.

Functions extract selected data.

Extracted data put into temporary storage in new relations .

New relations can be further manipulated and/or stored as permanent relations if required.


Example Relations

a) employee(empno,ename,esalary,dept)

employee
 
empno ename esalary dept
       
 

b) department(dept,location)

department
 
dept location
   
 

c) job(jname,budget)

job
 
jname budget
   
 

d) assignment(jname,empno,hours)

assignment
 
jname empno hours
     

select

Extracts tuples from a relation subject to required conditions on attributes in the relation.


Example

select employee where salary > 13000 [giving r1]

[r1]
 
empno ename esalary dept
146 harvey 15000 sales
468 mendoza 14000 planning


project

Extracts columns from a relation in a named order by attribute.

Example

Project employee over ename, dept
 
ename dept
smith sales
jones planning


join

Combines relations which have a common attribute. generates a temporary relation containing all attributes from both relations.

Example

Join employee and assignment over empno

empno ename esalary dept jname hours
134 smith 12000 sales projb 9.0
146 harvey 15000 sales proja 3.4
 *Suppose you wish to find out the names, jobs and hours worked on them by people in sales dept.

You can do this by nesting operations:

project (

over ename, jname, hours
 
ename jname hours
smith projb 9.0
harvey proja 3.4

The relation is constructed by working from innermost nesting outwards.


Relational algebra is

 
[Top][Learning Units]
[Previous][Next]