Triggers

Triggers automatically execute a specified action in response to certain events on a table, such as INSERT, UPDATE, or DELETE.


CREATE TRIGGER update_inventory
AFTER INSERT ON sales
FOR EACH ROW
BEGIN
    UPDATE inventory SET quantity = quantity - NEW.quantity_sold WHERE product_id = NEW.product_id;
END;