MySQL - After Insert Trigger that updates two tables? - mysql

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 ;

Related

trigger two according to data

I have three tables and I have one trigger from table D_ISI in table 2 to trigger MAX_ISI value to write to the other table. Can you help me solve the trigger problem?
DELIMITER $$
CREATE TRIGGER `ISI_YUKSEK_ISE` BEFORE INSERT ON `TB1` FOR EACH ROW BEGIN
DECLARE
D_ISI INT ;
SET
D_ISI = NEW.D_ISI ; IF
(SELECT D_ISI = NEW.D_ISI FROM TB1,TB2 WHERE TB2.CIHAZ_ID=TB1.CIHAZ_KODU AND TB1.D_ISI >= TB2.MAX_ISI OR TB1.D_ISI <= TB2.MIN_ISI) THEN
INSERT
INTO
TB3(CIHAZ_KODU,D_ISI)
VALUES(NEW.CIHAZ_KODU,NEW.D_ISI) ;
END IF ; END
$$
DELIMITER ;

Trigger after delete THEN UPDATE mysql

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 ;

MySQL After Insert Trigger with Variable

I've created this trigger, but I have two problems. One it does not actively populate the Person table and two. If I enter more than one row into my Party table I get an error in access, Result of Subquery returns more than one row.
Anybody know my problem(s)?
DELIMITER $$
Create TRIGGER Trigger1
AFTER
INSERT
ON Party
FOR EACH ROW
BEGIN
declare partytypeid int;
declare partyid int;
set #partyid:= (select partyid from party);
set #partytypeid:= (select partytypeid from party);
IF partytypeid = 1
THEN INSERT INTO Person
(PartyId) VALUES (PartyId);
END IF;
END$$
delimiter ;
When you set your variables, you're doing a select against the whole table instead of the inserted record.
DELIMITER $$
Create TRIGGER Trigger1
AFTER INSERT ON Party FOR EACH ROW
BEGIN
declare partytypeid int;
declare partyid int;
set #partyid:= new.partyid;
set #partytypeid:= new.partytypeid;
IF partytypeid = 1 THEN
INSERT INTO Person
(PartyId) VALUES (PartyId);
END IF;
END$$
delimiter ;

Trigger Before Insert MySql

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;

Mysql trigger after insert trigger not working

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 ;