VTw5_exp

15
BİL 354 – Veritabanı Sistemleri Relational Algebra Examples (İlişkisel Cebir Örnekleri)

description

Ganteng cakep

Transcript of VTw5_exp

BİL 354 –

Veritabanı Sistemleri

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

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

3

Example Tables

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

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!

6

Find the colors of boats reserved by Lubber.

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

7

sname(Sailors ⋈ Reserves)

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

8

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

boat.

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)

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

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.

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)

13

Find the names of sailors who have reserved all boats.

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

.....

Find the names of sailors who’ve reserved all

boats called Interlake