Before/after delete trigger - mysql

I have the following trigger in my DB table:
CREATE TRIGGER `ordernumberdelete` AFTER DELETE ON `classes`
FOR EACH ROW UPDATE classes set ordernumber = ordernumber-1 where ordernumber>old.ordernumber
which basically drops a value in an ordernumber column by one if it is higher than the deleted row's order number - plain and simple.
The call to delete the row is using PHP/ajax/mysqli prepared statements but I am getting the error:
can't update table 'classes' in stored function/trigger because it is already used by statement which invokes this stored function/trigger in ....
Looked all over SO/Google and can't see a solution. I though the trigger was done after the mysqli_stmt_execute call.
Any ideas?

Related

Use Procedure instead of trigger in MySQL

I'm trying to make trigger to update one field in inserted row but even with this answer: https://stackoverflow.com/a/15300941/4018940 i cant make it work.
Every time i get error:
General error: 1442 Can't update table 'people' in stored
function/trigger because it is already used by statement which invoked
this stored function/trigger
Here is my code:
CREATE TRIGGER short_name_trigger
BEFORE INSERT ON `people`
FOR EACH ROW BEGIN
UPDATE `people` SET NEW.short_name = "wohahaha";
END
Is there any chance that stored procedure will make it work?
How to write that procedure?
If you want to change the value being inserted in the new row, you don't need to use UPDATE people. Just set NEW.short_name and this will replace the value that's being inserted.
CREATE TRIGGER short_name_trigger
BEFORE INSERT ON `people`
FOR EACH ROW BEGIN
SET NEW.short_name = "wohahaha";
END

Trigger update same table

I have a table named 'item' which stores the amount and availability of an item along with a couple other things
I'm trying to create a trigger which when the 'item_amount' gets to 0, the 'item_available' sets also to 0 however I'm getting an error with the following trigger:
CREATE TRIGGER itemAvailability
AFTER UPDATE ON item
FOR EACH ROW
UPDATE item
SET NEW.item_available=0
WHERE NEW.item_amount<=0
The error is
#1442 - Can't update table 'item' in stored function/trigger because it is already used by statement which invoked this stored function/trigger

Error when Updating a Table with Trigger

I have a trigger setup that every time a new table is created, it reads the ID (which is in varchar) and converts it to integer and stores it in another column.
However when I run it I encounter the following error:
Can't update table 'products' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
This is my trigger:
CREATE TRIGGER `var_to_int` AFTER INSERT ON `products`
FOR EACH ROW UPDATE `products`
SET `int_id`= CAST(`var_id` AS INT)
WHERE `int_id` IS NULL OR `int_id` = ''
How do I prevent this?
This error is thrown by the PHP script creating a new item in the table.
You can not update the table itself.
Trigger starts when it is UPDATE, and then it has to do UPDATE. An endless loop is formed. Try using an additional table to store temporary data to later update the correct table

How to create a trigger for the table already being used

CREATE TRIGGER abc
after insert ON visadetails
FOR EACH ROW update visadetails SET VisaId=concat(new.Occupation,'-',new.Vid);
insert into visadetails (Occupation,Destination) value("student","rome");
after creating trigger m not able to insert the value, it gives following error message
#1442 - Can't update table 'gallery' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
No need for an update (which btw would have updated all rows in the table with the result of the concat() for the new row because of the missing where clause).
CREATE TRIGGER abc
after insert ON visadetails
FOR EACH ROW
SET new.VisaId=concat(new.Occupation,'-',new.Vid);

Updating joined rows in two tables from an INSERT trigger

I currently have the following MySQL query, which I would like to include in a trigger, rather than as a separate query. The query updates data in two tables which are joinable:
UPDATE `testing_names` INNER JOIN `purchase_names`
ON `testing_names`.`fulldomain` = `purchase_names`.`fulldomain`
SET
`testing_names`.`account_id` = `purchase_names`.`account_id`,
`purchase_names`.`purchase_status` = 1
WHERE `purchase_names`.`purchase_status` = 0
A row is (separately) inserted into testing_names. This row SHOULD have a corresponding entry in purchase_names. After a row is inserted into testing_names, when my UPDATE query is next run, it will update testing_names.account_id and update purchase_names.purchase_status to effectively mark this task as completed.
It makes sense to run this as part of a trigger, but I haven't been able to create a trigger that does the job.
So far, I have successfully created a trigger:
DELIMITER $$
CREATE TRIGGER `my_trigger` AFTER INSERT
ON testing_names
FOR EACH ROW BEGIN
UPDATE `testing_names` INNER JOIN `purchase_names`
ON `testing_names`.`fulldomain` = `purchase_names`.`fulldomain`
SET
`testing_names`.`account_id` = `purchase_names`.`account_id`,
`purchase_names`.`purchase_status` = 1
WHERE `purchase_names`.`purchase_status` = 0;
END$$
DELIMITER ;
but evidently running a new UPDATE query is not allowed, because upon inserting a row, I get an error: #1442 - Can't update table 'testing_names' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Unfortunately, a trigger on a table cannot update the same table.
From MySQL documentation:
Within a stored function or trigger, it is not permitted to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger.
Instead, you might be able to complete your update by having the trigger on testing_names update purchase_names and have another trigger on purchase_names update testing_names.
Try Using a Before INsert Instead