Can I define multiple events in one Trigger declaration in mysql? - mysql

Here is my requirement,
I have a mysql table on which any change (insert/delete/update) should be handled in exactly same way. According to mysql documentation create trigger syntax is as follows:
CREATE
[DEFINER = { user | CURRENT_USER }]
TRIGGER trigger_name
trigger_time **trigger_event**
ON tbl_name FOR EACH ROW
trigger_body
When I'm trying to put more than one event, its throwing syntax error.
One solution is I can write one procedure and 3 triggers (one for each event) and call the same procedure from all the triggers.
Is there any sophisticated solution for this ??

No. In MySQL, a trigger is for a single trigger_event. A trigger has to be either BEFORE or AFTER and one of INSERT, UPDATE, DELETE.
If we have lots of logic that is shared across the trigger events (logic that would need to be duplicated in multiple triggers), we can write/create a PROCEDURE to encapsulate that logic, and call that procedure from the body of multiple triggers.

Related

How to Identify MySQL Trigger Operation

In Oracle, when writing a unique trigger that handles INSERT, UPDATE OR DELETE operations, when can detect which operation is being executed using this technique, called 'Conditional Predicates'
create trigger sample_trigger
before insert or update
on sample_table
for each row
begin
case
when inserting then
--do something
when updating then
--do something
end case;
end;
Does MySQL 5.6 provides any technique that allows me to do the same? I wouldn't like to write three distinct triggers only to differentiate which operating is being executed. Thanks!
No. In MySQL (even in 8.0) triggers can only be called for one type of operation, INSERT, UPDATE or DELETE. From the manual, the syntax for CREATE TRIGGER is:
CREATE
[DEFINER = { user | CURRENT_USER }]
TRIGGER trigger_name
trigger_time trigger_event
...
and trigger_event may only be one of INSERT, UPDATE or DELETE:
trigger_event: { INSERT | UPDATE | DELETE }
So given that there is no scope to call a trigger for different operations, there is no need to have the conditional predicates you describe.
What you could do instead in put your trigger code into a stored procedure, and then call that with a parameter which specifies whether the type of operation e.g. for an INSERT trigger you might use
CREATE TRIGGER db_insert
BEFORE INSERT ON db
FOR EACH ROW
BEGIN
CALL db_trigger_proc('INSERT');
END
Unfortunately you can't.
You can see that the documentation states that you can use only one event to fire the trigger.
Syntax:
trigger_event: { INSERT | UPDATE | DELETE }
that means you can choose only one event as a trigger_event.
another argument on the same doc :
There cannot be multiple triggers for a given table that have the same trigger event and action time. For example, you cannot have two BEFORE UPDATE triggers for a table. But you can have a BEFORE UPDATE and a BEFORE INSERT trigger, or a BEFORE UPDATE and an AFTER UPDATE trigger.

How to use multiple events in one sql trigger?

This is my code at the moment:
DROP TRIGGER `backup`;
DELIMITER $$
CREATE TRIGGER `backup` AFTER INSERT UPDATE DELETE
ON `warehouse`
FOR EACH ROW
BEGIN
END$$
DELIMITER ;
This is the error I keep getting:
I checked my version of MariaDB. It's 10.1.21
It works if I use only one event, but with two or three it throws this error.
Insert Update trigger how to determine if insert or update
In MySQL or MariaDB, each trigger must be defined for exactly one event. You cannot define a trigger that will work for multiple events.
https://dev.mysql.com/doc/refman/5.7/en/create-trigger.html has the syntax:
trigger_event: { INSERT | UPDATE | DELETE }
This syntax notation means the event must be one of the three values INSERT, UPDATE, or DELETE.
Another clue is found if we DESCRIBE INFORMATION_SCHEMA.TRIGGERS:
EVENT_MANIPULATION enum('INSERT','UPDATE','DELETE')
The event type is an enum which means it can only have one value, not multiple.
The example you linked to is for Microsoft SQL Server, not MySQL or MariaDB.
Despite the fact that both "Microsoft" and "MySQL" start with a similar syllable, these are two different products, with different features.

Create MySQL trigger before update any table

I would like to create a trigger that intercepts any update on any table in the database, is there a way to NOT specify the table for which the trigger is being made so it can intercept all updates?
Something like:
create trigger interceptor
after update on ANY_TABLE
....
This is not possible, you have to create individual triggers per table.
However, as the comment above tells, every trigger can be calling the same stored procedure: Need an abstract trigger in MySQL 5.1 to update an audit log
You may go even further by creating a procedure that loops through all tables in your DB using information_schema, dropping and then (re)creating triggers automatically, then call this script every time you update your DB structure.

how to create trigger on any change in table

I have a mysql table which on any change (i.e. insert, update and delete) I need to run the relevant trigger code.
Do I need to create three different triggers or is there a syntax for just one.
Using mysql 5.1
Three triggers may perform better and AFAIK - there is no possibility to create multi-action trigger in MySQL, but I hope the syntax for one trigger is:
CREATE TRIGGER Name AFTER INSERT ON Table
FOR EACH ROW
begin
...
END

MySQL trigger which triggers on either INSERT or UPDATE?

Is there a way to create MySQL trigger which triggers on either UPDATE or INSERT?
Something like
CREATE TRIGGER t_apps_affected BEFORE INSERT OR UPDATE ...
Obviously, the above don't work. So, any workarounds without creating two separate triggers?
I need this in order to update running counter on another table.
Unfortunately, there is no shorthand form - you must create multiple triggers - one for each event.
The doc says:
trigger_event indicates the kind of statement that activates the trigger. The trigger_event can be one of the following:
INSERT: The trigger is activated whenever a new row is inserted into
the table; for example, through INSERT, LOAD DATA, and REPLACE
statements.
UPDATE: The trigger is activated whenever a row is modified; for
example, through UPDATE statements.
DELETE: The trigger is activated whenever a row is deleted from the
table; for example, through DELETE and REPLACE statements. However,
DROP TABLE and TRUNCATE TABLE statements on the table do not activate
this trigger, because they do not use DELETE. Dropping a partition
does not activate DELETE triggers, either. See Section 12.1.27,
“TRUNCATE TABLE Syntax”.
While it is impossible to put a trigger on multiple events, you can define the two triggers to merely call another stored procedure and, with that, cut down on the amount of code you need to commit. Just create the separate triggers to do nothing but, say,
CALL update_counter();
and put all of your actual work into the procedure. The triggers would then be a simple
CREATE TRIGGER t_apps_affected BEFORE INSERT ON table
FOR EACH ROW
BEGIN
CALL update_counter();
END;