MySQL:两列之间的条件

MySQL: condition between two columns

我有两列佣金(百分比('yes','no'))和经常性(枚举('yes','no')。我想声明以下内容依赖:

当百分比为 'yes' 时,循环也只能是 'yes'。

是否有可能在 mysql 内管理它?

您可以创建两个触发器来检查这一点,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/