Reputation: 155
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
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