Professional Documents
Culture Documents
RELATIONAL ALGEBRA
Relational Algebra is a system for manipulating
tables or relation.
It is a mathematical system, with a set of abstract
rules.
It could be used as a database query language in
its own right, and has been so in experimental
systems.
It is the main basis for practical database query
language such as SQL.
H.Seegobin
Relational Algebra
Relational Algebra
Relational Operations
Two types of operation in relational
algebra.
Traditional Set Operators.
Union, Intersection, Difference, Product.
Property of closure.
H.Seegobin
Relational Algebra
H.Seegobin
Relational Algebra
Relational Algebra
UNION
Returns a relation containing all tuples that
appear in either or both of two specified
relations.
Example: R Union S giving T
A
1
2
R
A
2
3
S
H.Seegobin
A
1
2
3
T
Relational Algebra
Example:UNION
EMPNAME
F JONES
K CHAN
S ABDUL
EMPNO
2
3
6
9
1
EMPNO
2
3
6
9
EMPNAME
P SMITH
K CHAN
J PETER
S ABDUL
EMPNAME
P SMITH
K CHAN
J PETER
S ABDUL
F JONES
H.Seegobin
Relational Algebra
Example:UNION
S_NAME
SMITH
JONES
BROWN
WHITE
DEGREE
CIS
CHF
CHF
S_NAME
WHITE
DEGREE
CIS
CHF
CHF
CHF
H.Seegobin
DEGREE
CHF
Relational Algebra
Intersection
Returns a relation containing all tuples that
appear in both of two specified relations.
Example: R Intersection S giving T
A
1
2
R
&
A
2
3
A
2
H.Seegobin
Relational Algebra
Example: Intersection
German_speakers INTERSECT French_speakers giving
result
What query did you just do?
Find all employee who can speak both German and
French
EMPNO
1
3
9
EMPNAME
F JONES
K CHAN
S ABDUL
EMPNO
2
3
6
9
EMPNAME
P SMITH
K CHAN
J PETER
S ABDUL
EMPNO
3
9
EMPNAME
K CHAN
S ABDUL
H.Seegobin
Relational Algebra
Example: Intersection
Current_marks INTERSECT old_marks giving result
S_NAME
SMITH
SMITH
SMITH
JONES
BROWN
BROWN
JONES
JONES
BROWN
WHITE
WHITE
WHITE
COURSE
SYS
DB
GR
GR
SYS
DB
AI
SYS
AI
SYS
DB
GR
MARK
A
B
A
A
B
C
D
C
B
D
B
C
S_NAME
SMITH
SMITH
JONES
BROWN
JONES
JONES
BROWN
OTHER
OTHER
COURSE
SYS
GR
GR
SYS
AI
SYS
AI
FR
ST
MARK
A
A
A
B
D
C
B
F
F
S_NAME
SMITH
SMITH
JONES
BROWN
JONES
JONES
BROWN
COURSE
SYS
GR
GR
SYS
AI
SYS
AI
MARK
A
A
A
B
D
C
B
H.Seegobin
Relational Algebra
Difference (Minus)
Returns a relation containing all tuples that
appear in the first and not the second of
two specified relations.
Example: R Minus S giving T
A
1
2
R
A
2
3
A
1
T
H.Seegobin
Relational Algebra
EMPNAME
F JONES
K CHAN
S ABDUL
EMPNO
1
EMPNO
2
3
6
9
EMPNAME
P SMITH
K CHAN
J PETER
S ABDUL
EMPNAME
F JONES
H.Seegobin
Relational Algebra
COURSE
SYS
DB
GR
GR
SYS
DB
AI
SYS
AI
SYS
DB
GR
MARK
A
B
A
A
B
C
D
C
B
D
B
C
S_NAME
SMITH
SMITH
JONES
BROWN
JONES
JONES
BROWN
OTHER
OTHER
S_NAME
BROWN
WHITE
WHITE
WHITE
SMITH
H.Seegobin
COURSE
SYS
GR
GR
SYS
AI
SYS
AI
FR
ST
COURSE
DB
SYS
DB
GR
DB
MARK
A
A
A
B
D
C
B
F
F
MARK
C
D
B
C
B
Relational Algebra
Cartesian Product
Returns a relation containing all possible tuples that are
a combination of two tuples, one from each of two
specified relations.
Combines every row of the first with every row of the
second.
b1
b2
b3
S
H.Seegobin
B
b1
b1
b2
b2
b3
b3
a2
a1
a2
a1
a2
T
Relational Algebra
F JONES
K CHAN
S ABDUL
2
3
6
9
EMPNO
1
1
1
1
EMPNAME
F JONES
F JONES
F JONES
F JONES
EMPNO
2
3
6
9
EMPNAME
P SMITH
K CHAN
J PETER
S ABDUL
3
3
3
3
9
9
9
9
K CHAN
K CHAN
K CHAN
K CHAN
S ABDUL
S ABDUL
S ABDUL
S ABDUL
2
3
6
9
2
3
6
9
P SMITH
K CHAN
J PETER
S ABDUL
P SMITH
K CHAN
J PETER
S ABDUL
H.Seegobin
P SMITH
K CHAN
J PETER
S ABDUL
Relational Algebra
Restriction
Returns a relation containing all tuples from a
specified relation that satisfy a specified
condition.
Logical Operation OR, AND, NOT may be used.
A
a1
a2
a3
a4
B
b1
b2
b3
b4
A
a2
H.Seegobin
B
b2
Relational Algebra
Example: Restriction
RESTRICT Language_Skill WHERE Skill = French giving
result
Language_Skill
EMPNO
1
2
2
2
3
3
6
6
9
9
9
5
SKILL
GERMAN
TYPING
SHORTHAND
FRENCH
GERMAN
FRENCH
FRENCH
TYPING
FRENCH
GERMAN
COBOL
PIANO
Result
EMPNO
2
3
6
9
H.Seegobin
SKILL
FRENCH
FRENCH
FRENCH
FRENCH
Relational Algebra
Project
Returns a relation containing all (sub)
tuples that remain in a specified relation
after specified attributes have been
removed.
PROJECT is another operations which
can be used on single relations only.
Whereas RESTRICT returns set of
complete tuples, PROJECT return tuples
with a restricted set of attributes.
H.Seegobin
Relational Algebra
Example: Project
Project R over B giving result
R
Result
A
a1
a2
a3
a4
B
b2
b2
b3
b4
B
b2
b3
b4
H.Seegobin
Relational Algebra
Example: Project
Project Language Skill over Empno giving Result
Language_Skill
EMPNO
1
2
2
2
3
3
6
6
9
9
9
5
SKILL
GERMAN
TYPING
SHORTHAND
FRENCH
GERMAN
FRENCH
FRENCH
TYPING
FRENCH
GERMAN
COBOL
PIANO
Result
EMPNO
1
2
3
6
9
5
H.Seegobin
Relational Algebra
Join
Returns a relation containing all possible tuples
that are a combination of two tuples, one from
each of two specified relations, such that the two
tuples contributing to any given combination
have a common value for the common attributes
of the two relations (and that common value
appears just once, not twice, in the result).
Natural join is a generalization of both Cartesian
product and intersection.
For common columns it works like intersection.
For the Unique columns of either table, it works
like Cartesian product
H.Seegobin
Relational Algebra
Join
Join R and S over B giving relation C
A
a1
a2
a3
a4
R
B
b2
b2
b3
b4
B
b1
b1
b2
b4
C
c1
c2
c3
c1
S
H.Seegobin
A
a1
a2
a4
B
b2
b2
b4
C
c3
c3
c1
Relational Algebra
Join
Join German_Speakers and Language_Skill over
Empno giving Result
EMPNO
1
3
9
EMPNAME
F JONES
K CHAN
S ABDUL
EMPNO
1
2
2
2
3
3
6
6
9
9
9
5
SKILL
GERMAN
TYPING
SHORTHAND
FRENCH
GERMAN
FRENCH
FRENCH
TYPING
FRENCH
GERMAN
COBOL
PIANO
H.Seegobin
EMPNO
1
3
3
9
9
9
EMPNAME
F JONES
K CHAN
K CHAN
S ABDUL
S ABDUL
S ABDUL
SKILL
GERMAN
GERMAN
FRENCH
FRENCH
GERMAN
COBOL
Relational Algebra
Divide
Takes two relations, one binary and one Unary and
builds a relation consisting of all values of one attribute
of the binary relation that match (in the other attributes)
all values in the Unary relation.
Divide R by S over C giving Relation C
A
a1
a2
a1
a1
a1
a1
a1
a1
B
b1
b1
b2
b2
b1
b2
b1
b1
R
C
c1
c1
c1
c2
c2
c3
c4
c5
Divide by
C
c1
H.Seegobin
A
a1
a2
a1
B
b1
b1
b2
Relational Algebra
Summary
Relational Algebra is a system
manipulating tables or relation.
for