MySql After Update Trigger execution order - mysql

I am calling an API of my Nodejs app to update a record in my MySQL database.
I defined an "After Update trigger" on it. the trigger calls a post restful API using sys_exec,to pass the updated record's ID to another API. Then, the other API fetch the record and based on the updated values, will insert a new record in the other table of the same database.
But what actually happens is: first the second API insert new record based on the old values of the record and then the old value update new value.
As far as I know, "after update trigger" guarantees to start executing trigger after updating current record.
any suggestion or help, please.

The after update trigger runs after the record is updated, but before the committing of the transaction.
By calling another api from the trigger, the 2nd insert is most likely runs in a different transaction. Unless you change the isolation level to read uncommitted, the 2nd transaction can only read the committed, therefore unchanged values of the record.
I would do the 2nd insertion from the trigger, not from another api because the trigger can obviously see the updated values. The 2nd api can still take care of whatever else it is doing at the moment.
I would not recommend changing the isolation level to read uncommitted - unless you really know what you are doing. It can have unintended side effects.

Related

Read Committed Data when in transaction in MySQL/MariaDB

I am trying to capture record history wherein i pass the old and new data to a php function and it returns an array of changed values.
So, is there a way to get the previous committed data from DB using same session before committing the current transaction?
Plan A:
BEGIN;
SELECT ... FOR UPDATE;
...
COMMIT;
Plan B:
See if you can use a TRIGGER, which has access to the old and new values as the pseudo tables OLD and NEW.
(If you would like to discuss your goal more; we might be able to provide a more focused Answer.)

Query related to trigger fire in SQL database?

Is it possible to fire a trigger when a row value in the table equals a certain value? Like if a value in the table hits zero, I want a certain DML operation to occur using a trigger, is this possible?
Triggers fire based on events on DB objects. For example you can define a trigger to fire before or after insert, update or delete events on tables. What you do in the trigger implementation is entirely up to you, DB will make old record (for update and delete) and new record (for insert and update) available to you.
For your requirement, you can implement a logic that will check for the attribute's value and if it hits zero, you perform the DML operation else just return without doing anything.

How a trigger on a table works on insert event?

Hypothetically, I am going to develop a trigger that inserts a record to Table A when an insertion made to an Table A.
Therefore, I want to know how the system handles that kind of loophole or it is going to continue as a loop until the system hangs which requires restart and possibly remove the DB.
I'm trying to gather information on almost every DBMS on this issue or loophole.
I can only speak to Oracle, I know nothing of MySQL.
In Oracle, this situation is known as mutation. Oracle will not spiral into an endless loop. It will detect the condition, and raise an ORA-04091 error.
That is:
ORA-04091: table XXXX is mutating, trigger/function may not see it
The standard solution is to define a package with three functions and a package level array. The three functions are as follows:
initialize - this will only zero out the array.
save_row - this will save the id of the current row (uk or pk) into the arrray.
process_rows - this will go through the array, and actually do the trigger action for each row.
Now, define some trigger actions:
statement level BEFORE: call initialize
row level BEFORE or AFTER: call save_row
statement level AFTER: call process_rows
In this way, Oracle can avoid mutation, and your trigger will work.
More details and some sample code can be found here:
https://asktom.oracle.com/pls/asktom/ASKTOM.download_file?p_file=6551198119097816936
You can only insert a record in same table if you are using instead of trigger. In all other cases you can only modify the record being inserted.
I hope this answers your quest.
you can create trigger in mysql DBMS.
check below link for create insert trigger syntex
http://www.techonthenet.com/oracle/triggers/after_insert.php

Mysql trigger inserts persist after original transaction is rolled back

I have implemented a typical "audit log" trigger in Mysql version 5.5. I use AFTER INSERT to copy inserted rows from table user into my 'audit_log' table.
So for this sequence:
BEGIN;
insert into user (name) values ('joe');
<--trigger fires, adds new row to audit_log table-->
COMMIT;
Then I get a new row in 'audit_log' with 'joe'. Great.
However, it appears that the results of my trigger are applied even if the insert which fired the trigger is aborted by its enclosing transaction.
So for this sequence:
BEGIN;
insert into user (name) values ('mary');
<--trigger fires, adds new row to audit_log table-->
ROLLBACK;
I STILL end up with a new row 'mary' in audit_log, which refers to data that was never committed to my user table. This seems pretty clearly wrong.
It appears that trigger updates performed in Postgres execute within the original transaction, which is what I would expect to happen. Anyone have experience with this in MySQL? Perhaps there is a setting I have wrong?
I note that this question implies that all updates happen in the original transaction. However, the answer refers to the Mysql manual page on triggers, which in fact has no mention of "transation" at all.
Turns out my audit table was using the MyISAM engine which of course prevented it from obeying the transaction properly.
My colleague says I owe Larry Ellison an apology.
Larry, I'm sorry I doubted the transactional semantics of Mysql.

MySQL Trigger with transaction insert or update

i'm using mysql db & java, so in my application i need to use trigger, but i need to make it save while inserting or updating.
when insering new record in table A, trigger will do the work to insert some information in another table B.
also when doing update for records in table A, trigger will update records for table B.
if transaction during update or insrt rolledback, does this rolled back any changes done by the trigger.??!!
Yes, if the transaction is rolled back, the work done by the trigger will also be rolled back (unless you do some monkeying with transaction scoping inside the trigger to specifically prevent this)