Upload
varun-garg
View
226
Download
0
Embed Size (px)
Citation preview
7/28/2019 Relation Algebra
1/31
Relational Algebra
The Relational Algebra is used to define the
ways in which relations (tables) can be operated
to manipulate their data.
The basic set of operations for the relationalmodel is the relational algebra.
This Algebra is composed of Unary operations
(involving a single table) and Binary operations
(involving multiple tables).
7/28/2019 Relation Algebra
2/31
Relational Algebra Operators Relational algebra defines the theoretical way of
manipulating table contents using the relational functions
Operators SELECT PROJECT INTERSECT UNION
DIFFERENCE PRODUCT DIVIDE
7/28/2019 Relation Algebra
3/31
Relational AlgebraFundamental or Basic operators
select project
Cartesian product
union
set difference - set intersection
division
7/28/2019 Relation Algebra
4/31
Relational Algebra Operators
SELECTyields values forall attributes found ina table. It yields a horizontal subset of a table.
predicate(R)
PROJECTproduces a list of all values forselected attributes. It yields a vertical subset ofa table. a1an(R)
DIFFERENCEyields all rows in one table thatare not found in the other table; i.e., it subtractsone table from the other. The tables must beunion compatible. R - S
7/28/2019 Relation Algebra
5/31
Relational Algebra Operators
A PRODUCTproduces a list of all
possible pairs of rows from two tables.
Default is CARTESIAN PRODUCT R X S
DIVIDE requires the use of one single-
column table and one two-column table.
RS
7/28/2019 Relation Algebra
6/31
Relational Algebra Operators
UNIONcombines all rows from two
tables. The two tables must be union
compatible. R S INTERSECTproduces a listing that
contains only the rows that appear in
both tables. The two tables must beunion compatible. R S
7/28/2019 Relation Algebra
7/31
Relational Algebra: 5 Basic
Operations
Selection ( ) Selects a subset ofrows
fromrelation (horizontal).
Projection ( ) Retains only wanted columns
from relation (vertical).
Cross-product (x) Allows us to combine tworelations.
Set-difference () Tuples in r1, but not in r2.
Union ( ) Tuples in r1 and/or in r2.
Since each operation returns a relation, operations
can be composed! (Algebra is closed.)
7/28/2019 Relation Algebra
8/31
Unary Operations
Selection
Projection
7/28/2019 Relation Algebra
9/31
Selection
Gives a horizontal subset of a relation
A B
a1
a2
b1
b2
R
C D
c1
c2
d1
d2
(B=b2) (R) =
A B
b2
C D
c2 d2a2
7/28/2019 Relation Algebra
10/31
Relational Operators
Selection: horizontal subset of a table
Employee
E-No E-Name D-No
179 Silva 7
857 Perera 4
342 Dias 7
Sales Employee
E-No E-Name D-No
179 Silva 7
342 Dias 7
Sales-Emp = D-No=7 (Employee)
7/28/2019 Relation Algebra
11/31
Projection
Gives a vertical subset of a relation
A B
a1
a2
b1
b2
R
A (R) =A
a1
a2
Duplicate tuples are eliminated
7/28/2019 Relation Algebra
12/31
Projection
Can project more than one column
Duplicate tuples are eliminated
A B
a1
a2
b1
b2
R
C D
c1
c2
d1
d2
B,C (R) =B C
b1
b2
c1
c2
7/28/2019 Relation Algebra
13/31
Projection: vertical subset of a table
Employee
E-No E-Name D-No
179 Silva 7
857 Perera 4342 Dias 7
Employee Names
E-No E-Name
179 Silva
857 Perera342 Dias
Emp-Names = E-No, E-Name (Employee)
7/28/2019 Relation Algebra
14/31
Binary Operations
Cartesian Product
7/28/2019 Relation Algebra
15/31
Cartesian Product
Set of all tuples (r X s) where r tuples
belong to the first relation R and s tuples
belong to the second relation S. Concatenation of every tuple in R with
every tuple in S.
7/28/2019 Relation Algebra
16/31
Cartesian ProductA B
a1
a2
b1
b2
R
C D
c1
c2
c3
d1
d2
d3
SRX S =
A B
a1
a1
a1
a2
a2
a2
b1
b1
b1
b2
b2
b2
C D
c1
c2
c3
c1
c2
c3
d1
d2
d3
d1
d2
d3
7/28/2019 Relation Algebra
17/31
Cartesian Product: Creates a single table from two tables.
D-No D-Name M-No
4 Finance 857
7 Sales 179
DepartmentEmployee
E-No E-Name D-No
179 Silva 7857 Perera 4
342 Dias 7
Emp-Info
E-No E-Name D-No D-No D-Name M-No
179 Silva 7 4 Finance 857
857 Perera 4 4 Finance 857
342 Dias 7 4 Finance 857
179 Silva 7 7 Sales 179857 Perera 4 7 Sales 179
342 Dias 7 7 Sales 179
Emp-Info = Employee E.D-No=D.D-No Department
7/28/2019 Relation Algebra
18/31
Cartesian Product - Example
Students Coursesstud# name course course# name
100 Fred PH PH Pharmacy
200 Dave CM CM Computing
300 Bob CM
Students X Courses =
stud# Students.name course course# Courses.name
100 Fred PH PH Pharmacy
100 Fred PH CM Computing
200 Dave CM PH Pharmacy
200 Dave CM CM Computing
300 Bob CM PH Pharmacy
300 Bob CM CM Computing
7/28/2019 Relation Algebra
19/31
Set OperationsUnion
Intersection
Difference
7/28/2019 Relation Algebra
20/31
Union
Takes the set of rows in each table andcombines them, eliminating duplicates
Participating relations must be compatible, iehave the same number of columns, and the
same column names, domains, and data typesR S R S
A B
a1 b1
a2 b2
A B
a2 b2
a3 b3
A B
a1 b1
a2 b2a3 b3
7/28/2019 Relation Algebra
21/31
7/28/2019 Relation Algebra
22/31
Union
S S1 2
S1
S2
sid sname rating age22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
sid sname rating age
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
sid sname rating age22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
44 guppy 5 35.028 yuppy 9 35.0
7/28/2019 Relation Algebra
23/31
Intersection
Takes the set of rows that are common toeach relation
Participating relations must be compatible
R S R SA B
a1 b1
a2 b2
A B
a2 b2
a3 b3
A B
a2 b2
7/28/2019 Relation Algebra
24/31
Intersection
R n S =
A B
a1
a2
b1
b2
S
A B
a1
a2
b1
b2
A B
A1
a2a3
b1
b2b3
R
7/28/2019 Relation Algebra
25/31
DivisionA Ba
a
b
e
e
a
b
b
c
d
d
b
C D
c
e
e
c
e
d
d
f
f
d
f
e
R
C D
c
e
d
f
SA B
a
e
b
dRS =..-
7/28/2019 Relation Algebra
26/31
DivisionA B
a
a
b
e
e
a
b
b
c
d
d
b
C D
c
e
e
c
e
d
d
d
f
d
f
e
R
C
c
e
S
RS = bb3
A B D
d
d3
a
a3
b..-
7/28/2019 Relation Algebra
27/31
Intersection
S1
S2
S S1 2
sid sname rating age
31 lubber 8 55.558 rusty 10 35.0
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
sid sname rating age
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
7/28/2019 Relation Algebra
28/31
Difference
Takes the set of rows in the first relationbut not the second
Participating relations must be compatible
R S R - SA B
a1 b1
a2 b2
A B
a2 b2
a3 b3
A B
a1 b1
7/28/2019 Relation Algebra
29/31
Set Difference
A B
a1
a2
b1
b2
S
A B
A1
a2
a3
b1
b2
b3
R
R- S =A B
a3 b3
7/28/2019 Relation Algebra
30/31
Set Difference
S1
S2
S S1 2
S2 S1
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
sid sname rating age
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
sid sname rating age
22 dustin 7 45.0
sid sname rating age
28 yuppy 9 35.044 guppy 5 35.0
7/28/2019 Relation Algebra
31/31