简体   繁体   English

使用引用不同表的触发器

[英]Using triggers referring to different tables

I am trying to use a trigger so that it denies user entry if Boolean value from another table is unticked. 我正在尝试使用触发器,以便如果未选中来自另一个表的布尔值时拒绝用户输入。 How can I do this 我怎样才能做到这一点

TABLE A

IF 如果

TABLE B attribute1 = 0 then, don't allow insert

TABLE B attribute1 = 1 then, allow insert

Sorry for the vauge description or zero code but I have no idea how to go about doing this 很抱歉输入的内容过多或代码为零,但我不知道该怎么做

This should give you an starting point. 这应该给您一个起点。 Adjust table names and conditions according to your schema. 根据您的架构调整表名称和条件。

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 ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM