mysql two triggers one update and one to delete - mysql

I have a trigger that after my table1 is updated it creates a record in table2. Is there a way after the process is finished to delete that record from table1?
i always end up with the error
Error Code: 1442. Can't update table 'table1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Which makes sense as the updating is still in progress
Thank you

Related

How to delete a row inside trigger?

Is there any way to delete a row inside trigger (on the table which fired the trigger) in MariaDB/MySQL?
I know it's possible to do so in postgres.
Here is the SQL I've tried.
TRIGGER trigger AFTER INSERT ON table FOR EACH ROW BEGIN
DELETE FROM table
WHERE expire_at <= CURRENT_TIMESTAMP;
END
I'm getting this error message:
Code: 1442. Can't update table 'cluster_events' in stored
function/trigger because it is already used by statement which invoked
this stored function/trigger.
Is it possible to solve this some other way?
If you want to periodically purge 'old' data, use an EVENT.
Be sure to turn on the Event Scheduler.

Deleting a record from a table that has been modified by a trigger

I have two tables namely "ab" and "eff" in mysql database.
My requirement is that when there is a status update in "ab",I need to move that particular record to "eff" and delete the old record in "ab".
I have written an after update trigger to successfully move the record from "ab" to "eff".But I am unable to delete the record from "ab".
The error thrown is as follows:Can't update table 'ab' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Please suggest a method by which I can delete the record from "ab".
Thanks in advance!
You can't delete the rows from the same table on which you implement the trigger. Because on insert MySQL locks the table and can't delete the rows because delete needs locking so its a deadlock situation, that's why MySQL would not allow this.
You need to handle this out of the trigger scope by marking with a flag for delete.

Secuencial triggers mysql is already used by statatement

i'm working on a proyect in mysql and i can't made things work between 2 triggers, what i'm trying to do is
1)
Create a trigger that after inserting values ​​in a table update the data of another table, working
2)
Create a trigger that after updating the aforementioned table, delete the row that detonated the first trigger, not working
I searched the documentation, examples, tutorials, goolge and of course in different posts of the site, but I have not found anything that can guide me or help to solve my problem :(
For the moment i have this
1)
CREATE TRIGGER update1 BEFORE INSERT ON table1
FOR EACH ROW
UPDATE table2 SET table2.value = table2.value +
NEW.value WHERE ac_id = NEW.acc_id
2)
CREATE TRIGGER `delete` AFTER UPDATE ON table2
FOR EACH ROW DELETE FROM table1 WHERE table1.acco_id =
NEW.acc_id
Individually both work, but if I make an insert in table1 the following message appears
Can't update table 'table1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
sorry for my bad English, any help will be highly appreciated, thanks in advance
The observed error message:
ERROR 2442 (HY000) Can't update table '%s' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
is expected behavior. The triggers shown in the question violate a documented restriction:
A stored function or trigger cannot modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger.
Reference: https://dev.mysql.com/doc/refman/5.7/en/stored-program-restrictions.html
To focus that on the specific triggers shown in the question, in the context of a trigger fired by an insert into table1, it is not possible to delete rows from table1. There is no workaround to this restriction.
To "solve this problem" means to step back from the current design, and come up with a different design which achieves the objectives.

Can't update table in stored function/trigger because it is already used by statement which invoked this stored function/trigger

In MySQL, I have just made this trigger to set the last modified date of one table if a new record relating to it is inserted into a linking table
create trigger trg_badge after insert on tbl_badge for each row
UPDATE tbl_sub_model SET last_modified_date = NOW()
WHERE sub_model_sk = NEW.sub_model_sk;
When I run a script that fills tbl_sub_model then tbl_badge I get:
Error Code: 1442. Can't update table 'tbl_sub_model' in stored
function/trigger because it is already used by statement which
invoked this stored function/trigger.
Things work fine when I insert using a separate script but just not if both statements are in the same script. is there a way of inserting into both tables with the same .sql script?
Ah... solved it, my insert statement was joining tbl_sub_model to a couple of other ones in order to get the sub_model_sk relating to the correct new records.
Now I know that I'll just create the triggers at the end of the script, no biggie.

mysql After insert trigger on table 1 that insert in table 2 that have an after insrt trigger that updates table 1

I have two tables one called fs_note the other called dumy_fs_note
I created after Insert and after Delete triggers on the fs_table that insert a row with calculated data to the dumy_fs_table that has an after insert trigger that should update a certain row in fs_table with the new values The problem is that now i am having error: #1442 Can't update table 'fs_note' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. why is that?? it is the dumy_fs_note table that is trying to update the fs_table not the same table, Any idea what's wrong?
Thanks in advance
Yeah, you just can't do that. Mysql is this way preventing circular loops and memory issues... It is like updating a cursor being parsed: some engines allow it, others no.