I have a hotel booking system.
I have a table Rooms with two basic columns:-
- Room_No (Primary key)
- AVAILABLE_FROM_DATE (Date)
I have a booking request with below parameters:-
- Booking_ID
- Booking_start_date (Date)
- Booking_end_date (Date)
So for every booking , I need to check if a room is available within booking_start_date and booking_end_date. Using somewhat below query right now:-
SELECT Room_No
FROM Rooms
WHERE AVAILABLE_FROM_DATE >= booking_start_date
AND AVAILABLEFROMDATE < booking_end_date;
If available, then I need to allocate that room to that particular Booking_ID for that particular start_date, end_date pair only.
I need to update the same information in Rooms table for that particular room_no , so that a room is not booked twice for a particular date range.
For now I am doing this by updating AVAILABLE_FROM_DATE
column as booking_end_date + 1
.
Problem is with current implementation I can keep track of only one date range.
So , in case My room is available from 1 Jan , and a booking comes for 1 Feb- 10 Feb, I update the AVAILABLE_FROM_DATE
to 11 Feb.
So for another booking, say 1 Jan - 31st Jan, although my room was available but I was not able to allocate it.
Is there any way I can keep record of all the date ranges within which my room is available so that I can better allocate the rooms.
I am thinking of making a separate table to store multiple booked (start, end) date ranges for every Room_No but the Rooms table can be very big (upto 5000 rows), so I need to take care of efficiency as well.
Any suggestions on how should I proceed with my problem to achieve maximum allocation?