I have the following table:
CREATE TABLE booking(
booking_id NUMBER(8) NOT NULL;
booking_start DATE NOT NULL;
booking_end DATE NOT NULL;
booking_room NUMBER(3) NOT NULL;
guest_no NUMBER(5) NOT NULL;
);
This table keeps track of all bookings for rooms in a particular hotel. I want to write a trigger to prevent bookings being added that overlap with the dates of previous bookings for particular rooms in the hotel.
For example, let's say that room 5 is currently booked from 01 Jan 2019 to 07 Jan 2019. If another booking is added for room 5 from 26 Dec 2018 to 03 Jan 2019 I want my trigger to prevent this data from being added to the bookings table. The same thing applies if a booking is made from 03 Jan 2019 to 10 Jan 2019, and also 02 Jan 2019 to 06 Jan 2019.
Basically booking start and end dates cannot overlap with other booking start and end dates for the same hotel room.
This is what I have tried so far:
CREATE OR REPLACE TRIGGER check_booking_valid
BEFORE INSERT ON booking
BEGIN
SELECT booking_start
FROM booking
WHERE booking_room = :new.booking_room;
SELECT booking_end
FROM booking
WHERE booking_room = :new.booking_room;
IF :new.booking_start > booking_start AND
:new.booking_start < booking_end
THEN raise_application_error(-20000, 'Invalid booking');
IF :new.booking_end > booking_start AND
:new.booking_end < booking_end
THEN raise_application_error(-20000, 'Invalid booking');
IF :new.booking_start > booking_start AND
:new.booking_start < booking_end AND
:new.booking_end > booking_start AND
:new.booking_end < booking_end
THEN raise_application_error(-20000, 'Invalid booking');
END IF;
END;
I am getting an error message saying "NEW or OLD references not allowed in table level triggers". I know that if I make this into a row-level trigger there may be a mutating-table error that is thrown.
Could anyone please point out what the error is?
Cheers!