我想为我的表创建一个检查约束,如果新的预订范围(开始日期,结束日期)与已提交的行相交,则不能在其中添加新行。但我不能将查询放在检查约束中。你知道怎么做吗?
表格:
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" ))
)
)