Вы находитесь на странице: 1из 6

Relational Operations (Relational Algebra)

1. Union
2. Intersection
3. Difference
4. Product
5. Select
6. Project
7. Join
The first four operations are taen fro! !athe!atical set theor" and are largel" the sa!e
as the operations found there# The ne$t four are ne% operations that appl" specificall" to
the relational data !odel# The &Assign!ent' operation is the standard co!puter language
operation of gi(ing a na!e to a (alue#

Interestingl" enough the &Data )anage!ent' te$t refers to restrict as a relational algebra
operation# The operation &Select' does %hat restrict does# So for the purposes of this
class the nine operations listed abo(e are %hat %e %ill recogni*e as relational algebra
operations#

Union + The union of t%o relations is for!ed b" adding the tuples fro! one relation to
those of a second relation, to create a third relation#
-$a!ple.
Table /
Snum
Name Major
/01 Jones 2istor"
/34 Pars )ath
05/ S!ith 2istor"

Table 0
Number
Name Interest
/63 Anderson )anage!ent
/01 Jones 2istor"

Result of Table / 7 Table 0
Snum or Number
Name Major or Interest
/01 Jones 2istor"
/34 Pars )ath
05/ S!ith 2istor"
/63 Anderson )anage!ent

8ote that tuple 9/01, Jones, 2istor": %hich occurs in both Table / and Table 0 is
not duplicated in the union# Relations !ust ha(e the sa!e nu!ber of attributes
and those attributes !ust ha(e lie do!ains#

The union operation in relation algebra is acco!plished using the union operator #
Union is a binar" operator it operates on t%o relationships#
An e$pression is of the for! r/ r0, %here r/ ; r0 are t%o relationships#
Difference + The difference of t%o relations is a third relation containing tuples that
occur in the first relation but not in the second#

-$a!ple
Table /
Snum
Name Major
/01 Jones 2istor"
/34 Pars )ath
05/ S!ith 2istor"

Table 0
Number
Name Interest
/63 Anderson )anage!ent
/01 Jones 2istor"

Result of Table / < Table 0
Snum
Name Major
/34 Pars )ath
05/ S!ith 2istor"
Differences !ust ha(e the sa!e nu!ber of attributes and those attributes !ust
ha(e lie do!ains#
The difference operation in relation algebra is acco!plished using the !inus operator <#
)inus is a binar" operator it operates on t%o relationships#
An e$pression is of the for! r/< r0, %here r/ ; r0 are t%o relationships#

Intersection The intersection of t%o relations is a third relation containing the tuples
that appear in both the first and second relations#

-$a!ple.
Table /
Snum
Name Major
/01 Jones 2istor"
/34 Pars )ath
05/ S!ith 2istor"

Table 0
Number
Name Interest
/63 Anderson )anage!ent
/01 Jones 2istor"

Result of Table / < Table 0
Snum
Name Major
/01 Jones 2istor"
Intersections !ust ha(e the sa!e nu!ber of attributes and those attributes !ust
ha(e lie do!ains#

The intersection operation in relation algebra is acco!plished using the intersection
operator # Intersection is a binar" operator it operates on t%o relationships#
An e$pression is of the for! r/ r0, %here r/ ; r0 are t%o relationships#
Product The product of t%o relations (so!eti!es called the Cartesian product) is the
concatenation of e(er" tuple of one relation %ith e(er" tuple of a second relation# The
product is denotes as A $ =# So if A has > ro%s and = has 1 ro%s the resulting table %ill
ha(e /0 ro%s# If table A has > attributes and table = has 1 attributes the resulting table
%ill ha(e 5 attributes#

-$a!ple
STUD-8T
SID Name Major Grade
/01 Jones 2istor" JR
/34 Pars )ath ?R
05/ S!ith 2istor" JR
/63 Anderson )anage!ent S8

-8RO@@)-8T
StudentNumber ClassName PositionNumber
/01 2136 /
/63 =A>A6 1
/01 =A>A6 5

Result of STUD-8T $ -8RO@@)-8T
SID Name Major Grade StudentNumber ClassName PositionNumber
/01 Jones 2istor" JR /01 2136 /
/01 Jones 2istor" JR /63 =A>A6 1
/01 Jones 2istor" JR /01 =A>A6 5
/34 Pars )ath ?R /01 2136 /
/34 Pars )ath ?R /63 =A>A6 1
/34 Pars )ath ?R /01 =A>A6 5
/63 Anderson )anage!ent S8 /01 2136 /
/63 Anderson )anage!ent S8 /63 =A>A6 1
/63 Anderson )anage!ent S8 /01 =A>A6 5
05/ S!ith 2istor" JR /01 2136 /
05/ S!ith 2istor" JR /63 =A>A6 1
05/ S!ith 2istor" JR /01 =A>A6 5
8ote. This relation does not !ae !uch sense# It is an e$a!ple of ho% the
operation %ors# Other operations !ust be utili*ed to !ae this data into so!e
useable infor!ation#
The cartesian product operation in relation algebra is acco!plished using the product
operator B# Product is a binar" operator it operates on t%o relationships#
An e$pression is of the for! r/B r0, %here r/ ; r0 are t%o relationships#
Projection < Projection is an operation that selects specified attributes fro! a relation#
The result of the projection is a ne% relation %ith the selected attributesC in other %ords a
projection chooses colu!ns fro! a table# The projection of STUD-8T on 8a!e and
)ajor is denoted as STUD-8T98a!e, )ajor:, %here STUD-8T is the na!e of the table
and 8a!e and )ajor are the attributes#
-$a!ple.
STUD-8T
SID Name Major Grade
/01 Jones 2istor" JR
/34 Pars )ath ?R
05/ S!ith 2istor" JR
/63 Anderson )anage!ent S8

Result of STUD-8T98a!e, )ajor:
Name Major
Jones 2istor"
Pars )ath
S!ith 2istor"
Anderson )anage!ent
Projection can be used to change the order of attributes# STUD-8T9?rade@e(el,
)ajor, 8a!e, SID: re(erses the order of STUD-8T attributes# This operation
%ould be useful to !ae t%o relations co!patible#
Projection operation is perfor!ed using the project operator # is a unar" operator it
operates on onl" one operand# A projection e$pression is of the for!
a/,a0,###,an (r)
Selection The selection operation taes a hori*ontal subset of the na!ed relation# This
is a fanc" %a" of sa"ing S-@-DT taes the ro%s of the table that satisf" a certain
condition#

-$a!ple
STUD-8T
SID Name Major Grade
/01 Jones 2istor" JR
/34 Pars )ath ?R
05/ S!ith 2istor" JR
/63 Anderson )anage!ent S8

Result of STUD-8T E2-R- )ajor F G)AT2'
SID Name Major Grade
/34 Pars )ath ?R

Selection operation is perfor!ed using the select operator # is a unar" operator it
operates on onl" one operand# A selection e$pression is of the for!
p(r), %here p is a predicate#
Join The join operation is a co!bination of the product, selection and (possibl")
projection operations#
Steps to do a join.
1. A $ =
2. Do a selection to eli!inate so!e tuples
3. Then optionall" re!o(e so!e attributes b" !eans of projection
-$a!ple. Suppose %e %ant to no% the na!e and position nu!ber of each
student

The join is denoted as STUD-8T JOI8 (SID F Student8u!ber) -8RO@@)-8T

Step / (STUD-8T $ -8RO@@)-8T)
SID Name Major Grade StudentNu
mber
ClassNam
e
Position
Number
/01 Jones 2istor" JR /01 2136 /
/01 Jones 2istor" JR /63 =A>A6 1
/01 Jones 2istor" JR /01 =A>A6 5
/34 Pars )ath ?R /01 2136 /
/34 Pars )ath ?R /63 =A>A6 1
/34 Pars )ath ?R /01 =A>A6 5
/63 Anderson )anage!ent S8 /01 2136 /
/63 Anderson )anage!ent S8 /63 =A>A6 1
/63 Anderson )anage!ent S8 /01 =A>A6 5
05/ S!ith 2istor" JR /01 2136 /
05/ S!ith 2istor" JR /63 =A>A6 1
05/ S!ith 2istor" JR /01 =A>A6 5
Step 0
Do a S-@-DT fro! the product %here SID of STUD-8T eHuals Student8u!ber
of -8RO@@)-8T# This t"pe of join is called an equijoin
SID Name Major Grade StudentNumber ClassName PositionN
umber
/01 Jones 2istor" JR /01 2136 /
/01 Jones 2istor" JR /01 =A>A6 5
/63 Anderson )anage!ent S8 /63 =A>A6 1

Step 1
-li!inate the duplicate field Student8u!ber %ith a projection# This t"pe of join
is called a natural join#
SID Name Major Grade ClassName PositionNumber
/01 Jones 2istor" JR 2136 /
/01 Jones 2istor" JR =A>A6 5
/63 Anderson )anage!ent S8 =A>A6 1

Diide The di(ide operation is the re(erse of the product operation# (A $ =)I = F A
Sounds eas" rightJ If "ou ha(e table A %hich is > colu!ns and fi(e ro%s, and
table = %hich is 1 colu!ns and 1 ro%s# The product of those t%o tables %ould be
a table %ith 5 colu!ns and /0 ro%s#

So if "ou %ere going to di(ide table D (A $ =) b" table = here are the steps#
1. Table D nu!ber of colu!ns + table = nu!ber of colu!ns F A nu!ber of
colu!ns
2. Table D nu!ber of ro%s I table = nu!ber of ro%s F A nu!ber of ro%s

This gi(es "ou the di!ensions of table A#

Table D
SID Name Major Grade StudentNu
mber
ClassName PositionNu
mber
/01 Jones 2istor" JR /01 2136 /
/01 Jones 2istor" JR /63 =A>A6 1
/01 Jones 2istor" JR /01 =A>A6 5
/34 Pars )ath ?R /01 2136 /
/34 Pars )ath ?R /63 =A>A6 1
/34 Pars )ath ?R /01 =A>A6 5
/63 Anderson )anage!ent S8 /01 2136 /
/63 Anderson )anage!ent S8 /63 =A>A6 1
/63 Anderson )anage!ent S8 /01 =A>A6 5
05/ S!ith 2istor" JR /01 2136 /
05/ S!ith 2istor" JR /63 =A>A6 1
05/ S!ith 2istor" JR /01 =A>A6 5

Step /.
5 colu!ns + 1 colu!ns# Re!e!ber the product rule %here table = colu!ns are
concatenated %ith table A colu!ns# Since %e are di(iding b" = %e %ill lea(e off
the last three colu!ns of table D to !ae table A#

SID Name Major Grade
/01 Jones 2istor" JR
/01 Jones 2istor" JR
/01 Jones 2istor" JR
/34 Pars )ath ?R
/34 Pars )ath ?R
/34 Pars )ath ?R
/63 Anderson )anage!ent S8
/63 Anderson )anage!ent S8
/63 Anderson )anage!ent S8
05/ S!ith 2istor" JR
05/ S!ith 2istor" JR
05/ S!ith 2istor" JR

Step 0.
As "ou can see %e are left %ith a table that has duplicate ro%s# Kou tr" to
eli!inate duplicate ro%s using appropriate relational operations##


Co!piled b" S#R#Ro" for reference onl"#

Вам также может понравиться