How to Identify MySQL Trigger Operation - mysql

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.

Related

Error Code: 1442. Can't update table 'customer' in stored function/trigger because it is already used by statement which invoked this stored function

I'm writing a query that creates a trigger to soft delete a row in the table customer using the a flag called "IsDelete" when the flag is '0' it is not deleted and when the flag becomes 1 the row has been marked as deleted.
When the query is run the error code 1442c is generated. can anyone explain why??
DELIMITER $$
CREATE TRIGGER SOFT_DELETE_TRIGGER
BEFORE DELETE ON customer
FOR EACH ROW
BEGIN
IF OLD.IsDelete = 0 THEN
CALL cannot_delete_error;
UPDATE customer
SET IsDelete = 1;
END IF;
END
$$
Deleting a row in the table to test the trigger.
DELETE FROM customer
WHERE C_username = 'testuser'
Yes, triggers don't allow you to INSERT/UPDATE/DELETE against the same table that spawned the trigger, because that could run invoke the trigger again, and then that trigger might do another INSERT/UPDATE/DELETE, and so on. You have too high a chance of causing an infinite loop.
I'm afraid MySQL doesn't support an "instead of" trigger like some other brands of SQL database do. You can't make a trigger on DELETE that does an update instead.
You can use SIGNAL to make it throw an error, and that blocks the DELETE, but it doesn't do an UPDATE instead.
To implement soft deletes as you are doing, you'll just have to make the client use UPDATE instead of DELETE.

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.

Can I define multiple events in one Trigger declaration in 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.

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;

how to create database trigger on insert, update and delete operation in mysql with coldfusion?

how to create trigger in mysql in coldfusion at the time of insert, update and delete operation.
Thanks
Yugal
I don't really get the point of this because the whole idea of using triggers is to automatically have the database do something without the need to do it from your server-side language on every insert/update or delete.
So basically it's code you only execute once to create the trigger.
I suppose it's just creating a trigger between your <CFQUERY> tags similar to how you do an insert, update or delete operation
Before the insert
CREATE TRIGGER triggerName BEFORE INSERT ON tableName FOR EACH ROW what_ever_you_want_your_trigger_todo;
After the insert
CREATE TRIGGER triggerName AFTER INSERT ON tableName FOR EACH ROW what_ever_you_want_your_trigger_todo;
http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html
But really I would do this using a mysql-client, set and forget it. Depending on the use of BEFORE or AFTER it will be executed either before or after your insert/update/delete statement on that table.