You are on page 1of 31

Relational Algebra

Operators
What is an Algebra
Mathematical system consists of:
Operands --- variables or values from which
new values can be constructed.
Operators --- symbols denoting procedures that
construct new values from given values.
What is Relational Algebra?
An algebra whose operands are relations or
variables that represent relations.
Operators are designed to do the most
common things that we need to do with
relations in a database.
The result is an algebra that can be used as a
query language for relations.
Relational Databases
Relational Model Operators
Original paper on relational Model :
E F Codd - A Relational Model of Data for Large Shared
Data Banks, CACM 13, No. 6, June 1970
Introduced 8 basic Operators to manipulate data
within relation parts of tables of a relational database.
Cartesian Basic Set
union intersection difference
Product Operators

restrictions projection join division


Relational Databases
Relational Model Operators
The 8 basic operators are all incorporated in the
standard, international, relational database language
SQL. SQL supports other DML operators beyond these 8
operators.
Cartesian Product of two sets is the set of all ordered pairs
of elements such that the first element in each pair belongs to
the first set and the second element from the second set
S1 = {1,2} S2 = {4,5,6}
S1 x S2 = {(1,4),(1,5),(1,6),(2,4),(2,5),(2,6)}
Relational Databases
Relational Model Operators
Cartesian Product
Table FEMALE Table MALE
Name Job Name Job
Shyama Clerk Shankar Sales
Reva Sales Ranjit Clerk

{(Shyama,Clerk),(Shankar,Sales)}
{(Shyama,Clerk),(Ranjit,Clerk)}
{(Reva,Sales),(Shankar,Sales)}
{(Reva,Sales),(Ranjit,Clerk)}
Relational Databases
Relational Model Operators
Cartesian Product
Table FEMALE_ MALE
FemaleName FemaleJob MaleName MaleJob
Shyama Clerk Shankar Sales
Reva Sales Ranjit Clerk
Shyama Clerk Ranjit Clerk
Reva Sales Shankar Sales
Relational Databases
Relational Model Operators
Restriction
A set can have any number of subsets
A set is a subset of another if all its members are also
members of the other set
S1 = {1,2,3,4,5} S2 = {2,3,4}
S2 is a subset of S1.

A restriction operation places restriction like =, <>, >, >=, <=


while making the subset
Relational Databases
Relational Model Operators
Restriction
Table FEMALE_ MALE
FemaleName FemaleJob MaleName MaleJob

Shyama Clerk Ranjit Clerk


Reva Sales Shankar Sales

same designation
Relational Databases
Relational Model Operators
Projection
The projection operation on a table forms another table by
copying specified columns( both header and body) from the
original table.
Table EMPLOYEE
Id name age salary
1 Shankar 32 5000
2 Ranjit 35 5600
3 Sampa 21 4000
Relational Databases
Relational Model Operators
Division
Table: EMPLOYEE
name language Table: LANGUAGE
Sharan Cobol Language
Shankar Fortran Cobol
Rahul Ada PL/1
Sharan PL/1
Shankar Cobol
Table: Cobolpl/1
Name
Sharan
Relational Databases
Relational Model Operators
Join

R and S being tables and with union compatible


columns in common, a join of R and S over these
columns will result in a table with header
derived by concatenating the headers of R and S
and whose tuples are the concatenation of all
those tuples in R and S where the values in the
common columns conform to the condition.
Natural Join
A frequent type of join connects two
relations by:
Equating attributes of the same name, and
Projecting out one copy of each pair of equated
attributes.
Called natural join.
Denoted R3 := R1 JOIN R2.
Natural Join Example
sid bid day sid sname rating age
22 101 10/10/96 22 dustin 7 45.0
58 103 11/12/96 31 lubber 8 55.5
R1 58 rusty 10 35.0
S1
R1 S1 =

sid sname rating age bid day


22 dustin 7 45.0 101 10/10/96
58 rusty 10 35.0 103 11/12/96
Theta-Join
R3 := R1 JOINC R2
Take the product R1 * R2.
Then apply SELECTC to the result.
As for SELECT, C can be any Boolean-
valued condition.
Historic versions of this operator allowed only
A theta B, where theta was =, <, etc.; hence the
name theta-join.
Theta Join Example
sid bid day sid sname rating age
22 101 10/10/96 22 dustin 7 45.0
58 103 11/12/96 31 lubber 8 55.5
58 rusty 10 35.0
R1
S1
S1 R1 =
S1.sid R1.sid

(sid) sname rating age (sid) bid day


22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.5 58 103 11/12/96
Outer Join
In NATURAL JOIN tuples without a matching (or
related) tuple are eliminated from the join result.
Tuples with null in the join attributes are also
eliminated. This loses information.
An extension of the join operation that avoids loss of
information.
Computes the join and then adds tuples form one
relation that does not match tuples in the other relation
to the result of the join.
Uses null values.
Outer Join
Left outer join operation keeps every tuple in the
first or left relation R in R S; if no matching
tuple is found in S, then the attributes of S in the
join result are padded with null values.
A similar operation, right outer join, keeps every
tuple in the second or right relation S in the
result of R S.
A third operation, full outer join, denoted by
keeps all tuples in both the left and the
right relations when no matching tuples are
found, padding them with null values as needed.
Left-Outer Join Example

R1 S1

R1 left outer join S1=


Right-Outer Join Example

R1 S1

R1 right outer join S1=


Full-Outer Join Example

R1 = S1

R1 full outer join S1=


Union
Union, intersection, and difference need
new definitions for bags.
An element appears in the union of two
bags the sum of the number of times it
appears in each bag.
Example: {1,2,1} UNION {1,1,2,3,1} =
{1,1,1,1,1,2,2,3}
Relational Algebra on Bags
A bag (or multiset ) is like a set, but an
element may appear more than once.
Example: {1,2,1,3} is a bag.
Example: {1,2,3} is also a bag that happens
to be a set.
Why Bags?
SQL, the most important query language for
relational databases, is actually a bag
language.
Some operations, like projection, are much
more efficient on bags than sets.
Operations on Bags
Selection applies to each tuple, so its effect
on bags is like its effect on sets.
Projection also applies to each tuple, but as
a bag operator, we do not eliminate
duplicates.
Products and joins are done on each pair of
tuples, so duplicates in bags have no effect
on how we operate.
Example: Bag Selection
R( A, B )
1 2
5 6
1 2

SELECTA+B<5 (R) = A B
1 2
1 2
Example: Bag Projection
R( A, B )
1 2
5 6
1 2

PROJECTA (R) = A
1
5
1
Example: Bag Product
R( A, B ) S( B, C )
1 2 3 4
5 6 7 8
1 2

R*S= A R.B S.B C


1 2 3 4
1 2 7 8
5 6 3 4
5 6 7 8
1 2 3 4
1 2 7 8
Example: Bag Theta-Join
R( A, B ) S( B, C )
1 2 3 4
5 6 7 8
1 2

R JOIN R.B<S.B S= A R.B S.B C


1 2 3 4
1 2 7 8
5 6 7 8
1 2 3 4
1 2 7 8
29
Bag Intersection
An element appears in the intersection of
two bags the minimum of the number of
times it appears in either.
Example: {1,2,1} INTER {1,2,3} = {1,2}.
Bag Difference
An element appears in the difference A B
of bags as many times as it appears in A,
minus the number of times it appears in B.
But never less than 0 times.
Example: {1,2,1} {1,2,3} = {1}.

You might also like