sqloracle-databaseconstraintsoverlapoverlap2d

Oracle SQL: How to use CHECK Constraint to not allow overlap reservation dates


I am hanging on a problem

Imagine you are the manager of a hotel that has a database in its database

CREATE TABLE Hotel
       (roomnr SMALLINT NOT NULL,
        arrival DATE NOT NULL,
        departure DATE NOT NULL,
        guest CHAR (30),
        PRIMARY KEY (roomnr, arrival)
        CHECK (departure >= arrival));

Table with the following definition:

So you can not leave this hotel before you arrive.

Change this definition so that you can not enter a reservation in the table whose arrival date conflicts with an existing departure date. Count as a collision


Solution

  • Oracle does not support a built-in range datatype (as far as I know). Hence, it does not have built-in overlap functionality.

    In order to implement such a check, you have two choices:

    (The second can be tricky in Oracle because of mutating table errors.) I think you need to write a function to use in a check constraint.