Top Banner
BİL 354 – Veritabanı Sistemleri Relational Algebra Examples (İlişkisel Cebir Örnekleri)
15
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: VTw5_exp

BİL 354 –

Veritabanı Sistemleri

Relational Algebra Examples (İlişkisel Cebir Örnekleri)

Page 2: VTw5_exp

2

Sailor Examples

In this example, we want to keep track of sailors who reserve boats.

We assume that a sailor reserves a boat for the full day.

We know the names of sailors and boats, but since two sailors or boats may have the same name, we also keep an id value for them.

We also keep track of Sailor ages, a rating value, and boat colors.

Obviously, a sailor might have done a lot of reservations in the past….

Page 3: VTw5_exp

3

Example Tables

Page 4: VTw5_exp

Find names of sailors who’ve reserved boat #103

Solution 1: sname bid

serves Sailors(( Re ) )103

Solution 2: ( , Re )Temp servesbid

1103

( , )Temp Temp Sailors2 1

sname Temp( )2

Solution 3: sname bidserves Sailors( (Re ))

103

Page 5: VTw5_exp

Find names of sailors who’ve reserved a red boat

Information about boat color only available in

Boats; so need an extra join:

sname color redBoats serves Sailors((

' ') Re )

A more efficient solution:

sname sid bid color redBoats s Sailors( ((

' ') Re ) )

A query optimizer can find this, given the first solution!

Page 6: VTw5_exp

6

Find the colors of boats reserved by Lubber.

color((σsname=‘Lubber’Sailors)Sailors ⋈ Reserves ⋈ Boats)

Page 7: VTw5_exp

7

sname(Sailors ⋈ Reserves)

Find the names of Sailors who have reserved at least one boat

Page 8: VTw5_exp

8

Find the names of sailors who have reserved a red or a green

boat.

Page 9: VTw5_exp

Find sailors who’ve reserved a red or a green boat

Can identify all red or green boats, then find sailors

who’ve reserved one of these boats:

( , (' ' ' '

))Tempboatscolor red color green

Boats

)Re( SailorsservesTempboatssname

Can also define Tempboats using union! (How?)

What happens if is replaced by in this query?

(Tempboats, (σcolor=‘red’Boats) ∪ (σcolor=‘green’Boats))

sname(Tempboats ⋈ Reserves ⋈ Sailors)

Page 10: VTw5_exp

Find sailors who’ve reserved a red and a green boat

Previous approach won’t work! Must identify

sailors who’ve reserved red boats, sailors who’ve

reserved green boats, then find the intersection

(note that sid is a key for Sailors):

( , ((' '

) Re ))Tempredsid color red

Boats serves

sname Tempred Tempgreen Sailors(( ) )

( , ((' '

) Re ))Tempgreensid color green

Boats serves

Page 11: VTw5_exp

11

(Reservations, sid,sname,bid(Sailors ⋈ Reserves))

(Reservationpairs(1sid1, 2sname, 3bid1, 4sid2,

5sname, 6bid2), ReservationsReservations)

sname1σ(sid1=sid2)(bid1bid2)Reservationpairs)

Find the names of sailors who have reserved at least two boats.

Page 12: VTw5_exp

12

Find the sids of sailors with age over 20 who have not

reserved a red boat.

sid(σage>20Sailors) - sid((σcolor=‘red’Boats) ⋈ Reserves ⋈ Sailors)

Page 13: VTw5_exp

13

Find the names of sailors who have reserved all boats.

Page 14: VTw5_exp

Find the names of sailors who’ve reserved all boats

Uses division; schemas of the input

relations to / must be carefully chosen:

( , (,

Re ) / ( ))Tempsidssid bid

servesbid

Boats

sname Tempsids Sailors( )

To find sailors who’ve reserved all ‘Interlake’ boats:

/ (' '

) bid bname Interlake

Boats

.....

Page 15: VTw5_exp

Find the names of sailors who’ve reserved all

boats called Interlake