i want make trigger after delete on orders_foods
my logic is: delete from orders_foods where id=;id then update price on orders price = sum (orders_foods.price ) where id=orders_foods.order_id
this my code i try befor but got error #1363 -
Delimiter //
CREATE TRIGGER ubah_tot_harga AFTER DELETE ON orders_foods
FOR EACH ROW
BEGIN
DECLARE harga_1 int default 0;
SELECT SUM(price) into harga_1 fROM `orders_foods` WHERE order_id = new.order_id;
update orders
set price= harga_1
where id=old.order_id;
END
//
delimiter ;
orders table
orders_foods table
Instead of new.order_id, you need to use old.order_id. because after delete there is no new.order_id there is only old.order_id.
Delimiter //
CREATE TRIGGER ubah_tot_harga AFTER DELETE ON orders_foods
FOR EACH ROW
BEGIN
DECLARE harga_1 int default 0;
SELECT SUM(price) into harga_1 fROM `orders_foods` WHERE order_id = old.order_id;
update orders
set price= harga_1
where id=old.order_id;
END
//
delimiter ;
Related
I have a problem with my trigger before insert.
I try to control the row number of my table 'user_search' for one 'id_user' and if this row number > 4, the oldest row should be deleted.
My actual trigger is this :
DELIMITER $$
CREATE TRIGGER before_insert_user_search
BEFORE INSERT
ON user_search FOR EACH ROW
BEGIN
DECLARE sDate DATETIME;
SELECT MIN(date_search) FROM user_search WHERE id_user = NEW.id_user INTO sDate;
IF ((SELECT COUNT(id) FROM user_search WHERE id_user = NEW.id_user) > 4) THEN
BEGIN
DELETE FROM user_search WHERE date_search = sDate;
END;
END IF;
END$$
DELIMITER ;
However, it does not work. Do someone have a solution to help me please.
Thanks.
You can not delete same table row during using INSERT trigger because it holds the lock of table. So try to write a Stored Procedure to insert new record.
Stored Procedure should be something like bellow:
CREATE PROCEDURE insert_user_search(new_id_user INT, ......other variable to inert)
BEGIN
DECLARE #minId INT;
//INSERT using parameter
SET #minId = SELECT MIN(id) FROM user_search WHERE id_user = new_id_user;
IF ((SELECT COUNT(id) FROM user_search WHERE id_user = new_id_user) > 4) THEN
BEGIN
DELETE FROM user_search WHERE id_user = #minId;
END;
END IF;
END;
I have 3 tables named opproduct, opcategory and opproduct_to_category.The opproduct table has an after insert and an after delete trigger. These triggers modify the category table content column, but it gets the category_id trough opproduct_to_category table. The statments of triggers roughly the same. The after update is working well, but after insert doesn't modify the category table.
Thanks for your answers!
DROP TRIGGER IF EXISTS opproduct_AINS;
DELIMITER $$
CREATE TRIGGER `opproduct_AINS` AFTER INSERT ON `opproduct`
FOR EACH ROW BEGIN
DECLARE _category_id INT;
IF new.`status`='1' THEN
SELECT category_id INTO _category_id
FROM opproduct_to_category
WHERE product_id = NEW.product_id;
UPDATE opcategory
SET content = content+1
WHERE category_id=_category_id;
END IF;
END$$
DELIMITER ;
DROP TRIGGER IF EXISTS opproduct_AUPD;
DELIMITER $$
CREATE TRIGGER `opproduct_AUPD` AFTER UPDATE ON `opproduct`
FOR EACH ROW BEGIN
DECLARE _category_id INT;
IF NEW.`status`<> OLD.`status` AND NEW.`status`=1 THEN
SELECT category_id INTO _category_id
FROM opproduct_to_category
WHERE product_id = NEW.product_id;
UPDATE opcategory SET content = content+1
WHERE category_id=_category_id;
ELSEIF NEW.`status`<> OLD.`status` AND NEW.`status`=0 THEN
SELECT category_id INTO _category_id
FROM opproduct_to_category
WHERE product_id = NEW.product_id;
UPDATE opcategory SET content = content-1
WHERE category_id=_category_id;
END IF;
END$$
DELIMITER ;
I have a problem with a MySQL Trigger.
I have 3 tables Customers, Products and Sales.
In Sales I reference customer and product and I want to update the some counts on Products and Customers after a new sale is inserted.
The following trigger fails to update both tables... I cannot figure out what I am doing wrong.
DELIMITER $
CREATE TRIGGER OnSalesInsert AFTER INSERT ON Sales
FOR EACH ROW BEGIN
UPDATE Products SET Products.sold=Products.sold+NEW.amount WHERE Products.id=NEW.product_id;
UPDATE Customers SET Customers.amount=Customers.amount+NEW.amount WHERE Customers.id=NEW.customer_id;
END $
DELIMITER ;
Try this:
DELIMITER $$
CREATE
/*!50017 DEFINER = 'root'#'%' */
TRIGGER `OnSalesInsert` BEFORE INSERT ON `Sales`
FOR EACH ROW BEGIN
UPDATE Products
SET sold = sold + new.amount
WHERE id = new.product_id;
UPDATE Customers
SET amount = amount + new.amount
WHERE id = new.customer_id;
END;
$$
DELIMITER ;
DELIMITER $$
create trigger UpdateAvail after insert on product_details
for each row
Begin
Declare a1 INT;
Declare d1 VARCHAR(1);
Declare d2 VARCHAR(100);
Select count(0) INTO a1 from prod_available where P_Id=new.P_Id;
Select P_Name,P_Brand INTO d1,d2 from product where P_Id=new.P_Id;
IF a1>0 THEN
Update prod_available set P_quantity=P_quantity+new.quantity where P_Id=new.P_Id;
ELSE
insert into prod_available (P_Id,P_Name,P_Brand,P_quantity) values (new.P_Id,d1,d2,new.quantity);
END IF;
END;
$$
DELIMITER ;
I'm not good at triggers in databases
Trigger:
delimiter $$
CREATE
DEFINER=`root`#`localhost`
TRIGGER `bon_apetite`.`orders_insert_update_trigger`
AFTER INSERT ON `bon_apetite`.`orders`
FOR EACH ROW
BEGIN
update budget_tracking set new.monthly_balance = old.monthly_balance + new.total_price
where tracking_userid = new.order_userid;
END
$$
tables:
budget_tracking and orders
columns:
budget_tracking (id,budget_userid,monthly_balance,created_date,modified_date)
orders(order_id,order_userid,total_price,created_date,modified_date)
budget_userid and order_userid are foriegn keys to a primary key in another table.
All I want is when I'm inserting values to orders table a trigger should update the monthly_balance field of budget_tracking table along with created_date and modified_date of both the tables.
Any help will be appreciated!
Try this
BEGIN
update budget_tracking set monthly_balance = monthly_balance + new.total_price
where tracking_userid = new.order_userid;
END
Try this:
DELIMITER $$
CREATE DEFINER=`root`#`localhost` TRIGGER `bon_apetite`.`orders_insert_update_trigger` AFTER INSERT ON `bon_apetite`.`orders` FOR EACH ROW
BEGIN
UPDATE budget_tracking SET monthly_balance = monthly_balance + new.total_price
WHERE tracking_userid = new.order_userid;
END$$
DELIMITER ;
i have a table orderlines on insert trigger is working but in ordernumber table i want to update ordernumber based on serialnumber
so for updates i have written two lines
line 1: UPDATE TEMPSTORAGE SETORDERNUMBER=orderno+1,SERIALNUMBER=serialno;
is working fine without where condition
line 2: UPDATE ORDERNUMBER SETORDERNUMBER=orderno+1 WHERESERIALNUMBER= serialno;
is not working can any body suggest me.... how to update ordernumber based on
serial number
DELIMITER $$
USE `temp`$$
DROP TRIGGER /*!50032 IF EXISTS */ `trg_updateordernumber`$$
CREATE TRIGGER `trg_updateordernumber` AFTER INSERT ON `ORDERLINES`
FOR EACH ROW BEGIN
DECLARE orderno VARCHAR(255);
DECLARE serialno VARCHAR(255);
SET #orderno = 'xxx';
SELECT ORDERNUMBER, SERIALNUMBER INTO orderno, serialno
FROM ORDERLINES
WHERE ID=NEW.ID;
UPDATE TEMPSTORAGE SET
`ORDERNUMBER` =orderno+1,
`SERIALNUMBER` =serialno;
UPDATE ORDERNUMBER SET
`ORDERNUMBER`=orderno+1
WHERE `SERIALNUMBER` = serialno;
END;
$$
DELIMITER ;
is there any problem with mysql triggers (hope not)....?
if not then plz suggest me why line2 is not working ?
Thanks and regards;
You require a BEFORE INSERT trigger but not AFTER INSERT.
I am not sure what the relation between ORDERLINES and ORDERNUMBER tables,
but suggest to try the following ( not tested ):
DELIMITER $$
USE `temp`$$
DROP TRIGGER /*!50032 IF EXISTS */ `trg_updateordernumber`$$
CREATE TRIGGER `trg_updateordernumber` BEFORE INSERT ON `ORDERLINES`
FOR EACH ROW BEGIN
UPDATE TEMPSTORAGE SET
`ORDERNUMBER` =NEW.ORDERNUMBER+1,
`SERIALNUMBER` =NEW.SERIALNUMBER;
UPDATE ORDERNUMBER SET
`ORDERNUMBER`==NEW.ORDERNUMBER+1
WHERE `SERIALNUMBER` = NEW.SERIALNUMBER;
END;
$$
DELIMITER ;