Sei sulla pagina 1di 21

VICTORIA UNIVERSITY OF WELLINGTON

Te Whare Wananga o te Upoko o te Ika a Maui

Relational Algebra
Lecturer Dr Pavle Mogin

COMP302
Database Systems

Plan for Relational Algebra

Relational database operations


Set theoretic operations
Additional operations
Reading:
Chapters 6 of the textbook

COMP302 Database Systems 2007

Relational Algebra 1

Relational Algebra
One of the relational data model languages
The others are Tuple Calculus, Domain Calculus,
SQL
Important for query optimization
A SQL query is transformed into a relational algebra
equivalent, optimized and executed

A set of operations to manipulate (query and update)


a relational database
Operations are applied onto relations
The result is a new relation
COMP302 Database Systems 2007

Relational Algebra 2

Relational Algebra Operations


Three types of operations:
Relational database operations:
select, project, and join

Set theoretic operations:


union, intersect, set difference, Cartesian product

Additional relational operations:


aggregate operations, grouping, and outer join

COMP302 Database Systems 2007

Relational Algebra 3

Select operation
It is used to select such a subset of tuples from a
relation that satisfy given condition
Notation: C (r (N ))
Condition C is a Boolean expression on attributes of
R in N(R, C)
Boolean expression is made up of clauses of the form
A a or A B, where adom (A ), { =, <, >, <, >, },
and A, B R, ( is equivalent to <>)
Clauses can be connected by Boolean operators , ,
to form new clauses
COMP302 Database Systems 2007

Relational Algebra 4

A Sample Relational Database


Student
LName FName StudId

Major

Smith

Susan

131313 Comp

Bond

James

007007 Math

Smith

Susan

555555 Comp

Cecil

John

010101 Math

Course
PName CourId

Points

Dept

DB Sys C302

15

Comp

SofEng C301

15

Comp

DisMat

M214

22

Math

Pr&Sys C201

22

Comp

COMP302 Database Systems 2007

Grades
StudId

CourId

Grade

007007

C302

A+

555555

C302

007007

C301

007007

M214

A+

131313

C201

B-

555555

C201

131313

C302

007007

C201

010101

C201

Relational Algebra 5

Select Operation (continued)


Relation C r(N) contains those tuples of r(N) that
evaluate true for C, hence:
degree(C r (N )) = degree(r (N )) (number of attributes)
C r (N ) r (N ) and | C r (N ) | < |r (N ) | (number of tuples)
e.g.
Student1
StudId = 007007 r (Student ) = LName FName StudId Major
Bond

James

007007 Math

Student2
LName FName StudId

FName = 'Susan' r (Student ) =


COMP302 Database Systems 2007

Major

Smith

Susan

131313 Comp

Smith

Susan

555555 Comp
Relational Algebra 6

Project Operation
Project operation produces a new relation by
choosing some of the columns from the starting
relation, and dropping all the others
Notation:
ALr (N ),
where AL is a attribute sublist from R in N (R, C)
If AL = (Al,, Ak), then ALr (R ) = r (N )[Al,, Ak]
Student1
e.g.

<LName, FName>r (Student ) =


COMP302 Database Systems 2007

Lname

FName

Smith

Susan

Bond

James

Cecil

John
Relational Algebra 7

Project Operation (continued)


Properties of project operation:
Degree(ALr (R )) = |AL | < |R | (number of attributes)
|ALr (R )| < |r (R )| (number of tuples)

Combining select and project operations

AL (C r (R ))
e.g.

(FName, LName ) (StudId = 007007 r (Student )) =

COMP302 Database Systems 2007

Student3
FName

LName

James

Bond

Relational Algebra 8

Join Operation
Join operation joins two relations by merging those
tuples from two relations that satisfy a given condition
The condition is defined on attributes belonging to relations to
be joined

Theta, equi, and natural join operations


Theta, equi, and natural join are collectively called
INNER joins
In each of inner joins, tuples with null valued join
attributes do not appear in the result

COMP302 Database Systems 2007

Relational Algebra 9

Theta Join Operation


Notation:
r (N) = r (N1) JC r (N2)
r(N ) is the result of joining r(N1) over N1(R1, C1) with r(N2)
over N2(R2, C2)
Join condition JC = jc1 jcn
jci = A B, AR1, BR2,
{=, , <, >, , },
Dom(N1, A ) Dom (N2, B ),
Range(N1, A ) Dom (N2, B )

R1 = {A1,, An}, R2 = {B1,, Bn}, R = {A1,, An, B1,, Bn}


degree(r (N )) = degree(r (N1)) + degree(r (N2))
| r (R) | < | r (R1) | | r (R2) |
COMP302 Database Systems 2007

Relational Algebra 10

Equijoin Operation

A special case of the theta join, when {=}


Notation: r (N ) = r (N1) X = Y r (N2), X R1, Y R2

e.g.
Let X = Student.StudId and Y = Grades.StudId and
r (Stud_Grades) = r (Student ) X = Y r (Grades ),
then

COMP302 Database Systems 2007

Relational Algebra 11

Equijoin Operation (continued)


Superfluous
column

Stud_Grades
StudId

StudId

Susan

131313

131313

Comp

C201

B-

Smith

Susan

131313

131313

Comp

C302

Bond

James

007007

007007

Math

C302

A+

Bond

James

007007

007007

Math

C301

Bond

James

007007

007007

Math

M214

A+

Bond

James

007007

007007

Math

C201

Smith

Susan

555555

555555

Comp

C201

Smith

Susan

555555

555555

Comp

C302

Cecil

John

010101

010101

Math

C201

LName

FName

Smith

COMP302 Database Systems 2007

Major

CourId

Grade

Relational Algebra 12

Natural Join Operation


A special case of an equijoin operation, when
attributes belonging to different relation schemas in
the equijoin condition have the same name (N1.X =
N2.X )
Notation: r (N) = r (N1) * r (N2)
Formal definition:
r (N1) * r (N2) = { t [R1 R2] | t [R1]r (N1) t [R2]r (N2)}
degree(r (N) = degree(r (N1) + degree(r (N2) |X |) (no of attr)
0 < |r (N1) * r (N2)| < |r (N1)| |r (N1)| (no of tuples)

COMP302 Database Systems 2007

Relational Algebra 13

Set Theoretic Operations


Union, Intersect, Difference, Cartesian product
r(N) = r(N1) r (N1),
where R1 = (A1,, An), R2 = (B1,, Bn) are lists of attributes, and
{ , , \, } ( \ or -)
For union, intersect and difference attribute sets R1 and R2 have
to be union compatible:
| R1| = | R2|,
(i{1,, n })(Dom (N1, Ai ) = Dom (N2, Bi)), and
(i{1,, n })(Range (N1, Ai ) = Range (N2, Bi ))

Cartesian product
R = R1 R2, degree(r(N1) r(N2)) = degree(r(N1)) +
degree(r(N2 )),
|r (N1) r (N2)| = |r (N1) | |r (N2)|
COMP302 Database Systems 2007

Relational Algebra 14

Question For You

Consider the following relations


r (AB )

r (BC )

How many tuples will contain the Cartesian product


r(AB ) r(BC )?
a) 3
b) 6
c) 9

COMP302 Database Systems 2007

Relational Algebra 15

Additional relational operations


Relational database and set theoretic operations are not
sufficient to perform some common database queries
To enhance the power of relational algebra, there are some new
operations introduced:
Aggregate functions (SUM, AVERAGE, MAX, MIN, COUNT),
Grouping, and
Outer join

Aggregate functions are defined on numeric attributes and


applied on all relation tuples
Relation is partitioned by means of grouping attribute values,
and the defined aggregate functions are computed for the tuples
of each group
Outer join extends the join operation to cope with null values
COMP302 Database Systems 2007

Relational Algebra 16

Aggregate Functions and Grouping


Notation:

<grouping attributes>Y<(function, attribute) list>(r (N ))

where:
<grouping attributes> is a list of attributes from R,
Y (pronounced as "script F") is the symbol used to denote
aggregate operation, and
<(function, attribute)_list> is a list of pairs (aggregate function
from {SUM, AVERAGE, COUNT, MIN, MAX }, attribute from
R)

The resulting relation has columns for grouping


attributes, and one column with the name of the form
FUNCTION_ATTRIBUTE for each (function, attribute)
pair
COMP302 Database Systems 2007

Relational Algebra 17

Outer Join
Introduced to insert those tuples that don't match, or
contain null values for join attributes into join relation
Notation:
LEFT, RIGHT, and FULL outer join

Example:
r (AB )
A

B = B

COMP302 Database Systems 2007

r (BC )

r (ABBC )

Relational Algebra 18

Summary
Relational algebra:
Relational database operations,
Set theoretic operations
Additional operations

Relational database operations


select (chooses tuples), project (restricts tuples), join
(concatenates matching tuples)

Set theoretic operations


Union, Intersect, Set difference, Cartesian product

Additional relational operations


Aggregate functions COUNT, SUM, AVERAGE,, Grouping,
and Outer join
COMP302 Database Systems 2007

Relational Algebra 19

Next topic

Structured Query Language


Reading:
Chapter 8 of the textbook

COMP302 Database Systems 2007

Relational Algebra 20

Potrebbero piacerti anche