Using BEFORE UPDATE Triggers in MySQL
📖 Scenario: You manage a small online store database. You want to keep track of when product prices change by automatically recording the old price before any update.
🎯 Goal: Create a MySQL BEFORE UPDATE trigger that saves the old price of a product into a separate column before the price is updated.
📋 What You'll Learn
Create a table called
products with columns id, name, price, and old_priceAdd a
BEFORE UPDATE trigger named before_price_update on the products tableThe trigger should copy the current
price value into old_price before the update happensTest the trigger by updating a product's price
💡 Why This Matters
🌍 Real World
BEFORE UPDATE triggers help keep track of changes automatically in databases, useful for auditing and history tracking.
💼 Career
Database developers and administrators use triggers to enforce business rules and maintain data integrity without manual intervention.
Progress0 / 4 steps