80 likes | 251 Views
Relational Algebra & Calculus. Chapter 4, Part A (Relational Algebra) Query Formulation Exercise. Exercises. Given relational schema: Sailors (sid, sname, rating, age) Reservation (sid, bid, date) Boats (bid, bname, color) Find names of sailors who’ve reserved boat #103
E N D
Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra) Query Formulation Exercise
Exercises Given relational schema: Sailors (sid, sname, rating, age) Reservation (sid, bid, date) Boats (bid, bname, color) • Find names of sailors who’ve reserved boat #103 • Find names of sailors who’ve reserved a red boat • Find sailors who’ve reserved a red or a green boat • Find sailors who’ve reserved a red and a green boat • Find the names of sailors who’ve reserved all boats
Solution 2: • Solution 3: 1) Find names of sailors who’ve reserved boat #103 • Solution 1:
A more efficient solution -- why more efficient? 2) Find names of sailors who’ve reserved a red boat • Boats (bid, bname, color) • Information about boat color only available in Boats; so need an extra join: A query optimizer can find this, given the first solution!
What happens if is replaced by in this query? 3) 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: • Can also define Tempboats using union! (How?)
4) Find sailors who’ve reserved a red and a green boat • Previous approach won’t work! Why? • Must identify sailors who’ve reserved red boats, sailors who’ve reserved green boats, then find the intersection (note thatsid is a key for Sailors):
4) Find sailors who’ve reserved a red and a green boat • Previous approach won’t work! Why? • Must identify sailors who’ve reserved red boats, sailors who’ve reserved green boats, then find the intersection (note thatsid is a key for Sailors):
5) Find the names of sailors who’ve reserved all boats • Uses division; schemas of the input relations to division (/) must be carefully chosen: • To find sailors who’ve reserved all ‘Interlake’ boats: .....