This example creates two tables: products and price_changes. The trigger after_product_update runs after a product's price changes and records the old and new prices with the time. Then we insert a product and update its price to see the trigger in action.
CREATE TABLE products (
id INT PRIMARY KEY,
name VARCHAR(50),
price DECIMAL(10,2)
);
CREATE TABLE price_changes (
product_id INT,
old_price DECIMAL(10,2),
new_price DECIMAL(10,2),
changed_at DATETIME
);
DELIMITER $$
CREATE TRIGGER after_product_update
AFTER UPDATE ON products
FOR EACH ROW
BEGIN
INSERT INTO price_changes(product_id, old_price, new_price, changed_at)
VALUES (OLD.id, OLD.price, NEW.price, NOW());
END$$
DELIMITER ;
INSERT INTO products VALUES (1, 'Pen', 1.00);
UPDATE products SET price = 1.20 WHERE id = 1;
SELECT * FROM price_changes;