You are on page 1of 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 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 Operators

Relational algebra defines the theoretical way of manipulating table contents using the relational functions Operators

SELECT PROJECT INTERSECT UNION DIFFERENCE PRODUCT DIVIDE

Relational Algebra
Fundamental or Basic operators

select project Cartesian product union set difference set intersection division

Relational Algebra Operators

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

Relational Algebra Operators


A PRODUCT produces a list of all possible pairs of rows from two tables. Default is CARTESIAN PRODUCT R X S DIVIDE requires the use of one singlecolumn table and one two-column table. RS

Relational Algebra Operators


UNION combines all rows from two tables. The two tables must be union compatible. R S INTERSECT produces a listing that contains only the rows that appear in both tables. The two 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.

Relational Algebra: 5 Basic Operations

Since each operation returns a relation, operations can be composed! (Algebra is closed.)

Unary Operations

Selection Projection

Selection

Gives a horizontal subset of a relation


R A a1 a2 B b1 b2 C c1 c2 D d1 d2

(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

179 857 342

Silva Perera Dias

7 4 7

179 342

Silva Dias

7 7

Sales-Emp = D-No=7 (Employee)

Projection

Gives a vertical subset of a relation


R A a1 a2 B b1 b2 A a1 a2

A (R) =

Duplicate tuples are eliminated

Projection
R A a1 a2 B b1 b2 C c1 c2 D d1 d2 B b1 b2 C c1 c2

Can project more than one column

B,C (R) =
Duplicate tuples are eliminated

Projection: vertical subset of a table


Employee
E-No E-Name D-No

Employee Names
E-No E-Name

179 857 342

Silva Perera Dias

7 4 7

179 857 342

Silva Perera Dias

Emp-Names = E-No, E-Name (Employee)

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

Cartesian Product: Creates a single table from two tables.


Employee
E-No E-Name D-No

Department
D-No D-Name M-No

179 857 342

Silva Perera Dias Emp-Info


E-No

7 4 7
D-No D-No

4 7

Finance Sales

857 179

E-Name

D-Name

M-No

179 857 342 179 857 342

Silva Perera Dias Silva Perera Dias

7 4 7 7 4 7

4 4 4 7 7 7

Finance Finance Finance Sales Sales Sales

857 857 857 179 179 179

Emp-Info = Employee E.D-No=D.D-No Department

Cartesian Product - Example


Students stud# name 100 Fred 200 Dave 300 Bob course PH CM CM Courses course# name PH Pharmacy CM Computing

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

Courses.name Pharmacy Computing Pharmacy Computing Pharmacy Computing

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 sname rating age 31 lubber 8 55.5 58 rusty 10 35.0

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

sid sname rating age 22 dustin 7 45.0

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

sid sname rating age 28 yuppy 9 35.0 44 guppy 5 35.0

S2 S1

You might also like