MySQL after insert and after trigger update

I have two tables t1 and t2. I created two triggers tr1 after inserting on t1 and tr2 after updating to t2. in both tables i m updates table t2. therefore, it throws an error saying that table t2 is already being updated in another trigger, so it cannot update it again.

Please let me know if anyone has encountered this type of problem and fixed it.

Thanks in advance! MySQL DBA.

+1


a source to share


2 answers


I fixed this issue, there was a problem as I was trying to update and insert into the same table tb1 from two triggers, but Mysql does not allow the same table to be modified. Let's pretend that



thanks

-6


a source


If you want to change the data being updated or inserted, you must use the ADDIT and / or ADD INSERT trigger and then use a new alias (it refers to the row that is being inserted or the row how it will make sure the update is applied) - you can actually change the entered fields. ALID OLD refers to the row before the update is applied, or the row before it is deleted (in triggers that are fired on deletion).

So, you probably need something like this for a tr2 trigger:



DELIMITER $$
    CREATE TRIGGER tr2 BEFORE UPDATE ON t2
    FOR EACH ROW BEGIN
      SET NEW.field1 = some_value_you_want_to_set_it_to;
    END;
$$
DELIMITER ;

      

Because in a trigger, you cannot UPDATE a table that is already being updated, that is correct.

+22


a source







All Articles