Home > Back-end >  How can I cancel insertion inside the trigger?
How can I cancel insertion inside the trigger?

Time:05-31

enter image description here

Inside the History table, The COL1 shouldn't be like COL2, If was equal then cancel the insertion.

How can I do it?

CodePudding user response:

For MySQL 5.7 use

CREATE TRIGGER prevent_self_referencing
BEFORE INSERT
ON tablename
FOR EACH ROW
BEGIN
    IF NEW.column1 = NEW.column2 THEN
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Self-referencing not allowed.';
    END IF;
END

And the same trigger for BEFORE UPDATE.

In shown window insert only 3 code lines, the whole IF statement.

  • Related