Escolar Documentos
Profissional Documentos
Cultura Documentos
8
S
( R) is a renamed relation S based on R (which does not speciIy column names).
8
(
1
,
2
, .,
n
)
( R) is a renamed relation with column names
1
,
1
, ...
n
which
does not speciIy a new relation name.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-14
&nary ReIationaI Operations (cont.)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-15
ReIationaI AIgebra Operations From
Set Theory
UO Operation
The result oI this operation, denoted by R S, is a relation that includes all
tuples that are either in R or in S or in both R and S. uplicate tuples are
eliminated.
Example: To retrieve the social security numbers oI all employees who either
work in department 5 or directly supervise an employee who works in
department 5, we can use the union operation as Iollows:
EP5_EMPS 9
O5
(EMPLOYEE)
RESULT1 6
SS
(EP5_EMPS)
RESULT2(SS) 6
SUPERSS
(EP5_EMPS)
RESULT RESULT1 RESULT2
The union operation produces the tuples that are in either RES<1 or
RES<2 or both. The two operands must be 'type compatible.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-16
ReIationaI AIgebra Operations From
Set Theory
Type Compatibility
The operand relations R
1
(A
1
, A
2
, ..., A
n
) and R
2
(
1
,
2
, ...,
n
)
must have the same number oI attributes, and the domains oI
corresponding attributes must be compatible; that is,
dom(A
i
)dom(
i
) Ior i1, 2, ..., n.
The resulting relation Ior R
1
R
2
,R
1
Y R
2
, or R
1
-R
2
has the
same attribute names as the 17st operand relation R1 (by
convention).
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-17
ReIationaI AIgebra Operations From
Set Theory
UO Example
$%&DEN%N$%R&C%OR
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-18
ReIationaI AIgebra Operations From Set
Theory (cont.) - use Fig. 6.4
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-19
ReIationaI AIgebra Operations From Set
Theory (cont.)
TERSECTO OPERATO
The result oI this operation, denoted by R Y S, is a relation that includes all
tuples that are in both R and S. The two operands must be "type compatible"
Example: The result oI the intersection operation (Iigure below) includes only
those who are both students and instructors.
$%&DEN% YN$%R&C%OR
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-20
ReIationaI AIgebra Operations From Set
Theory (cont.)
Set ifference (or MUS) Operation
The result oI this operation, denoted by R - S, is a relation that includes all
tuples that are in R but not in S. The two operands must be "type compatible.
Example: The Iigure shows the names oI students who are not instructors, and
the names oI instructors who are not students.
ST&ENT-INSTR&CTOR
INSTR&CTOR-ST&ENT
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-21
ReIationaI AIgebra Operations From Set
Theory (cont.)
Notice that both union and intersection are .4mmutat;e
4pe7at4ns, that is
R S S R, and R Y S S Y R
oth union and intersection can be treated as n-ary operations
applicable to any number oI relations as both are ass4.at;e
4pe7at4ns, that is
R (S T) (R S) T, and (R Y S) Y T R Y (S Y T)
The minus operation is n4t .4mmutat;e, that is, in general
R - S / S - R
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-22
ReIationaI AIgebra Operations From Set
Theory (cont.)
CARTESA (or cross product) Operation
This operation is used to combine tuples Irom two relations in a
combinatorial Iashion. In general, the result oI R(A
1
, A
2
, . . ., A
n
) x S(
1
,
2
, . . .,
m
) is a relation Q with degree n m attributes Q(A
1
, A
2
, . . ., A
n
,
1
,
2
, . . .,
m
), in that order. The resulting relation Q has one tuple Ior
each combination oI tuplesone Irom R and one Irom S.
ence, iI R has n
R
tuples (denoted as ,R, n
R
), and S has n
S
tuples, then
, R x S , will have n
R
* n
S
tuples.
The two operands do NOT have to be "type compatible
Example:
FEMALE_EMPS 9
SEX`F`
(EMPLOYEE)
EMPAMES 6
FNAME, LNAME, SSN
(FEMALE_EMPS)
EMP_EPEETS EMPAMES x EPEET
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-23
ReIationaI AIgebra Operations From Set
Theory (cont.)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-24
inary ReIationaI Operations
1O Operation
The sequence oI cartesian product Iollowed by select is used
quite commonly to identiIy and select related tuples Irom two
relations, a special operation, called 1O. It is denoted by a
This operation is very important Ior any relational database
with more than a single relation, because it allows us to process
relationships among relations.
The general Iorm oI a join operation on two relations R(A
1
, A
2
,
. . ., A
n
) and S(
1
,
2
, . . .,
m
) is:
R
join condition~
S
where R and S can be any relations that result Irom general
7eat4na aeb7a exp7ess4ns.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-25
inary ReIationaI Operations (cont.)
Example: Suppose that we want to retrieve the name oI
the manager oI each department. To get the manager`s
name, we need to combine each EPARTMENT tuple
with the EMPLOYEE tuple whose SSN value matches
the MGRSSN value in the department tuple. e do this
by using the join operation.
EPT_MGR EPARTMET
MGRSSSS
EMPLOYEE
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-26
inary ReIationaI Operations (cont.)
EQU1O Operation
The most common use oI join involves join conditions with equality comparisons only.
Such a join, where the only comparison operator used is , is called an EQ&IJOIN. In
the result oI an EQ&IJOIN we always have one or more pairs oI attributes (whose
names need not be identical) that have dent.a ;aues in every tuple.
The JOIN seen in the previous example was EQ&IJOIN.
ATURAL 1O Operation
ecause one oI each pair oI attributes with identical values is superIluous, a new
operation called natural joindenoted by *was created to get rid oI the second
(superIluous) attribute in an EQ&IJOIN condition.
The standard deIinition oI natural join requires that the two join attributes, or each pair
oI corresponding join attributes, have the same name in both relations. II this is not the
case, a renaming operation is applied Iirst.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-27
inary ReIationaI Operations (cont.)
Example: To apply a natural join on the N&MER attributes oI
EPARTMENT and EPTLOCATIONS, it is suIIicient to write:
EPT_LOCS EPARTMET ` EPT_LOCATOS
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-28
CompIete Set of ReIationaI Operations
The set oI operations including select 9,
project 6 , union , set difference - , and
cartesian product X is called a complete set
because any other relational algebra expression
can be expressed by a combination oI these Iive
operations.
For example:
R Y S (R S ) - ((R S) (S R))
R
join condition~
S 9
join condition~
(R X S)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-29
inary ReIationaI Operations (cont.)
'SO Operation
The division operation is applied to two relations
R(Z) S(X), where X subset Z. Let Y Z - X (and hence Z
X Y); that is, let Y be the set oI attributes oI R that are
not attributes oI S.
The result oI IVISION is a relation T(Y) that includes a
tuple t iI tuples t
R
appear in R with t
R
|Y| t, and with
t
R
|X| t
s
147 e;e7y tupe t
s
in S.
For a tuple t to appear in the result T oI the IVISION, the
values in t must appear in R in combination with e;e7y tuple
in S.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-30
inary ReIationaI Operations (cont.)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-31
Recap of ReIationaI AIgebra Operations
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-32
AdditionaI ReIationaI Operations
Aggregate Functions and Grouping
A type oI request that cannot be expressed in the basic relational algebra
is to speciIy mathematical aggregate functions on collections oI values
Irom the database.
Examples oI such Iunctions include retrieving the average or total salary
oI all employees or the total number oI employee tuples. These Iunctions
are used in simple statistical queries that summarize inIormation Irom
the database tuples.
Common Iunctions applied to collections oI numeric values include
S&M, AVERAGE, MAXIM&M, and MINIM&M. The CO&NT
Iunction is used Ior counting tuples or values.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-33
AdditionaI ReIationaI Operations (cont.)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-34
Use of the Functional operator
MAX $aa7y
(Employee) retrieves the maximum salary value
Irom the Employee relation
MIN $aa7y
(Employee) retrieves the minimum Salary value Irom
the Employee relation
S&M $aa7y
(Employee) retrieves the sum oI the Salary Irom the
Employee relation
NO