mysql – Why doesn't this trigger update?

Question:

To start I will show the structure of my 3 tables

este tabla seria los productos
Producto (
  CODIGO int(11),
  CANTIDAD int(11)
)

this serious header requested

Pedido (
  codigo int(11),
  nombre varchar(50),
  unix varchar(100)
)

and this would be the order detail table

Detalle (
  codigo int(11),
  producto int(11),
  cantidad` int(11)
)

In short, when registering an order, register the header and the detail does it well

My problem is that when deleting an order I need the quantity of the products registered in the order detail to be returned to the Product table, I made a trigger that in theory would do that but I suppose that the code failed because I delete an Order and it does not update the product table

CREATE TRIGGER Devolver_Productos_Prueba AFTER DELETE ON Detalle FOR EACH ROW

Update Producto
set Producto.CANTIDAD = (Producto.CANTIDAD + old.cantidad)
where Producto.CODIGO = old.codigo;

I would like you to help me, thanks in advance for your help

Answer:

In the detail table I see that you put the code , product , and quantity fields, which gives me to understand that the code field is probably the primary key of the detail table. Now look at the trigger in this part: where Product.CODIGO = old .code; Shouldn't it be an old product? that is, you should put it like this: where Product.CODE = old.product;

Scroll to Top