我正在为 DB 类(class)构建 jail 管理数据库,并且正在开发一个触发器,以便在囚犯刑期的开始日期和结束日期不正确时拒绝插入。
我知道有更好的方法可以做到这一点,但我需要使用触发器。
这是我的想法,我不知道我是否正确使用了 SIGNAL 功能:
CREATE TRIGGER `Prisoner_Date_Check` BEFORE INSERT ON `Prisoner`
FOR EACH ROW begin
if (new.sentence_end-new.sentence_start) < 0 then
SIGNAL sqlstate '12345'
SET MESSAGE_TEXT = 'Incorrect Prisoner.start_date or Prisoner.end_date';
END IF;
if new.sentence_end < current_date then
SIGNAL sqlstate '12345'
SET MESSAGE_TEXT = 'Incorrect Prisoner.end_date, less than current_date';
END IF;
if new.sentence_start > current_date then
SIGNAL sqlstate '12345'
SET MESSAGE_TEXT = 'Incorrect Prisoner.start_date, greater than current_date';
end if;
END$$
当我运行脚本时,出现此错误。
Error Code: 1235. This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table'
最佳答案
SIGNAL
的这种用法很好。
这里失败的是CREATE TRIGGER
,因为已经为同一事件的同一个表定义了另一个触发器。
首先使用DROP TRIGGER
。
关于mysql - 触发和信号逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23506765/