Trigger update same table - mysql

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

Related

Before/after delete trigger

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?

how to run trigger after update table then running statement update in same table

I have table machine and I would like to create trigger, the condition is when table machine updates the trigger will be running script update table machine again where field status_1 = '' OR status_2 = ''
CREATE TRIGGER `update_machine` AFTER UPDATE ON `machine`
FOR EACH ROW
UPDATE `machine`
SET `status_1`='disabled',`status_1`='disabled'
WHERE `status_1`='' OR `status_2`=''
but the error say
1442 - Can't update table 'machine' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
but how can I still run this trigger?
A Trigger can't update another row of the same table.
A typical way to do this is to create a stored procedure that updates your table and then update other rows of the same table.

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

Can I delete a row of a table by a trigger which is included in the table it self?

I want to delete a row of a table through a trigger. And the trigger is written in the same table.Currently I get an error #1442 - Can't update table 'Traffic' in stored function/trigger because it is already used by statement which invoked this stored function/trigger

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);