You are on page 1of 26

Relational Algebra

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

Rules for a Relation


All the column names must be different.
All the rows are different; i.e., two identical
rows can be combined.
The column order is immaterial.
The row order is immaterial.
Only one value is allowed at any
row/column intersection.
H.Seegobin

Relational Algebra

Relational Operations
Two types of operation in relational
algebra.
Traditional Set Operators.
Union, Intersection, Difference, Product.

Special Relational Operators.


Restriction, Projection, Join, Division.

Property of closure.

H.Seegobin

Relational Algebra

Syntax of Relational Algebra


Relation-name-1 PRODUCT Relation-name-2
[GIVING relation-name-3]
Relation-name-1 UNION Relation-name-2
[GIVING relation-name-3]
Relation-name-1 DIFFERENCE Relation-name2 [GIVING relation-name-3]
Relation-name-1 INTERSECT Relation-name-2
[GIVING relation-name-3]

H.Seegobin

Relational Algebra

Syntax of Relational Algebra


RESTRICT relation-name-1 WHERE condition
[GIVING relation-name-2]
PROJECT relation-name-1 OVER attribute-list
[GIVING relation-name-2]
JOIN relation-name-1 AND relation-name-2
[OVER common-attribute-list] [GIVING relationname-3]
DIVIDE dividend-relation-name BY divisorrelation-name [OVER common-attribute-list]
[GIVING relation-name-1]
H.Seegobin

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

German_speakers UNION French_speakers giving


result
What query did you just do?
Find all employee who can speak either German or
French
EMPNO
1
3
9

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

3rd_Year_Student UNION 2nd_Year_Student


giving result
S_NAME
SMITH
JONES
BROWN

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

Example: Difference (Minus)


German_speakers MINUS French_speakers giving
result
What query did you just do?
Find all employee who can speak only German but not
French
EMPNO
1
3
9

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

Example: Difference (Minus)


Current_Marks MINUS 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

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.

Example: R Product S giving T


*
= Aa1
A
B
a1
a2

b1
b2
b3

S
H.Seegobin

B
b1
b1
b2
b2
b3
b3

a2
a1
a2
a1
a2
T

Relational Algebra

Example: Cartesian Product


German_Speakers Product French_Speakers giving
EMPNO
EMPNAME
result EMPNO EMPNAME
1
3
9

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.

Example: Restrict R where B = b2 giving result


R

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.

Two types of operation in relational


algebra.
Traditional Set Operators.
Union, Intersection, Difference, Product.

Special Relational Operators.


Restriction, Projection, Join, Division.
H.Seegobin

for

You might also like