Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 3630 Database Design and Implementation

Similar presentations


Presentation on theme: "CS 3630 Database Design and Implementation"— Presentation transcript:

1 CS 3630 Database Design and Implementation

2 Joins Retrieve data from two or more tables Join Conditions
PK and FK (Natural Join) Other attributes (Theta Join) Based on Cartesian Product Implementation Nested loops Optimization

3 Tables Schemas Assignment 7

4 Joins List all guests who have at least one booking.
(We keep all guests even they don’t have any bookings.) -- Which table(s)? -- Guest (Guest_no, Guest_Name, Address) PK: Guest_no -- Booking (HoteL_no, Guest_no, ...) PK: Hotel_no, Guest_No, Date_From FK: Guest_no references Guest Select Guest.* From Guest, Booking Where Guest.Guest_no = Booking.Guest_no;

5 Joins: New Style Select Guest.* From Guest, Booking
Where Guest.Guest_no = Booking.Guest_no; From Guest Join Booking on Guest.Guest_no = Booking.Guest_no; We use the new style! -- Run in Oracle Duplicate records

6 Joins List all guests who have at least one booking. Select Guest.*
From Guest Join Booking on Guest.Guest_no = Booking.Guest_no; Why duplicate records? For every g in Guest For every b in Booking If g.Guest_no = b.Guest_no Then Select g.*

7 Always remove duplicate records!
Joins Always remove duplicate records! Select Distinct Guest.* From Guest Join Booking on Guest.Guest_no = Booking.Guest_no; Select Unique Guest.*

8 Using Short Table Names
List all guests who have at least one booking. Select Distinct G.* From Guest G Join Booking B on G.Guest_no = B.Guest_no; Pause -- Cannot use the original table name any more Select Distinct Guest.* on Guest.Guest_no = B.Guest_no; ERROR at line 1: ORA-00904: "GUEST"."GUEST_NO": invalid identifier

9 Joins List all guests who have at least one booking
with the details of each booking. -- No need for Distinct -- All columns from both tables Select * From Guest G Join Booking B on G.Guest_no = B.Guest_no; Set linesize 100 Col Guest_name Format a14 Heading “Guest Name” Col address format a21

10 Joins What if no common attribute is specified? Select G.*
From Guest G, Booking B; -- Where G.Guest_no = B.Guest_no; -- Cartesian product! Select Distinct G.* From Guest G Join Booking B; -- on G.Guest_no = B.Guest_no; ERROR at line 3: ORA-00905: missing keyword

11 Join not on PK/FK List all guests who are in a city where there is also a hotel. Assuming Address is City. Select Distinct G.* From Guest G Join Hotel H on G.Address = H.Address;

12 Joins: Three Tables List Hotel name, guest name and date_from
for all bookings, sorted by hotel_no in ascending order and then by guest_no in descending order. Which tables? Hotel Name: Hotel Guest Name: Guest Date_From : Booking Select H.name, G.Guest_name, Date_from From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Join Guest G on G.Guest_no = B.Guest_no Order By H.Hotel_No, G.Guest_no desc; -- H.Hotel_No, G.Guest_no not selected

13 Joins: Three Tables -- List Hotel name, guest name and date_from
-- for all bookings. -- Sort the result Select H.name, G.Guest_name, Date_from From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Join Guest G on G.Guest_no = B.Guest_no Order By H.name, G.Guest_name, Date_from; -- Sort by name, Guest_name, Date_from; Order By H.Hotel_No, G.Guest_no; -- Sort by Hotel_No, Guest_no;

14 Joins with Group By -- For each hotel, display Hotel name and number of -- bookings of the hotel. -- Tables: -- Hotel, Booking Select name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No; -- Will it work?

15 Joins with Group By -- For each hotel, display Hotel name and number of -- bookings of the hotel. -- Tables: -- Hotel, Booking -- Must also Group by name Select name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No, name; -- What if group by name only?

16 Joins with Group By -- For each hotel, display Hotel name and
-- number of bookings of the hotel. Select name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No, name; -- Missing hotels without bookings -- How to display a zero for such hotels? Select * From hotel;

17 Current Year with Assumption
-- For each hotel, display Hotel number with hotel name -- and number of bookings of the hotel for this year. -- Assume no booking is longer than 1 year. Select H.Hotel_No, name, count(*) From Hotel H 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;

18 Be Careful about And/Or
-- For each hotel, display Hotel number with hotel name -- and number of bookings of the hotel for this year. -- Assume no booking is longer than 1 year. Select H.Hotel_No, name, count(*) From Hotel H 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; -- and ((to_char(Date_From, 'yyyy') = to_char(SysDate, 'yyyy')) or (to_char(Date_To, 'yyyy') = to_char(SysDate, 'yyyy')))

19 Join and Where -- For each hotel, display Hotel number with hotel name
-- and number of bookings of the hotel for this year. -- Assume no booking is longer than 1 year. Select H.Hotel_No, name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Where 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; -- could use where after join condition -- Same result for the query -- But one is Join condition the other is selection condition -- We should do it this way!

20 Current Year without Assumption
-- For each hotel, display Hotel number with hotel name -- and number of bookings of the hotel for this year. -- No assumptions: booking could be longer than one year. Select H.Hotel_No, name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Where to_char(Date_From, 'yyyy') = to_char(SysDate, 'yyyy') or to_char(Date_To, 'yyyy') = to_char(SysDate, 'yyyy') (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;

21 Current Year without Assumption
-- For each hotel, display Hotel number with hotel name -- and number of bookings of the hotel for this year. -- No assumptions. Select H.Hotel_No, name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Where 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; -- How to make it work for the current month? Use ‘yyyy mm’ Not ‘yyyy’ Not ‘mm yyyy’

22 Group By and Having -- For each hotel with at least 3 bookings this
-- year, display Hotel number with the name and the number -- of bookings of the hotel this year. -- With assumption Select H.Hotel_no, name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Where to_char(sysDate, 'yyyy') = to_char(date_to, 'yyyy') or to_char(sysDate, 'yyyy') = to_char(date_from,'yyyy') Group by H.Hotel_No, name Having Count(*) >= 3;

23 Group By and Having -- For each hotel with more than 5 bookings,
-- display Hotel number with name and the number -- of bookings of the hotel. Select H.Hotel_No, name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No, name Having Count(*) > 5;

24 Joins Due Tuesday, April 25
Assignment 9 Joins Due Tuesday, April 25

25 Friday, April 14? Based on Assignment 8
Quiz 3 Friday, April 14? Based on Assignment 8


Download ppt "CS 3630 Database Design and Implementation"

Similar presentations


Ads by Google