Professional Documents
Culture Documents
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 relational model is the relational algebra. This Algebra is composed of Unary operations (involving a single table) and Binary operations (involving multiple tables).
Relational algebra defines the theoretical way of manipulating table contents using the relational functions Operators
Relational Algebra
Fundamental or Basic operators
select project Cartesian product union set difference set intersection division
SELECT yields values for all attributes found in a table. It yields a horizontal subset of a table. predicate (R) PROJECT produces a list of all values for selected attributes. It yields a vertical subset of a table. a1an (R) DIFFERENCE yields all rows in one table that are not found in the other table; i.e., it subtracts one table from the other. The tables must be union compatible. R - S
Selection ( ) Selects a subset of rows from relation (horizontal). Projection ( ) Retains only wanted columns from relation (vertical). Cross-product (x) Allows us to combine two relations. 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.)
Unary Operations
Selection Projection
Selection
(B=b2) (R) =
A a2
B b2
C c2
D d2
Relational Operators
Selection: horizontal subset of a table
Employee
E-No E-Name D-No
Sales Employee
E-No E-Name D-No
7 4 7
179 342
Silva Dias
7 7
Projection
A (R) =
Projection
R A a1 a2 B b1 b2 C c1 c2 D d1 d2 B b1 b2 C c1 c2
B,C (R) =
Duplicate tuples are eliminated
Employee Names
E-No E-Name
7 4 7
Binary Operations
Cartesian Product
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.
Cartesian Product R
A a1 a2 B b1 b2 A a1 B b1 b1 b1 b2 b2 b2 C c1 c2 c3 c1 c2 c3 D d1 d2 d3 d1 d2 d3
S C c1 c2 c3 D d1 d2 d3
RXS=
a1 a1 a2 a2 a2
Department
D-No D-Name M-No
7 4 7
D-No D-No
4 7
Finance Sales
857 179
E-Name
D-Name
M-No
7 4 7 7 4 7
4 4 4 7 7 7
Students X Courses = stud# Students.name course 100 Fred PH 100 Fred PH 200 Dave CM 200 Dave CM 300 Bob CM 300 Bob CM
course# PH CM PH CM PH CM
Set Operations
Union Intersection Difference
Union
Takes the set of rows in each table and combines them, eliminating duplicates Participating relations must be compatible, ie have the same number of columns, and the same column names, domains, and data types R S RS
A a1 a2 B b1 b2 A a2 a3 B b2 b3 A a1 a2 a3 B b1 b2 b3
Union
sid 22 31 58 sid 28 31 44 58 sname rating age dustin 7 45.0 lubber 8 55.5 rusty 10 35.0
S1
sid sname rating age 22 31 58 44 28 dustin lubber rusty guppy yuppy 7 8 10 5 9 45.0 55.5 35.0 35.0 35.0
sname rating age yuppy 9 35.0 lubber 8 55.5 guppy 5 35.0 rusty 10 35.0
S2
S1S2
Intersection
Takes the set of rows that are common to each relation Participating relations must be compatible
R
A a1 a2 B b1 b2 A a2 a3
S
B b2 b3
RS
A a2 B b2
Intersection
R A A1 a2 a3 S A a1 a2 B b1 b2 B b1 b2 b3 A B b1 b2
RnS =
a1 a2
R a a b e e S C c e a
B b b c d d b c e e c e d
D d f f d f e
Division
D d f
. R . S=
A a e
B b d
Division
A a a b e e
R B b b c d d b c e e c e d A a a3 C D d d f d f e B b b b3 D d d3 S C c e
. .S = R
Intersection
sid 22 31 58 sname rating age dustin 7 45.0 lubber 8 55.5 rusty 10 35.0
S1
sid 28 31 44 58
sname rating age yuppy 9 35.0 lubber 8 55.5 guppy 5 35.0 rusty 10 35.0
S2
S1S2
Difference
Takes the set of rows in the first relation but not the second Participating relations must be compatible
R
A a1 a2 B b1 b2 A a2 a3
S
B b2 b3
R-S
A a1 B b1
Set Difference
R A A1 a2 a3 S A a1 a2 B b1 b2 B b1 b2 b3
R-S
A a3
B b3
Set Difference
sid 22 31 58 sname rating age dustin 7 45.0 lubber 8 55.5 rusty 10 35.0
S1
S1 S2
sid 28 31 44 58
sname rating age yuppy 9 35.0 lubber 8 55.5 guppy 5 35.0 rusty 10 35.0
S2
S2 S1