I Help You
Databases

Database Trigger

A trigger is a block of SQL code that the database runs automatically when a particular event happens on a table, such as an INSERT, UPDATE or DELETE. It can be set to run BEFORE the change, for example to validate or adjust the data, or AFTER it, for example to record the change.

Triggers are used for audit logs, keeping summary columns up to date, enforcing complex rules and setting timestamps. Because they run silently in the background, too many triggers can make a database slow and hard to debug, so many teams keep such logic in application code instead.

Example (MySQL)SQL
CREATE TRIGGER log_price_change
AFTER UPDATE ON products
FOR EACH ROW
INSERT INTO price_history (product_id, old_price, new_price, changed_at)
VALUES (OLD.id, OLD.price, NEW.price, NOW());