MySQL触发前

时间:2012-01-13 13:00:21

标签: mysql sql triggers

我有两张桌子:

   CREATE TABLE `reservation` (
   `id_reservation` int(11) PRIMARY KEY,
   `reserved_date` date NOT NULL,
   `id_event` int(11) NOT NULL,   
   FOREIGN KEY (`id_event`) REFERENCES `event` 
   )

   CREATE TABLE `event` (
   `id_event` int(11) PRIMARY KEY,
   `begin_date` date NOT NULL,
   `end_date` date NOT NULL,   
   )

基本上我插入一个reservation只有一个reserved_datereservationevent相关,其中包含begin_dateend_date

我需要检查reserved_date中的begin_dateend_dateevent中是否包含{{1}}。

1 个答案:

答案 0 :(得分:0)

尝试此触发器 -

CREATE TRIGGER trigger1
  BEFORE INSERT
  ON reservation
  FOR EACH ROW
BEGIN
  DECLARE id_event_var INT DEFAULT NULL;
  SELECT id_event INTO id_event_var FROM `event` WHERE id_event = new.id_event AND new.reserved_date >= begin_date AND new.reserved_date <= end_date;
  IF id_event_var IS NULL THEN
    ... -- wrong date!
  END IF;
END