Tham khảo tài liệu 'sql puzzles & answers- p2', công nghệ thông tin, cơ sở dữ liệu phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 22 PUZZLE 6 HOTEL RESERVATIONS Answer 2 Another solution is to redesign the table giving a row for each day and each room thus CREATE TABLE Hotel room_nbr INTEGER NOT NULL occupy_date DATE NOT NULL guest_name CHAR 30 NOT NULL PRIMARY KEY room_nbr occupy_date guest_name This does not need any check clauses but it can take up disk space and add some redundancy. Given cheap storage today this might not be a problem but redundancy always is. You will also need to find a way in the INSERT statements to be sure that you put in all the room days without any gaps. As an aside in full SQL-92 you will have an OVERLAPS predicate that tests to see if two time intervals overlap a temporal version of the BETWEEN predicate currently in SQL implementations. Only a few products have implemented it so far. Answer 3 Lothar Flatz an instructor for Oracle Software Switzerland made the objection that the clause BETWEEN AND does not allow for a guest name to arrive the same day another guest name leaves. Since Oracle cannot put subqueries into CHECK constraints and triggers would not be possible because of the mutating table problem he used a VIEW that has a WITH CHECK OPTION to enforce the occupancy constraints CREATE VIEW HotelStays room_nbr arrival_date departure_date guest_name AS SELECT room_nbr arrival_date departure_date guest_name FROM Hotel AS H1 WHERE NOT EXISTS SELECT FROM Hotel AS H2 WHERE AND Please purchase PDF Split-Merge on to remove this watermark. o PUZZLE 6 HOTEL RESERVATIONS 23 AND WITH CHECK OPTION For example INSERT INTO HotelStays VALUES 1 2008-01-01 2008-01-03 Coe COMMIT INSERT INTO HotelStays VALUES 1 2008-01-03 2008-01-05 Doe will give a WITH CHECK OPTION clause violation on the second INSERT INTO statement. This is a good trick for getting table-level constraints in a table on products without full SQL-92 .