Uploaded by Vũ Hùng

SE171367 Duong-Nhat-Minh Lab-1-DBI202

advertisement
Giving a database schema:
-
Sailors(sid: integer, sname: string, rating: integer, age:real)
-
Boats(bid:integer , bname: string, color: string)
-
Reserves(sid: integer, bid: integer , day: date )
Sid
22
29
31
32
58
64
71
74
85
95
Sname
Dustin
Brutus
Lubber
Andy
Rusty
Horatio
Zorba
Horatio
Art
Bob
Rating
7
1
8
8
10
7
10
9
3
3
Age
45.0
33.0
55.5
25.5
35.0
35.0
16.0
35.0
25.5
63.5
Sid
22
22
22
22
31
31
31
64
64
74
Bid
101
102
103
104
102
103
104
101
102
103
Day
10/10/08
10/10/08
10/08/08
10/07/08
11/10/08
11/06/08
11/12/08
9/05/08
9/08/08
9/08/08
Bid
101
102
103
104
Bname
Interlake
Interlake
Clipper
Marine
Boats
Sailors
Reserves
Using relational algebra expression to answer below queries
1. Find the names of sailors who have reserved boat 103
π Sname(σ bid=103 (Sailor ⋈ Reserves))
2. Find the names of sailors who have reserved a red boat
π Sname[σColor=’Red’(Boats) ⋈ Reserves ⋈ Sailors]
3. Find the colors of boats reserved by Lubber.
π Color[σSname=’Lubber’(Sailors) ⋈ Reserves ⋈ Boats]
4. Find the names of sailors who have reserved at least one boat.
π Sname[π Sid(Reserves) ⋈ Sailors]
5. Find the names of sailors who have reserved a red or a green boat
π Sname[σColor=’Red’ ⋁ Color=’Green’(Boats) ⋈ Reserves ⋈ Sailors]
6. Find the names of sailors who have reserved a red and a green boat
π Sname[σColor=’Red’ (Boats) ⋈ σColor=’Green’(Boats) ⋈ Reserves ⋈
Sailors]
7. Find the sids of sailors with age over 20 who have not reserved a red boat
π Sid[σAge>20 (Sailors)] \ π Sid[σColor=’Red’(Boats) ⋈ Reserves]
Color
Blue
Red
Green
Red
8. Find the names of sailors who have reserved all boats
π Sname(σ Bid=101 (Sailor ⋈ Reserves))  π Sname(σ Bid=102 (Sailor ⋈ Reserves))  π
Sname(σ Bid=103 (Sailor ⋈ Reserves))  π Sname(σ Bid=104 (Sailor ⋈ Reserves))
9. Find the names of sailors who have reserved all boats called Interlake
π Sname[σBname=’Interlake’ (Boats) ⋈ Reserves ⋈ Sailors]
10. Find the names of sailors who have reserved at least two boats
π Sname (Sailor ⋈(σ Reserves.Sid = R2.Sid ⋀ Reserves.Bid != R2.Bid (Reserves ><p R2(Sid, Bid, Day)
(Reserves))))
Download