cs 157b spring 2008

10
CS 157B Spring 2008 Prof. Sin Min Lee Presented by Li Ch’i Ooi

Upload: peggy

Post on 01-Feb-2016

44 views

Category:

Documents


0 download

DESCRIPTION

CS 157B Spring 2008. Prof. Sin Min Lee Presented by Li Ch’i Ooi. Manipulating Information with the Relational Algebra [Ch. 6.1]. Relation is a set of tuples and that each tuple in a relation has the same number and types of attributes. Relational algebra includes : Selection Operators - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 157B Spring 2008

CS 157B Spring 2008CS 157B Spring 2008Prof. Sin Min Lee

Presented by Li Ch’i Ooi

Prof. Sin Min LeePresented by Li Ch’i Ooi

Page 2: CS 157B Spring 2008

Manipulating Information with the Relational Algebra

[Ch. 6.1]

Manipulating Information with the Relational Algebra

[Ch. 6.1]

Relation is a set of tuples and that each tuple in a relation has the same number and types of attributes. Relational algebra includes :Selection OperatorsProjection OperatorsSet OperatorsJoin and product Operations

Relation is a set of tuples and that each tuple in a relation has the same number and types of attributes. Relational algebra includes :Selection OperatorsProjection OperatorsSet OperatorsJoin and product Operations

Page 3: CS 157B Spring 2008

Selection Operators ()Selection Operators ()

Reduce the number of tuples in a set by selecting those that satisfy some criteria.

Example : lastName = ‘Doe’ (Customer) [ Select from Customer where

lastName = ‘Doe’ ]

CustomerCustomer

Reduce the number of tuples in a set by selecting those that satisfy some criteria.

Example : lastName = ‘Doe’ (Customer) [ Select from Customer where

lastName = ‘Doe’ ]

CustomerCustomerAccoun

t IdLast

NameFirst Name

Street City State

Zip Code

Balance

101 Block Jane 345 Randolph Circle Apopka FL 30458- $0.00

102 Hamilton Cherry 3230 Dade St. Dade City FL 30555- $3.00

103 Harrison Katherine 103 Landis Hall Bratt FL 30457- $31.00

104 Breaux Carroll 76 Main St. Apopka FL 30458- $35.00

106 Morehouse

Anita 9501 Lafayette St. Houma LA 44099- $0.00

111 Doe Jane 123 Main St. Apopka FL 30458- $0.00

201 Greaves Joseph 14325 N. Bankside St. Godfrey IL 43580- $0.00

444 Doe Jane Cawthon Dorm, room 142

Tallahassee

FL 32306- $10.55

Page 4: CS 157B Spring 2008

Projection Operators ()Projection Operators ()

Reduce the size of each tuple in a set by eliminating specific attributes.

Example : lastName, firstNAme (Customer) [ project customer onto (lastName,

firstName) ]

CustomerCustomer

Reduce the size of each tuple in a set by eliminating specific attributes.

Example : lastName, firstNAme (Customer) [ project customer onto (lastName,

firstName) ]

CustomerCustomer

Account Id

Last Name

First Name

Street City State

Zip Code

Balance

101 Block Jane 345 Randolph Circle Apopka FL 30458- $0.00

102 Hamilton Cherry 3230 Dade St. Dade City FL 30555- $3.00

103 Harrison Katherine 103 Landis Hall Bratt FL 30457- $31.00

104 Breaux Carroll 76 Main St. Apopka FL 30458- $35.00

106 Morehouse

Anita 9501 Lafayette St. Houma LA 44099- $0.00

111 Doe Jane 123 Main St. Apopka FL 30458- $0.00

201 Greaves Joseph 14325 N. Bankside St. Godfrey IL 43580- $0.00

444 Doe Jane Cawthon Dorm, room 142

Tallahassee

FL 32306- $10.55

Page 5: CS 157B Spring 2008

Set Operators ( -)Set Operators ( -)

Manipulate two similar sets of tuples by combining or comparing.

Example : Rental PreviousRental Rental PreviousRental

Manipulate two similar sets of tuples by combining or comparing.

Example : Rental PreviousRental Rental PreviousRental

accountId

videoID dateRented

dateDue

cost

103 101 1/3/99 1/4/99 $1.59

101 113 2/22/99 2/25/99 $3.00

101 114 2/2/99 2/25/99 $3.00

103 123 12/1/98 12/31/98

$10.99

101 145 2/14/99 2/16/99 $1.99

101 90987 1/1/99 1/8/99 $2.99

101 99787 1/1/99 1/4/99 $3.49

accountId

videoId

dateRented

dateReturned

cost

101 101 12/9/98 12/10/98 $2.49

101 112 1/13/98 1/4/98 $1.99

101 113 1/15/99 1/15/99 $0.99

102 113 12/1/98 12/3/98 $2.49

111 101 12/4/98 12/6/98 $2.49

111 99787 1/1/99 1/4/99 $3.95

201 113 129/98 12/14/98 $3.99

201 77564 1/14/99 1/24/99 $3.35

Page 6: CS 157B Spring 2008

Set Operators ( -) ...con’t

Set Operators ( -) ...con’t

• The union of two relations is a relation that contains the set of each tuple that is in at least one of the input relations.

Partial result of the Rental PreviousRentalaccountId videoId dateRented dateDue cost

101 90987 1/1/99 1/8/99 $2.99

101 99787 1/1/99 1/4/99 $3.49

103 101 1/3/99 1/4/99 $1.59

103 123 12/1/98 12/31/98 $10.99

111 101 12/4/98 12/6/98 $2.49

201 77564 1/14/99 1/24/99 $3.35

Page 7: CS 157B Spring 2008

Set Operators ( -) ...con’t

Set Operators ( -) ...con’t

The intersection of two relations is the set of all tuples that occur in both input relations.

The intersection of the relations Rental PreviousRental in the previous example will return an empty set.

Another example would be the intersection between the video IDs of the two tables.

videoId (Rental) videoId (PrevioutsRental) = Videotapes that are currently rented as well as those that have been rented before.

The set of all videotapes that have been rented previously but are not currently rented is expressed as follows: videoId (PreviousRental) - videoId (Rental)

The intersection of two relations is the set of all tuples that occur in both input relations.

The intersection of the relations Rental PreviousRental in the previous example will return an empty set.

Another example would be the intersection between the video IDs of the two tables.

videoId (Rental) videoId (PrevioutsRental) = Videotapes that are currently rented as well as those that have been rented before.

The set of all videotapes that have been rented previously but are not currently rented is expressed as follows: videoId (PreviousRental) - videoId (Rental)

Page 8: CS 157B Spring 2008

Join and Product Operations ()

Join and Product Operations ()

Increase the size of each tuple by adding attributes The Cartesian product produces a tuple of the new

realtion for each combination of one tuple from the left operand and one tuple from the right operand. Example : Employee TimeCard

Employee TimeCard

Increase the size of each tuple by adding attributes The Cartesian product produces a tuple of the new

realtion for each combination of one tuple from the left operand and one tuple from the right operand. Example : Employee TimeCard

Employee TimeCardssn lastNam

efirstNam

e

145-09-0967

Uno Jane

245-11-4554

Toulouse Jennifer

376-77-0099

Threat Ayisha

479-98-0098

Fortune Bruce

588-99-0093

Fivozinsky

Bruce

ssn date startTime

endTime

storeId

paid

145-09-0967

01/14/99

8:15 12:00 3 yes

245-11-4554

01/14/99

8:15 12:00 3 yes

376-77-0099

02/23/99

14:00 22:00 5 yes

145-09-0967

01/16/99

8:15 12:00 3 yes

376-77-0099

01/03/99

10:00 14:00 5 yes

376-77-0099

01/03/99

15:00 19:00 5 yes

Page 9: CS 157B Spring 2008

Join and Product Operations () ...con’t

Join and Product Operations () ...con’t

The result of this operation has 30 tuples because there are 5 Employee and 6 TimeCard.

Partial result of Cartesian product Employee TimeCard

The result of this operation has 30 tuples because there are 5 Employee and 6 TimeCard.

Partial result of Cartesian product Employee TimeCard

Employee.ssn

lastName

firstName

TimeCard.ssn

Date startTime

endTime

storeId

paid

145-09-0967

Uno Jane 145-09-0967

01/14/99

8:15 12:00 3 no

245-11-4554

Toulouse Jie 245-11-4554

01/14/99

8:15 12:00 3 no

145-09-0967

Uno Jane 376-77-0099

02/23/99

14:00 22:00 5 no

245-11-4554

Toulouse Jie 145-09-967 01/14/99

8:15 12:00 3 no

Page 10: CS 157B Spring 2008

Join and Product Operations () ...con’t

Join and Product Operations () ...con’t

A selection of those tuples where Employee.ssn equals TimeCard.ssn can be expressed by :

Employee.ssn = TimeCard.ssn (Employee TimeCard) This type of product is called a join. The join operation

puts together related objects from two relations. A Natural Join however is defined so that the shared

attribute appears only once in the output table. Ref. textbook Table 6.6 [natural join] vs Table 6.7 [join]

A selection of those tuples where Employee.ssn equals TimeCard.ssn can be expressed by :

Employee.ssn = TimeCard.ssn (Employee TimeCard) This type of product is called a join. The join operation

puts together related objects from two relations. A Natural Join however is defined so that the shared

attribute appears only once in the output table. Ref. textbook Table 6.6 [natural join] vs Table 6.7 [join]