我试图在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条答案
按热度按时间pkmbmrz71#
触发器确实会触发,但您的过程逻辑不正确-应该触发还是不触发,以及/&&