# 1 Assignment 2 Relational Algebra Which tables? What operations? Common attributes? What result (attributes)? Syntax (Standard Notations and Symbols) –Product:

## Presentation on theme: "1 Assignment 2 Relational Algebra Which tables? What operations? Common attributes? What result (attributes)? Syntax (Standard Notations and Symbols) –Product:"— Presentation transcript:

1 Assignment 2 Relational Algebra Which tables? What operations? Common attributes? What result (attributes)? Syntax (Standard Notations and Symbols) –Product: X –Join:

2 I a) R (A1, A2) R = {(1, 2), (4, 3), (2, 9), (3, 8)} S (B1, B2, A1) S = {(x, a, 1), (y, b, 1), (z, a, 4)}  A1, B2 (  A1 < 4 and B1  x (S)) =  A1, B2 ( { (y, b, 1) } ) = { (1, b) }

3 I b) R (A1, A2) R = {(1, 2), (4, 3), (2, 9), (3, 8)} S (B1, B2, A1) S = {(x, a, 1), (y, b, 1), (z, a, 4)}  A2, B1 (  R.A1 > S.A1 ( R  S)) = { (3, x), (3, y), (8, x), (8, y), (9, x), (9, y)} R  S (1, 2, x, a, 1) (1, 2, y, b, 1) (1, 2, z, a, 4) (4, 3, x, a, 1) (4, 3, y, b, 1) (4, 3, z, a, 4) (2, 9, x, a, 1) (2, 9, y, b, 1) (2, 9, z, a, 4) (3, 8, x, a, 1) (3, 8, y, b, 1) (3, 8, z, a, 4)  R.A1 > S.A1 ( R  S) (4, 3, x, a, 1) (4, 3, y, b, 1) (2, 9, x, a, 1) (2, 9, y, b, 1) (3, 8, x, a, 1) (3, 8, y, b, 1)

4 I c) R (A1, A2) R = {(1, 2), (4, 3), (2, 9), (3, 8)} S (B1, B2, A1) S = {(x, a, 1), (y, b, 1), (z, a, 4)} R S = { (1, 2, x, a), (1, 2, y, b), (4, 3, z, a)} R  S (1, 2, x, a, 1) (1, 2, y, b, 1) (1, 2, z, a, 4) (4, 3, x, a, 1) (4, 3, y, b, 1) (4, 3, z, a, 4) (2, 9, x, a, 1) (2, 9, y, b, 1) (2, 9, z, a, 4) (3, 8, x, a, 1) (3, 8, y, b, 1) (3, 8, z, a, 4)  R.A1 = S.A1 ( R  S) (1, 2, x, a, 1) (1, 2, y, b, 1) (4, 3, z, a, 4)

5 I d) R (A1, A2) R = {(1, 2), (4, 3), (2, 9), (3, 8)} S (B1, B2, A1) S = {(x, a, 1), (y, b, 1), (z, a, 4)}  A2, B1, B2, S.A1 (R S) = { (2, x, a, 1), (2, y, b, 1), (3, z, a, 4)} R  S (1, 2, x, a, 1) (4, 3, x, a, 1) (2, 9, x, a, 1) (3, 8, x, a, 1) (1, 2, y, b, 1) (4, 3, y, b, 1) (2, 9, y, b, 1) (3, 8, y, b, 1) (1, 2, z, a, 4) (4, 3, z, a, 4) (2, 9, z, a, 4) (3, 8, z, a, 4) R S (1, 2, x, a, 1) (1, 2, y, b, 1) (4, 3, z, a, 4)

6 I e) R (A1, A2) R = {(1, 2), (4, 3), (2, 9), (3, 8)} S (B1, B2, A1) S = {(x, a, 1), (y, b, 1), (z, a, 4)} R S = { (1, 2, x, a), (1, 2, y, b), (4, 3, z, a), (2, 9, null, null), (3, 8, null, null)} R  S (1, 2, x, a, 1) (1, 2, y, b, 1) (1, 2, z, a, 4) (4, 3, x, a, 1) (4, 3, y, b, 1) (4, 3, z, a, 4) (2, 9, x, a, 1) (2, 9, y, b, 1) (2, 9, z, a, 4) (3, 8, x, a, 1) (3, 8, y, b, 1) (3, 8, z, a, 4) R S (1, 2, x, a, 1) (1, 2, y, b, 1) (4, 3, z, a, 4) (2, 9, null, null, null) (3, 8, null, null, null)

7 II (a) List all hotels Hotel (hotelNo, hotelName, city)  * (Hotel) Use H for Hotel  * (H)  hotelNo, hotelName, city (Hotel)  hotelNo (Hotel) Incorrect! All attributes when not specified.

8 b) List all single rooms with a price below £20 per night Room (roomNo, hotelNo, type, price)  (type = ‘Single’ and price < 20) (Room)

9 b) List all single rooms with a price below £20 per night  * (  (type = ‘Single’ and price < 20) (Room) ) Projection is NOT Needed!  (type = Single and price < 20) (Room)  (type = “Single” and price < 20) (Room)  (type = ‘Single’ and price < £20) (Room)  (type = ‘Single’ and price < 20 pounds) (Room)  (type = ‘Single’  price < 20) (Room) Incorrect notation!

10 c) List the name and addresses of all guests Guest (guestNo, guestName, guestAddress)  guestName, guestAddress (Guest) One table only! guestAddress: (street, city, state, zipcode) composite attribute

11 c) List the name and addresses of all guests Guest (guestNo, guestName, guestAddress)  guestName, guestAddress (Guest)  guestName, guestAddress (Guest Booking Hotel ) Don’t need Joins!

12 d) List the price and type of all rooms at the Grosvenor Hotel Room (RoomNo, HotelNo, Type, Price) Hotel (HotelNo, hotelName)  price, type (  hotelName = ‘Grosvenor’ (Room Hotel)) Common attribute: HotelNo  price, type (Room (hotelName = ‘Grosvenor’) (Hotel)) Incorrect! It’s the same as the following:  price, type (  (hotelName = ‘Grosvenor’) (Room  Hotel))

13 e) List all guests currently staying at the Grosvenor Hotel Guest (guestNo, guestName, guestAddress) Hotel (hotelNo, hotelName) Booking (hotelNo, guestNo, dateFrom, dateTo, roomNo)  Guest.* (  (fromDate = CurrentDate) and hotelName = ‘Grosvenor’) (Guest Booking Hotel))

14 e) List all guests currently staying at the Grosvenor Hotel Guest (guestNo, guestName, guestAddress) Hotel (hotelNo, hotelName) Booking (hotelNo, guestNo, dateFrom, dateTo, roomNo) What is the common attribute between Guest and Hotel?  Guest.* (  (fromDate = CurrentDate) and hotelName = ‘Grosvenor’) (Guest Hotel Booking) )  Guest.* (  (fromDate = CurrentDate) and hotelName = ‘Grosvenor’) (Guest (Hotel Booking)))

15 f) List the details of all rooms at the Grosvenor Hotel, including the name of the guest staying in the room if the room is occupied Room Guest  Room.*, guestName  (hotelName = ‘Grosvenor’ and fromDate <= CurrentDate and CurrentDate <= toDate) ((Room Hotel) (Booking Guest)) Not correct!  Room.*, guestName ( (  hotelName = ‘Grosvenor’ (Room Hotel) ) (  (fromDate <= CurrentDate and CurrentDate <= toDate) (Booking Guest)) )

Download ppt "1 Assignment 2 Relational Algebra Which tables? What operations? Common attributes? What result (attributes)? Syntax (Standard Notations and Symbols) –Product:"

Similar presentations