Title: Operations%20in%20the%20Relational%20Model
1Operations in the Relational Model
- These operation can be expressed in an algebra,
called relational algebra. - In this algebra relations are the operands and by
applying any of operators to be described later,
we can build progressively more complex
expressions.
2Operations
- Operations of relational algebra fall into four
broad classes - The usual set operations
- union
- intersection
- difference
- Operations that remove parts of a relation
- selection eliminates some rows(tuples)
- projection eliminates some columns
- Operations that combine the tuples of two
relations - Cartesian product, which pairs the tuples of two
relations in all possible ways - join operation, which selectively pair tuples
from two relations. - An operation called renaming.
3Conditions for Set Operations on Relations
- We can apply three most common operations on
sets- union, intersection, difference- to
relations provided that these two conditions
hold - R and S must have schemas with identical sets of
attributes. - Before applying the operations, the columns of R
and S must be ordered so that the order of
attributes is the same for both relations.
4Set Operations on Relations
R ? S, the union of R and S, is the set of
elements that are in R or S or both. R ? S, the
intersection of R and S, is the set of elements
that are in both R and S. R ? S, the difference
of R and S, is the set of elements that are in R
but not in S. Note that R ? S is different from
S ? R.
5Projection
Produces from a relation R a new relation that
has only some of Rs columns. ?A1, A2,,An(R) is
a relation that has only the columns for
attributes A1, A2,, An of R. Example Compute
the expression ?title, year, length(Movies) on
the table title year length filmType
studioName producerC Star wars
1977 124 color Fox 12345 Mighty Ducks
1991 104 color Disney 67890 Waynes World
1992 95 color Paramount 99999
6Example (Continued)
Resulting relation title year length Star
wars 1977 124 Mighty Ducks 1991 104 Waynes
World 1992 95 What about ?filmtype(Movies)
7Selection
The selection operator, applied to a relation R,
produces a new relation with a subset of Rs
tuples. The tuples in the result are those that
satisfy some condition C. We denote this
operation ?C( R ). The schema for the resulting
relation is the same as Rs schema. Example The
expression ?length?100(Movie) is title
year length filmType studioName producerC Star
wars 1977 124 color Fox 12345 Mighty
Ducks 1991 104 color Disney 67890
8Example
Suppose we want the set of tuples in the previous
relation Movie such that represent Fox movies at
least 100 minutes long. We can get these with a
more complicated condition ?length?100 And
studioNameFox(Movie) The tuple title
year length filmType studioName producerC Star
wars 1977 124 color Fox 12345 Is the
only one in the resulting relation.
9Cartesian Product
The Cartesian Product (or just product) of two
sets R and S is the set of pairs that can be
formed by choosing the first element of the pair
to be any element of R and the second an element
of S. This product is denoted as R?S. The
relation schema for the resulting schema is the
union of the schemas for R and S. However, if R
and S should happen to have some attributes in
common, then we need to invent new names for at
least one of each pair identical
attributes. Example R A B S B C
D 1 2 2 5 6 3 4 4 7
8 9 10 11
10Example(Continued)
The resulting relation will be R?S
A R.B S.B C D 1 2 2 5 6 1 2 4 7 8 1 2 9 10 11
3 4 2 5 6 3 4 4 7 8 3 4 9 10 11
11Natural Join
Denoted as R S. Let A1, A2,,An be the
attributes in both the schema of R and the schema
of S. Then a tuple r from R and a tuple s from S
are successfully paired if and only if r and s
agree on each of the attributes A1, A2, , An.
Example The natural join of the relation R and
S from previous example is A B C D
1 2 5 6 3 4 7 8
12Theta-Joins
- R C S.
- The result of this operation is constructed as
follows - Take the product of R and S.
- Select from the product only those tuples that
satisfy the condition C. - Schema for the result is the union of the schema
of R and S with, R or S prefix as necessary. -
-
13Example
Compute the natural and theta join for two
relations U and V A B C B C D 1 2 3 2 3 4 6 7
8 2 3 5 9 7 8 7 8 10 Relation U
Relation V
14Example
A B C D A U.B U.C V.B V.C
D 1 2 3 4 1 2 3
2 3 4 1 2 3 5
1 2 3 2 3
5 6 7 8 10 1 2 3
7 8 10 9 7 8 10
6 7 8 7 8
10 Result U V 9 7 8
7 8 10 Result of U AltD V
15Combing Operations to Form Queries
- What are the title and years of movies made by
Fox that are at least 100 minutes long? - One way to compute the answer to this query is
- Select those Movie tuples that have length ?
100. - Select those Movie tuples that have studioName
Fox. - Compute the intersection of first and second
steps. - Project the relation from the third step onto
attributes title and year.
16Example (Continued)
17Another Example
Consider two relations Movie1 and Movie2, With
schemas Movie1(title, year, length, filmType,
studioName) Movie2(title, year, starName) Suppose
we want to know Find the stars of the movies
that are at least 100 minutes long. First we
join the two relations Movie1, Movie2 Second we
select movies with length at least 100 min. Then
we project the starName.
18Renaming Operator
- In order to control the names of the attributes
used for relations that are constructed by one or
more applications of the relational algebra
operations, it is often convenient to use an
operator that explicitly renames relations. - We shall use the operator ?S(A1,A2,,An) (R) to
rename a relation R. - The resulting relation has exactly the same
tuples as R, but the name of the relation is S. - Moreover, the attributes of the result relation S
are named A1, A2, , An, in order from the left.
19Problem
Product(maker, model, type) PC(model, speed, ram,
hd, rd, price) Laptop(model, speed, ram, hd,
screen, price) Printer(model, color, type,
price) (Exercise 5.2.1)
20(No Transcript)
21Problem
- a) What PC models have a speed of at least 1000?
- b) Which manufacturers make laptops with a hard
disk of at least one gigabyte? - c) Find the model number and price of all
products (of any type) made by manufacturer B. - d) Find the model numbers of all color laser
printers. - e) Find those manufacturers that sell Laptops,
but not PC's. - !f) Find those hard-disk sizes that occur in two
or more PC's. - !g) Find those pairs of PC models that have both
the same speed and RAM. A pair should be listed
once. - !!h)Find those manufacturers of at least two
different computers (PC or Laptops) with speed of
at least 700.