How to delete a row inside trigger? - mysql

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.

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.

mysql two triggers one update and one to delete

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

mysql stored procedure glitching

I have to limit table record to 25. after I just delete everything (in the future will modify it to delete just oldest rows)
DELIMITER //
CREATE PROCEDURE p1 () delete FROM tests;//
CREATE TRIGGER trigger1
BEFORE INSERT
ON tests
FOR EACH ROW
BEGIN
SELECT COUNT(*) INTO #cnt FROM tests;
IF #cnt >= 25 THEN
CALL p1();
END IF;
END
//
DELIMITER ;
, but I am getting error:
Can't update table 'tests' in stored function/trigger because it is already used by statement which invoked this stored function/trigger
So I can not add any more fields.
The MySQL trigger FAQ sais that you cannot modify the table that calls the trigger.
But you can set up a cron job, or CREATE EVENT in MySQL that cleans the table at regular intervals. (CREATE EVENT needs the PROCESS privilege, and a running event_scheduler. The event_scheduler is turned off by default: it can be turned on from SQL console, but the MySQL config must be modified to ensure that it starts when MySQL restarts.)
It seems that you can't update or delete from the same table which invoked the trigger.
But you can work around this by creating another table, for example tests2 and instead of inserting into tests just insert into tests2, and have the trigger insert the NEW. values into tests, then you can count from tests and delete from tests like how you want it.
see this sqlFiddle
The problem with this is then tests2 gets filled up with Inserted data So you might have to manually delete from tests2.

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.