我有两栏佣金(百分比('是',#39;不'))和重复(enum('是',' no&#) 39;)。我想声明以下依赖项:
重复发生只能是“是”,当百分比是“是”时,
是否有可能在mysql中管理它?
答案 0 :(得分:1)
您可以创建两个触发器来检查,sqlfiddle
CREATE
TRIGGER `before_insert` BEFORE INSERT
ON `commission`
FOR EACH ROW BEGIN
IF NEW.recurring = 'yes' AND NEW.percental != 'yes' THEN
signal sqlstate '45000' set message_text = "percental must be 'yes' for recurring to be 'yes'";
END IF;
END/
CREATE
TRIGGER `before_update` BEFORE UPDATE
ON `commission`
FOR EACH ROW BEGIN
IF NEW.recurring = 'yes' AND NEW.percental != 'yes' THEN
signal sqlstate '45000' set message_text = "percental must be 'yes' for recurring to be 'yes'";
END IF;
END/
由于UPDATE和INSERT的代码相同,您可能需要创建一个程序来调用这两个触发器sqlfiddle
DROP PROCEDURE IF EXISTS check_commission_recurring_based_on_percental/
CREATE PROCEDURE check_commission_recurring_based_on_percental(IN percental ENUM('yes','no'), IN recurring ENUM('yes','no'))
BEGIN
IF recurring = 'yes' AND percental != 'yes' THEN
signal sqlstate '45000' set message_text = "percental must be 'yes' for recurring to be 'yes'";
END IF;
END/
CREATE
TRIGGER `before_insert` BEFORE INSERT
ON `commission`
FOR EACH ROW BEGIN
CALL check_commission_recurring_based_on_percental(NEW.percental,NEW.recurring);
END/
CREATE
TRIGGER `before_update` BEFORE UPDATE
ON `commission`
FOR EACH ROW BEGIN
CALL check_commission_recurring_based_on_percental(NEW.percental,NEW.recurring);
END/