How to avoid circular Trigger dependencies in MySQL

前端 未结 1 1402
北恋
北恋 2020-12-09 13:59

I have a little probleme using Triggers in MySQL.

Suppose we have 2 tables:

  • TableA
  • TableB

And 2 Triggers:

  • Trigger
相关标签:
1条回答
  • 2020-12-09 14:33

    Try to use variable.

    First trigger:

    CREATE TRIGGER trigger1
      BEFORE DELETE
      ON table1
      FOR EACH ROW
    BEGIN
      IF @deleting IS NULL THEN
        SET @deleting = 1;
        DELETE FROM table2 WHERE id = OLD.id;
        SET @deleting = NULL;
      END IF;
    END
    

    Second trigger:

    CREATE TRIGGER trigger2
      BEFORE DELETE
      ON table2
      FOR EACH ROW
    BEGIN
      IF @deleting IS NULL THEN
        SET @deleting = 1;
        DELETE FROM table1 WHERE id = OLD.id;
        SET @deleting = NULL;
      END IF;
    END
    

    And additional AFTER DELETE triggers:

    CREATE TRIGGER trigger3
      AFTER DELETE
      ON table1
      FOR EACH ROW
    BEGIN
      SET @deleting = NULL;
    END
    
    CREATE TRIGGER trigger4
      AFTER DELETE
      ON table2
      FOR EACH ROW
    BEGIN
      SET @deleting = NULL;
    END
    
    0 讨论(0)
提交回复
热议问题