Using AFTER UPDATE Triggers in MySQL
📖 Scenario: You manage a small online store database. You want to keep track of every time a product's price changes. This helps you analyze price updates over time.
🎯 Goal: Create a MySQL AFTER UPDATE trigger that records old and new prices into a separate price_changes table whenever a product's price is updated.
📋 What You'll Learn
Create a
products table with columns product_id, product_name, and price.Create a
price_changes table with columns change_id, product_id, old_price, new_price, and change_date.Create an
AFTER UPDATE trigger on products that inserts a record into price_changes when the price changes.Ensure the trigger only records changes if the
price is actually updated.💡 Why This Matters
🌍 Real World
Tracking price changes helps businesses analyze pricing strategies and maintain audit trails for product updates.
💼 Career
Database triggers are widely used in real-world applications to automate tasks and enforce business rules without extra application code.
Progress0 / 4 steps