0% found this document useful (0 votes)
30 views

Chapter2-Part 3 (New)

The document discusses relational algebra operations used to retrieve information from a relational database including select, project, join, cross product and outer join. It describes these operations and provides examples of applying them to relational tables.

Uploaded by

flamezodiark
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Chapter2-Part 3 (New)

The document discusses relational algebra operations used to retrieve information from a relational database including select, project, join, cross product and outer join. It describes these operations and provides examples of applying them to relational tables.

Uploaded by

flamezodiark
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Relational Data Model

Copyright © 2007 Ramez Elmasri and Shamkant B.


Navathe
Learning Outcome
 Identify the fundamental operators used to
retrieve information from a relational database:
 Restrict (select)
 Project
 Join (outer, inner)
 Cross Product
 Describe the purpose and input of each of the
operators.
 Use the operators to write the expressions
based on given relational tables.
 Show the output of the fundamental operators
based on a given database.

Slide 3- 2
Relational Algebra Operations from Set
Theory: CARTESIAN PRODUCT
 CARTESIAN (or CROSS) PRODUCT Operation
 This operation is used to combine tuples from two
relations in a combinatorial fashion.
 Denoted by R(A1, A2, . . ., An) x S(B1, B2, . . ., Bm)
 Result is a relation Q with degree n + m attributes:
○ Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order.
 The resulting relation state has one tuple for each
combination of tuples—one from R and one from S.
 Hence, if R has nR tuples (denoted as |R| = nR ), and
S has nS tuples, then R x S will have nR * nS tuples.
 The two operands do NOT have to be "type
compatible”

Slide 3- 3
Relational Algebra Operations from Set
Theory: CARTESIAN PRODUCT (cont.)
 Generally, CROSS PRODUCT is not a
meaningful operation
Can become meaningful when followed by
other operations
 Example (not meaningful):
 FEMALE_EMPS   SEX=’F’(EMPLOYEE)
 EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS)
 EMP_DEPENDENTS  EMPNAMES x
DEPENDENT
 EMP_DEPENDENTS will contain every
combination of EMPNAMES and DEPENDENT
 whether or not they are actually related

Slide 3- 4
Relational Algebra Operations from Set
Theory: CARTESIAN PRODUCT (cont.)
 To keep only combinations where the
DEPENDENT is related to the
EMPLOYEE, we add a SELECT operation
as follows
 Example (meaningful):
 FEMALE_EMPS   SEX=’F’(EMPLOYEE)
 EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS)
 EMP_DEPENDENTS  EMPNAMES x DEPENDENT
 ACTUAL_DEPS   SSN=ESSN(EMP_DEPENDENTS)
 RESULT   FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPS)
 RESULT will now contain the name of female
employees and their dependents

Slide 3- 5
Example of applying CARTESIAN
PRODUCT

Slide 3- 6
Binary Relational Operations:
JOIN
 JOIN Operation (denoted by )
 The sequence of CARTESIAN PRODUCT followed
by SELECT is used quite commonly to identify and
select related tuples from two relations
 A special operation, called JOIN combines this
sequence into a single operation
 This operation is very important for any relational
database with more than a single relation, because it
allows us combine related tuples from various
relations
 The general form of a join operation on two relations
R(A1, A2, . . ., An) and S(B1, B2, . . ., Bm) is:
R <join condition>S
 where R and S can be any relations that result from
general relational algebra expressions.

Slide 3- 7
Binary Relational Operations: JOIN (cont.)

 Example: Suppose that we want to retrieve the name of


the manager of each department.
 To get the manager’s name, we need to combine each
DEPARTMENT tuple with the EMPLOYEE tuple whose SSN
value matches the MGRSSN value in the department tuple.
 We do this by using the join operation.

 DEPT_MGR  DEPARTMENT Mgr_ssn=Ssn EMPLOYEE


 MGRSSN=SSN is the join condition
 Combines each department record with the employee who
manages the department
 The join condition can also be specified as
DEPARTMENT.Mgr_ssn= EMPLOYEE.Ssn

Slide 3- 8
Example of applying the JOIN operation

DEPT_MGR  DEPARTMENT MGRSSN=SSN EMPLOYEE

Slide 3- 9
Some properties of JOIN
 Consider the following JOIN operation:
 R(A1, A2, . . ., An) S(B1, B2, . . ., Bm)
R.Ai=S.Bj
 Result is a relation Q with degree n + m attributes:
○ Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order.
 The resulting relation state has one tuple for each
combination of tuples—r from R and s from S, but
only if they satisfy the join condition r[Ai]=s[Bj]
 Hence, if R has nR tuples, and S has nS tuples, then
the join result will generally have less than nR * nS
tuples.
 Only related tuples (based on the join condition) will
appear in the result

Slide 3- 10
Some properties of JOIN
 The general case of JOIN operation is
called a Theta-join: R S
theta
 The join condition is called theta
 Theta can be any general boolean
expression on the attributes of R and S; for
example:
R.Ai<S.Bj AND (R.Ak=S.Bl OR R.Ap<S.Bq)
 Most join conditions involve one or more
equality conditions “AND”ed together; for
example:
R.Ai=S.Bj AND R.Ak=S.Bl AND R.Ap=S.Bq

Slide 3- 11
Binary Relational Operations: EQUIJOIN

 EQUIJOIN Operation also known as


INNER JOIN Operation
 The most common use of join involves join
conditions with equality comparisons only
 Such a join, where the only comparison
operator used is =, is called an EQUIJOIN.
In the result of an EQUIJOIN we always have
one or more pairs of attributes (whose names
need not be identical) that have identical values
in every tuple.
The JOIN seen in the previous example was an
EQUIJOIN.

Slide 3- 12
Binary Relational Operations:
NATURAL JOIN Operation
 NATURAL JOIN Operation
 Another variation of JOIN called NATURAL JOIN —
denoted by * — was created to get rid of the second
(superfluous) attribute in an EQUIJOIN condition.
○ because one of each pair of attributes with identical
values is superfluous
 The standard definition of natural join requires that
the two join attributes, or each pair of corresponding
join attributes, have the same name in both relations
 If this is not the case, a renaming operation
is applied first.

Slide 3- 13
Binary Relational Operations
NATURAL JOIN (contd.)
 Example: To apply a natural join on the DNUMBER attributes of
DEPARTMENT and DEPT_LOCATIONS, it is sufficient to write:
 DEPT_LOCS  DEPARTMENT * DEPT_LOCATIONS
 Only attribute with the same name is DNUMBER
 An implicit join condition is created based on this attribute:
DEPARTMENT.DNUMBER=DEPT_LOCATIONS.DNUMBER

 Another example: Q  R(A,B,C,D) * S(C,D,E)


 The implicit join condition includes each pair of attributes with the
same name, “AND”ed together:
○ R.C=S.C AND R.D=S.D
 Result keeps only one attribute of each such pair:
○ Q(A,B,C,D,E)

Slide 3- 14
Example of NATURAL JOIN operation

Slide 3- 15
Complete Set of Relational Operations

 The set of operations including SELECT ,


PROJECT  , UNION , DIFFERENCE  ,
RENAME , and CARTESIAN PRODUCT
X is called a complete set because any
other relational algebra expression can be
expressed by a combination of these five
operations.
 For example:
R  S = (R  S ) – ((R  S)  (S  R))
R <join condition> S =  <join condition> (R X S)

Slide 3- 16
Outer join
 To display rows in the result that do not
have matching values in the join column,
use Outer join.

 R S
(Left) outer join is join in which tuples from
R that do not have matching values in
common columns of S are also included in
result relation.

© Pearson Education Limited 1995, 2005


17
Example - Left Outer join
 Produce a status report on property
viewings.

propertyNo, street, city(PropertyForRent)


Viewing

© Pearson Education Limited 1995, 2005


18
Outer join
 To display rows in the result that do not
have matching values in the join column,
use Outer join.

 R S
(Right) outer join is join in which tuples
from S that do not have matching values in
common columns of R are also included in
result relation.

© Pearson Education Limited 1995, 2005


19
Recap of Relational Algebra Operations

Slide 3- 20
Slide 3- 21

You might also like