Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can we have multiple triggers on a single table in mysql

Tags:

mysql

triggers

i have written a before insert trigger and after insert trigger on a single table in MySQL. But the one of the trigger automatically gets replaced when i put the other trigger.if i put after insert trigger , the before insert trigger code automatically gets replaced. Note it gets deleted or you can the the code gets deleted.

Both of them separately work fine. Please help me on this.

like image 998
user2320499 Avatar asked Apr 26 '13 04:04

user2320499


People also ask

Can we have multiple triggers in the same table?

You can create multiple triggers for the same subject table, event, and activation time. The order in which those triggers are activated is the order in which the triggers were created.

How many triggers are possible per table in MySQL?

MySQL triggers fire depending on the activation time and the event for a total of six unique trigger combinations.

How many triggers are possible for a table?

A single SQL statement can potentially fire up to four types of triggers: BEFORE row triggers, BEFORE statement triggers, AFTER row triggers, and AFTER statement triggers.

Can we have multiple triggers in the same table in SQL Server?

SQL Server allows multiple triggers on the table for the same event and there is no defined order of execution of these triggers. We can set the order of a trigger to either first or last using procedure sp_settriggerorder. There can be only one first or last trigger for each statement on a table.


3 Answers

Before MySQL 5.7.2

You can have only one trigger per table and trigger event and action time. For example it's possible to have

  • one BEFORE UPDATE and one AFTER UPDATE or
  • one BEFORE UPDATE and one BEFORE INSERT

trigger on a table. But you can have triggers that execute multiple statements by using BEGIN ... END though. See MySQL 5.6 Documentation for more information.

With MySQL 5.7.2

There are no restrictions about the number of triggers per table any more. It's moreover possible to define the order of trigger processing with the PRECEDES and FOLLOWS keywords. See MySQL 5.7 Documentation for more information.

like image 192
steffen Avatar answered Sep 30 '22 02:09

steffen


Note: The first half of this answer applies to MySQL prior to v5.7.2. See the answer below from @steffen for v5.7.2 and higher. The second half is still valid: the OP was using the same name when trying to implement multiple triggers.


You can have a BEFORE INSERT trigger and an AFTER INSERT trigger. It's in the documentation. You can only have one of each - for example, you can't have two (or three or four) BEFORE INSERT triggers.

If your AFTER INSERT trigger is wiping out your BEFORE INSERT trigger then the two triggers probably have the same name. Make sure each has a unique name.

like image 41
Ed Gibbs Avatar answered Sep 30 '22 01:09

Ed Gibbs


From the MySQL Version 8.0 Documentation

It is possible to define multiple triggers for a given table that have the same trigger event and action time. For example, you can have two BEFORE UPDATE triggers for a table. By default, triggers that have the same trigger event and action time activate in the order they were created. To affect trigger order, specify a trigger_order clause that indicates FOLLOWS or PRECEDES and the name of an existing trigger that also has the same trigger event and action time. With FOLLOWS, the new trigger activates after the existing trigger. With PRECEDES, the new trigger activates before the existing trigger.

like image 42
Zhang Avatar answered Sep 30 '22 02:09

Zhang