触发器未在MySQL中触发

时间:2018-11-22 09:51:33

标签: mysql triggers

我正在尝试在mysql数据库(5.7.17-log)中设置触发器,以在插入/更新期间验证数据。但是,我无法触发触发器(附加了完整的sql脚本)。

以下是完整的脚本:

CREATE DATABASE IF NOT EXISTS `CUSTOMER_SERVICE`;
USE `CUSTOMER_SERVICE`;

CREATE TABLE IF NOT EXISTS `CustomerRecord` (
  `ID` VARCHAR(255) NOT NULL,
  `AGENT_ID` INTEGER DEFAULT 1,
  PRIMARY KEY (`ID`)
);

DELIMITER $$
CREATE PROCEDURE `AgentIdCheck`(IN AGENT_ID INTEGER)
BEGIN
    IF ((AGENT_ID < 0) || (AGENT_ID >= 1000)) THEN
        SIGNAL SQLSTATE '45000'
            SET MESSAGE_TEXT = 'AgentIdDCheck constraint failed';
    END IF;

END $$
DELIMITER ;

-- before insert
DELIMITER $$
CREATE TRIGGER `AgentIdCheck_Before_Insert` BEFORE INSERT ON `CustomerRecord`
FOR EACH ROW
BEGIN
    CALL AgentIdCheck(new.AGENT_ID);
END$$
DELIMITER ;

-- before update
DELIMITER $$
CREATE TRIGGER `AgentIdCheck_Before_Update` BEFORE UPDATE ON `CustomerRecord`
FOR EACH ROW
BEGIN
    CALL AgentIdCheck(new.AGENT_ID);
END $$
DELIMITER ;

INSERT INTO CustomerRecord (ID, AGENT_ID) VALUES ("Customer1", 5000);

根据对现有问题的搜索,我尝试了END $$END$$END;$$等。请提出建议。

1 个答案:

答案 0 :(得分:0)

触发器确实触发,但是您的过程逻辑不正确-应该为OR否和/ &&