如何使用以下代码创建触发器以停止预订表中的重叠日期?目前它无法正常工作,我收到以下错误。
(SELECT * 来自booking_requests 在哪里DateOfArrival< = NEW。'在第3行
CREATE TRIGGER date_overlap
BEFORE INSERT ON hotel_booking
IF EXISTS
(
SELECT *
FROM booking_requests
WHERE DateOfArrival <= NEW.DateOfDeparture
AND DateOfDeparture >= NEW.DateOfArrival
AND (RoomNumber != NEW.RoomNumber)
)
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'overlapping date entries are not supported.';
END IF;
答案 0 :(得分:0)
IF EXISTS (
... # this part looks about right, except for the last line, which
# you only need in the update trigger and should simply say:
AND (id != NEW.id)
) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'overlapping date entries are not supported.';
END IF;
适用于MySQL Server 5.5及更高版本。
这需要同时包含BEFORE INSERT
和BEFORE UPDATE
触发器。
(date_start,date_end)和(date_end,date_start)上的多列索引将允许优化器快速识别冲突行的最佳机会,因为它可以通过任一列上的范围进行搜索并扫描其他列中的值。直接索引,用它作为覆盖索引。