Create a trigger to trigger whenever a field is edited mysql

1

Good colleagues, I have a problem that I still can not solve. I want to create a trigger that whenever the column of quantity of product undergoes a update it triggers an action for another table, to allow the user to know that a certain product has increased or decreased. Is it possible in mysql? If so, what would be the logic?

Table that has been updated in the quantity field

'producto', 'CREATE TABLE 'producto' (\n  
idProducto' int(11) NOT NULL AUTO_INCREMENT,\n 
 'artigo' int(11) NOT NULL,\n  'data' datetime NOT NULL,\n 
 'tipoProducto' varchar(50) NOT NULL,\n 
 'descricao' mediumtext NOT NULL,\n  
'quantidade' int(11) NOT NULL,\n  
'validade' date DEFAULT NULL,\n 
 PRIMARY KEY ('idProducto')\n) ENGINE=InnoDB DEFAULT CHARSET=utf8'

Table where the action will be triggered

''entrada_producto', 'CREATE TABLE 'entrada_producto' (\n  
'idEntrada' int(11) NOT NULL AUTO_INCREMENT,\n 
 'idProducto' int(11) DEFAULT NULL,\n 
 'dataEntrada' datetime DEFAULT NULL,\n 
 'quantidade_entrada' int(11) DEFAULT NULL,\n 
 'descricao' mediumtext,\n
 PRIMARY KEY ('idEntrada'),\n  KEY 'idProducto' ('idProducto'),\n  CONSTRAINT 'entrada_producto_ibfk_1' FOREIGN KEY ('idProducto') REFERENCES 'producto' ('idproducto')\n) ENGINE=InnoDB DEFAULT CHARSET=utf8''
    
asked by anonymous 31.07.2018 / 12:12

1 answer

1

You can do a trigger as below:

DELIMITER $$
CREATE TRIGGER produto_update
AFTER UPDATE
   ON producto FOR EACH ROW
BEGIN

if (NEW.quantidade <> OLD.quantidade)
THEN

/*  insert modulos (descricao) values ('Teste');  Aqui você faz o que quiser, coloquei uma tabela qualquer só para mostrar */


END IF;
END $$;
DELIMITER ;
    
31.07.2018 / 13:15