Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we have single trigger for multiple tables in MySQL

Tags:

mysql

triggers

Can I have a single trigger for multiple tables in MySQL? I have to perform same task after inserting either of the table_1 or table_2 e.g :

CREATE TRIGGER trigger-1_4_task1
   AFTER INSERT ON `table_1`
   FOR EACH ROW
   BEGIN
    .....task1
   END //

CREATE TRIGGER trigger-2_4_task1
   AFTER INSERT ON `table_2`
   FOR EACH ROW
   BEGIN
     .... same task as task1
   END //

Can I combine two above trigger like:

CREATE TRIGGER trigger_4_task1
   AFTER INSERT ON `table_1` OR `table_2`
   FOR EACH ROW
     BEGIN
      ..... task1
     END//

Thanks

like image 539
krishna Prasad Avatar asked Jul 25 '14 07:07

krishna Prasad


People also ask

Can we use one trigger for multiple tables?

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.

How many tables can a trigger associated to in MySQL?

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

Can a trigger update multiple tables?

Actually, you can't update multiple tables in one statement. After UPDATE operation, a trigger on that table fires up. Secondly, when AFTER UPDATE trigger fires up, SQL Server stores records in two special tables: inserted and deleted. The inserted table stores copies of the affected rows after INSERT and UPDATE.

What is limitation of trigger?

Only one INSTEAD OF trigger is allowed for each event type (INSERT, UPDATE, or DELETE) per table. If a table has a BEFORE trigger, it cannot have an INSTEAD OF trigger. The reverse is also true. If a table has an INSTEAD OF X trigger, AFTER X triggers defined for that table will not fire (where X is an event type).


1 Answers

Can I have a single trigger for multiple tables in MySQL?

No.

But multiple triggers could invoke the same stored procedure.

like image 123
RandomSeed Avatar answered Sep 28 '22 05:09

RandomSeed