Create an UPDATE Trigger Using OLD and NEW
📖 Scenario: You manage a small library database. You want to keep track of every time a book's price changes. To do this, you will create a trigger that records the old price and the new price whenever an update happens.
🎯 Goal: Build an UPDATE trigger on the books table that uses OLD and NEW to log price changes into a price_changes table.
📋 What You'll Learn
Create a
books table with columns id, title, and price.Create a
price_changes table with columns book_id, old_price, and new_price.Create an UPDATE trigger on
books that inserts a record into price_changes whenever the price changes.Use
OLD.price and NEW.price inside the trigger to get the old and new prices.💡 Why This Matters
🌍 Real World
Tracking changes in data is important for audit logs, history, and debugging in real-world databases.
💼 Career
Database developers and administrators often write triggers to automate tasks and maintain data integrity.
Progress0 / 4 steps