I want to create a check constraint for my table, where you can't add new line, if the new booking range(start_date, end_date) have intersect with an already submitted row. But i can't place query in a check constraint. Do you have an idea how to do this?
The tables:
APARTMAN
id INT
price INT
BOOKINGS
id INT
start_date DATE
end_date DATE
apartman_id INT
[apartman_id] IN (SELECT [id] FROM [dbo].[APARTMAN]
WHERE [id] NOT IN (
SELECT [apartman_id] FROM
[dbo].[BOOKINGS]
WHERE
([start_date] <= "requested end_date" AND
[end_date] >= "requested start_date" )
OR
([start_date] <= "requested start_date" AND
[end_date] >= "requested end_date" )
OR
(([start_date] <= "requested end_date" AND [end_date] >= "requested start_date" )
OR
([end_date] <= "requested start_date" AND [end_date] >= "requested end_date" ))
)
)