Top Banner
Operations 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.
21

Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Operations 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.

Page 2: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

OperationsOperations of relational algebra fall into four broad classes:

1. The usual set operations

union

intersection

difference

2. Operations that remove parts of a relation:

selection eliminates some rows(tuples)

projection eliminates some columns

3. 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.

4. An operation called “renaming”.

Page 3: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Conditions 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:

1. R and S must have schemas with identical sets of attributes.

2. 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.

Page 4: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Set 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.

Page 5: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Projection

Produces from a relation R a new relation that has only some of R’s 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

Wayne’s World 1992 95 color Paramount 99999

Page 6: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Example (Continued)

Resulting relation:

title year length

Star wars 1977 124

Mighty Ducks 1991 104

Wayne’s World 1992 95

What about filmtype(Movies)

Page 7: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

SelectionThe selection operator, applied to a relation R, produces a new relation with a subset of R’s 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 R’s schema.

Example:

The expression length100(Movie) is:

title year length filmType studioName producerC#

Star wars 1977 124 color Fox 12345

Mighty Ducks 1991 104 color Disney 67890

Page 8: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

ExampleSuppose 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:

length100 And studioName=’Fox’(Movie)

The tuple

title year length filmType studioName producerC#

Star wars 1977 124 color Fox 12345

Is the only one in the resulting relation.

Page 9: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Cartesian ProductThe 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 RS.

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

Page 10: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Example(Continued)The resulting relation will be:

RS 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

Page 11: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Natural JoinDenoted 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

Page 12: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Theta-JoinsR 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.

Page 13: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Example

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

Page 14: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Example

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 A<D V

Page 15: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Combing 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.

Page 16: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Example (Continued)

title,year( length100(Movie) studioName=‘Fox’(Movie))

title,year

length100 studioName=‘Fox’

Movie Movie

Page 17: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Another ExampleConsider 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.

Page 18: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Renaming 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.

Page 19: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Problem

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)

Page 20: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.
Page 21: Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Problem

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.