Relational Algebra
Relational Algebra
π
By relieving the brain of all unnecessary
work, a good notation sets it free to
concentrate on more advanced
problems, and, in effect,
increases the mental power of the
race.
Declarative
Declarative
query
query Algebra
Algebra Implementation
Implementation
language
language
Set Specific
Operators Operators
Set Operators: Introduction
Relational algebra received little attention until
the publication of E.F. Codd’s relational model
of data in 1970. Codd proposed such an algebra
as a basis for database query languages.
E.F. Codd’s Operators
Set operators from mathematical set theory
Union
Intersection
Difference
Cartesian Product
Set Operators
Relation is a set of tuples, so set operations
should apply: ∩, ∪, − (set difference)
Result of combining two relations with a set
operator is a relation
All its elements must be tuples having same
structure
Hence, scope of set operations limited to union
compatible relations.
Union Compatible Relations
Two relations are union compatible if
Both have same degree (number of columns)
Names of attributes are the same in both
Attributes with the same name in both
relations have the same domain (i.e.the data
type of the attributes should be same like
character,integer etc. )
Union compatible relations can be combined
using union,
union intersection,
intersection and set difference.
Union
• Returns a relation consisting of all tuples
appearing in either or both of two specified
relations.
• Duplicates are eliminated
• Both relations must be union compatible
• It is denoted by symbol ∪.
Example
A B
S# SNAME STATUS CITY S# SNAME STATUS CITY
Smith 20 London S1 Smith 20 London
S1
S4 Clark 20 London S2 Jones 20 Paris
AUB
S# SNAME STATUS CITY
S1 Smith 20 London
S4 Clark 20 London
S2 Jones 20 Paris
Intersection
• Returns a relation consisting of all tuples
appearing in both of two specified relations.
• It is denoted by symbol ∩ .
• Both relations must be union compatible.
Duplicates are eliminated
Example
A
S# SNAME STATUS CITY
S1 Smith 20 London
S4 Clark 20 London
B
S# SNAME STATUS CITY
S1 Smith 20 London
S4 Jones 20 Paris
A INTERSECTION B (A ∩ B)
S# SNAME STATUS CITY
S1 Smith 20 London
Difference
Returns a relation consisting of all tuples
appearing in the first and not the second of two
specified relations.It is denoted by symbol − .
Example
A
S# SNAME STATUS CITY
S1 Smith 20 London
S4 Clark 20 London
B
S# SNAME STATUS CITY
S1 Smith 20 London
S4 Jones 20 Paris
A DIFFERENCE B (A - B)
S# SNAME STATUS CITY
S4 Clark 20 London
Cartesian Product
Cross product or cross join
Result relation will have degree= sum of degrees of
both source relations
Result relation will have tuples= product of tuples of
both source relations
Returns a relation consisting of all possible tuples
that are a combination of two tuples, one from each
of two specified relations.It is denoted by the
symbol X.
R and S need not be union compatible
Example
a x ax
b * y = ay
c bx
(A)(B) b y
cx
cy
(A X B)
Cross-Product : detailed example
• Each row of R is paired with each row of S.
• Result schema has one field per field of S and R, with
field names `inherited’ if possible.
R R C R O S S S
A 1 A 1 A 1 F 4 A 1
B 2 A 1 C 2 F 4 C 2
D 3 A 1 D 3 F 4 D 3
F 4 A 1 E 4 F 4 E 4
E 5 B 2 A 1 E 5 A 1
S B 2 C 2 E 5 C 2
B 2 D 3 E 5 D 3
A 1 B 2 E 4 E 5 E 4
C 2 D 3 A 1
D 3 D 3 C 2
E 4 D 3 D 3
D 3 E 4
• Union and Intersection are cumulative
and associative operations therefore
AUB=BUA
A∩ B=B∩ A
AU(BUC)=(AUB)UC
A ∩ ( B ∩ C) = ( A ∩ B ) ∩ C
Project
Select
Join
Project
Retains only wanted columns from relation.
Projection operator has to eliminate duplicates
Denoted by symbol π.
Produces table containing subset of columns of
argument table
π (relation)
attribute list
Example
sid sname rating age
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
S
2
ρ ( Temp2, Temp1
Sailors)
π sname (Temp2)
π sname((Tempred ∩ Tempgreen )
Sailors )
Find the names of sailors who’ve
reserved all boats
• Uses division; schemas of the input
relations to / must be carefully chosen:
ρ (Tempsids, (π Re serves) / (π Boats))
sid, bid bid
π sname (Tempsids
Sailors )