Paji.R
Paji.R

Reputation: 155

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

Upvotes: 0

Views: 39

Answers (1)

james_bond
james_bond

Reputation: 6908

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 ;

Upvotes: 1

Related Questions