SQL Triggers

Execute SQL automatically in response to INSERT, UPDATE, or DELETE events.

A trigger is a special type of stored procedure that automatically runs when an event occurs in the database server. They are commonly used for automated audit trails and logging.

Key Takeaways: SQL Triggers

Mastering SQL Triggers helps you write clean and reliable SQL code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

SQL Triggers Code Example

SQL
CREATE TRIGGER audit_price_update
AFTER UPDATE ON products
FOR EACH ROW
BEGIN
  IF OLD.price <> NEW.price THEN
    INSERT INTO price_audit (product_id, old_price, new_price, changed_at)
    VALUES (NEW.id, OLD.price, NEW.price, NOW());
  END IF;
END;
Output
Automates audit history logging on product price changes
Free sign-in required · Google login available
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services