我正在尝试使用触发器,以便在未选中另一个表中的 bool 值的情况下拒绝用户输入。我怎样才能做到这一点
表A
如果
TABLE B attribute1 = 0 则不允许插入
TABLE B attribute1 = 1 然后,允许插入
对 vauge 描述或零代码感到抱歉,但我不知道如何去做
最佳答案
这应该为您提供一个起点。根据您的架构调整表名称和条件。
delimiter //
CREATE TRIGGER DENY_IF_TRUE
BEFORE INSERT ON [your table] FOR EACH ROW
BEGIN
DECLARE attr BOOLEAN;
-- 'set variable to attribute value
set @attr := (SELECT attribute FROM [your other table] WHERE [some condition] LIMIT 1);
IF @attr = TRUE THEN
-- 'this will make the trigger fail and therefore avoid the insert operation succeed'
CALL non_existent_function();
END IF;
END;
delimiter ;
关于mysql - 使用引用不同表的触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41088468/