MYSQL TRIGGER AFTER DELET - delete data from multiple tables - mysql

I would like to delete data in multiple tables, from a trigger but it seem like my syntax is incorrect.
{ DELIMITER $$
CREATE TRIGGER `delete_store`
AFTER DELETE ON `menufacture`
FOR EACH ROW
BEGIN
DELETE FROM company WHERE OLD.id = menufacture_id ;
DELETE FROM product WHERE OLD.id = product.menufacture_id;
DELETE FROM menufacture WHERE OLD.id = menufacture.parent ;
END $$`
}
#1442- can't update table 'menufacture' in stored function/trigger because it is already used by statement which invoked this stored function /trigger

Related

MariaDB move data to another table with TRIGGER

i want to move my current data to another table when the update value is "jakarta"
IF NEW.alamat = "jakarta" THEN
INSERT INTO detail_mhs_copy1
SELECT * FROM detail_mhs WHERE id_mhs = NEW.id_mhs;
DELETE FROM detail_mhs WHERE id_mhs = OLD.id_mhs;
END IF;
but with this trigger its make error like this
1442 - Can't update table 'detail_mhs' in stored function/trigger because it is already used by statement which invoked this stored function/trigger
any solutions?
Please try below trigger and change col1 & col2 with your column's name and change primary key with column name which have primary key and not updating.
DELIMITER $$
CREATE TRIGGER detail_mhs_trig
AFTER UPDATE ON detail_mhs FOR EACH ROW
BEGIN
IF NEW.alamat = "jakarta" THEN
INSERT INTO detail_mhs_copy1(col1,col2, alamat)
VALUES(old.col1, old.col2, new.alamat);
END IF; END$$
DELIMITER ;

Update Trigger on same table MySQL 5.6

I have a table 'foods' with columns 'featured', 'restaurant_id', 'retaurant_stock'.
How can I update column 'restaurant_stock' using trigger or any other solution available on mySQL version 5.6 after update of column 'featured' on the same table on a condition?
Calculated columns are not available on mySQL version 5.6.
Here's my trigger:
CREATE TRIGGER update_restaurant_stock BEFORE UPDATE ON foods
OR EACH ROW
BEGIN
IF NEW.featured = 1 THEN
UPDATE `foods` SET NEW.restaurant_stock = (NEW.restaurant_id);
ELSE
UPDATE `foods` SET NEW.restaurant_stock = 0;
END IF;
END
Now when I update column 'featured' I get the following error message:
#1442 - Can't update table 'foods' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Thanks for you help!
You can't update the current row that way, you can use set to change the new values
DELIMITER $$
CREATE TRIGGER update_restaurant_stock BEFORE UPDATE ON foods
OR EACH ROW
BEGIN
IF NEW.featured = 1 THEN
SET NEW.restaurant_stock = (NEW.restaurant_id);
ELSE
SET NEW.restaurant_stock = 0;
END IF;
END$$
DELIMITER ;
CREATE TRIGGER tr_bu_foods
BEFORE UPDATE
ON foods
FOR EACH ROW
-- update restaurant_stock
SET NEW.restaurant_stock = NEW.restaurant_id * (NEW.featured = 1);

Trigger After Insert Update -cant update table

i have this Trigger in my MySql:
CREATE DEFINER=`root`#`localhost` TRIGGER `mydatabase`.`content_AFTER_INSERT` AFTER INSERT ON `content` FOR EACH ROW
BEGIN
UPDATE member
SET member.points = member.points +10, member.postcountT = member.postcountT +1
WHERE member.user_id = NEW.owner;
END
and this error comes if i insert data into the "content" table:
Can't update table 'member' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
how can i resolve this? thanks.

MySQL Trigger on 2 tables with

I have 2 tables
FIRST
ps_product (id_product, price)
SECOND
ps_product_price (id_product, price, price_tax)
Now what I trying to do is to SET TRIGGER:
DELIMITER $$
CREATE TRIGGER `after_ps_product_price_update`
BEFORE UPDATE ON `ps_product_price`
FOR EACH ROW
BEGIN
UPDATE ps_product
SET price = NEW.price_tax -(NEW.price_tax * 0.1666666666666666666)
WHERE id_product = NEW.id_product;
END
When ps_rpoduct_price TABLE, COLUMN(price_tax) is updated then update TABLE ps_product, COLUMN (price)
And that works fine but when I try do do this same other way around:
DELIMITER $$
CREATE TRIGGER `after_ps_product_update`
AFTER UPDATE ON `ps_product`
FOR EACH ROW
BEGIN
UPDATE ps_product_price
SET price_tax = (NEW.price * 1.2)
WHERE id_product = NEW.id_product;
END
Then I get an error:
Can't update table 'ps_rpoduct' in stored function/trigger because it is already used by statement which invoked this stored function/trigger

#1442 - Can't update table 'sale_price' in stored function/trigger because it is already used by statement

I am trying to update the same table before inserting new row in the table.
I want to set the status of all previous rows with the same product_id to 0 and after that insert new row with the status 1....Please help. here is my code written mysql..
DROP TRIGGER IF EXISTS `priceStatusUpdate`//
CREATE TRIGGER `priceStatusUpdate` BEFORE INSERT ON `sale_price`
FOR EACH ROW BEGIN
UPDATE sale_price
SET status=0
WHERE product_id=new.product_id;
END
//
Use a stored procedure instead:
DELIMITER $$
CREATE PROCEDURE procedureName (IN p_product_id)
BEGIN
UPDATE sale_price SET status = 0 WHERE product_id = p_product_id;
INSERT INTO sale_price (product_id) VALUES (p_product_id);
END $$
DELIMITER ;
Then call it with
CALL procedureName(1);
DROP TRIGGER IF EXISTS `priceStatusUpdate`//
CREATE TRIGGER `priceStatusUpdate` BEFORE INSERT ON `sale_price`
FOR EACH ROW BEGIN
SET NEW.status= 0;
END