Trigger To Update Stock in MYSQL

2

How about, I'm doing a tool lending system and I need to update the stock of products every time a product comes in or out of the warehouse, I have not used triggers in mySQL and this is being a bit complicated.

This is the Trigger that I have written, the trigger will be triggered every time a new record is inserted in the "INCLUDE" table updated "STOCK" in the "PRODUCT" table, but every time this happens it shows an error of that the column has not been found.

DELIMITER $$
USE 'DemoDos'$$
CREATE DEFINER = CURRENT_USER TRIGGER 'DemoDos'.'restarStock' AFTER INSERT 
ON 'INCLUYE' FOR EACH ROW
BEGIN
Update 'PRODUCTO' set 'STOCK' = 'STOCK' - 'New.CANTIDAD_ENTREGADA'
where 'ID_PRODUCTO' = 'ID_PRODUCTO';
END$$

These are the tables in question.

I hope you can help me and thank you in advance!

    
asked by Aarón Zúñiga 10.10.2017 в 15:39
source

2 answers

1

I have already solved the problem, I add the correct trigger code:

CREATE DEFINER = CURRENT_USER TRIGGER DemoDos.restarStock 
AFTER INSERT ON INCLUYE FOR EACH ROW
Update PRODUCTO
set PRODUCTO.STOCK = PRODUCTO.STOCK - NEW.CANTIDAD_ENTREGADA
where PRODUCTO.ID_PRODUCTO = NEW.ID_PRODUCTO;
    
answered by 10.10.2017 / 17:29
source
-1
CREATE DEFINER = CURRENT_USER TRIGGER DemoDos.restarStock 
AFTER INSERT ON INCLUYE FOR EACH ROW
Update PRODUCTO
set PRODUCTO.STOCK = PRODUCTO.STOCK - NEW.CANTIDAD_ENTREGADA
where PRODUCTO.ID_PRODUCTO = ID_PRODUCTO;

CORRECTED REMOVES THE NEW FROM THE LAST LINE

    
answered by 02.11.2018 в 05:34