CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking --...

19
CS 3630 Database Design and Implementation

Transcript of CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking --...

Page 1: CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

CS 3630 Database Design and Implementation

Page 2: CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

Joins -- For each booking, display the booking-- details with the room type and price

Select B.*, rtype, priceFrom Booking BJoin Room R on R.Hotel_no = B.Hotel_no;

-- Correct?

2

Page 3: CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

Joins -- For each hotel, display Hotel name and -- number of bookings of the hotel

Select name, count(*) From Hotel HJoin Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No, name;

// Missing hotels without bookings// How to get all hotels?

3

Page 4: CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

Joins-- For each hotel, display Hotel name and -- number of bookings of the hotel

Select name, count(*) From Hotel HJoin Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No, name;

Why Missing Hotels without bookings?

For every h in Hotel For every b in Booking If h.Hotel_no = b.Hotel_no Then generating a record

Group and counting4

Page 5: CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

Out Joins-- For each hotel, display Hotel name and -- number of bookings of the hotel

How to keep hotels without bookings?

For every h in Hotel joined = False For every b in Booking If h.Hotel_no = b.Hotel_no Then generating a record joined = True

Keep the record if joined is False (adding nulls for booking fields)

Group and counting5

Page 6: CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

Left Outer Join

-- For each hotel, display Hotel name and -- number of bookings of the hotel

Select name, count(*) From Hotel HLeft Join Booking B On H.Hotel_no = B.Hotel_no Group By H.Hotel_No, name;

-- Hotels without a booking have 1-- for count(*)

6

Page 7: CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

Left Outer Join

-- For each hotel, display Hotel name and -- number of bookings of the hotel

Select name, count(Guest_No)From Hotel HLeft Join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No, name;

-- Hotels without a booking have 0-- for count(Guest_No)

7

Page 8: CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

Left Outer Join

-- For each hotel, display Hotel name and -- number of bookings of the hotel

Select name, count(B.Hotel_No)From Hotel HLeft Join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No, name;

-- What if count(B.Hotel_No)-- YES!

8

Page 9: CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

Left Outer Join-- For each hotel, display Hotel name and -- number of bookings of the hotel

Select name, count(H.Hotel_No)From Hotel HLeft Join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No, name;

-- What if count(H.Hotel_No)-- NO!

9

Page 10: CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

Left Outer Join-- For each hotel, display Hotel name and -- number of bookings of the hotel,-- including hotels without any bookings.

Select name, count(B.Hotel_No)From Hotel HLeft Join Booking B on H.Hotel_no = B.Hotel_no Group by B.Hotel_No, name;

-- Group by B.Hotel_No instead of H.Hotel_No-- NO: Missing some hotels!-- Some records in the result have no B.Hotel_No!-- All records with the same name and a null value -- for B.Hotel_No are in one group!

10

Page 11: CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

Left Outer Join

-- For each hotel, display Hotel name and -- number of bookings of the hotel,-- including hotels without any bookings.

Select name, count(B.Hotel_No)From Hotel HLeft Join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No, name;

-- Group by name only-- Group by H.Hotel_No only

11

Page 12: CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

Right Outer Join -- For each hotel with at least one booking,-- display Hotel name and -- number of bookings of the hotel.

Select name, H.Hotel_No, count(B.Hotel_No)From Hotel HRight join Booking B on H.Hotel_no = B.Hotel_noGroup by H.Hotel_No, name;

-- NO: same as natural join-- since each booking must have a hotel_no

12

Page 13: CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

Joins

-- For each booking, display Hotel name, -- hotel_no, room no and guest_no.

Select name, H.Hotel_No, room_no, guest_noFrom Hotel HJoin Booking B on H.Hotel_no = B.Hotel_noOrder By H.Hotel_No, Room_No;-- No Outer Join!

13

Page 14: CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

Outer Joins and Where Clause

-- For each hotel, display Hotel name and number -- of bookings of the hotel during the current year,-- including hotels without any bookings during the -- current year.-- Assuming no booking is longer than one year.

Select name, H.Hotel_No, count(B.Hotel_No)From Hotel Hleft join Booking B on H.Hotel_no = B.Hotel_noWhere To_Char(Date_From, 'yyyy') = To_Char(sysDate, 'yyyy') Or To_Char(Date_To, 'yyyy') = To_Char(sysDate, 'yyyy')Group by H.Hotel_No, name;

-- Incorrect!-- Where condition is applied after Left Join

14

Page 15: CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

Outer Joins and Where Clause

-- For each hotel, display Hotel name and number -- of bookings of the hotel for the current year,-- including hotels without any bookings for the -- current year.

Select name, H.Hotel_No, count(B.Hotel_No)From Hotel Hleft join Booking B on H.Hotel_no = B.Hotel_no and (To_Char(Date_From, 'yyyy') = To_Char(sysDate, 'yyyy') Or To_Char(Date_To, 'yyyy') = To_Char(sysDate, 'yyyy'))Group by H.Hotel_No, name;

-- YES: Checking the Current Year condition when Left Join

15

Page 16: CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

Outer Joins and Where Clause

-- For each hotel, display Hotel name and number -- of bookings of the hotel for the current year,-- including hotels without any bookings for the -- current year.-- Assuming a booking could be longer than one year.

Select name, H.Hotel_No, count(B.Hotel_No)From Hotel Hleft join Booking B on H.Hotel_no = B.Hotel_no and (To_Char(Date_From, 'yyyy') <= To_Char(sysDate, 'yyyy') and To_Char(Date_To, 'yyyy') >= To_Char(sysDate, 'yyyy'))Group by H.Hotel_No, name;

-- YES: Checking the Current Year condition when Left Join

16

Page 17: CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

Assignment8

17

Page 18: CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

Quiz 3

Wednesday, April 15

No Join

Assignment 7 & 8

Must come to Lab 206

Use D2L

18

Page 19: CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

Quiz3

Remember Your Password!

Try it Today!

19