Trigger Performance Considerations
📖 Scenario: You are managing a small online store database. You want to use triggers to automatically update stock quantities when orders are placed. However, you also want to keep your database fast and responsive.
🎯 Goal: Build a simple trigger setup that updates stock quantities after an order is inserted, while considering performance by limiting the trigger's work.
📋 What You'll Learn
Create a table called
products with columns product_id (integer) and stock (integer).Create a table called
orders with columns order_id (integer), product_id (integer), and quantity (integer).Create a trigger function called
update_stock that subtracts the ordered quantity from the product stock.Create a trigger called
after_order_insert that calls update_stock after a new order is inserted.Ensure the trigger only updates stock for the product in the new order to keep performance efficient.
💡 Why This Matters
🌍 Real World
Triggers help automate database tasks like updating stock levels when orders change, saving manual work and reducing errors.
💼 Career
Understanding trigger performance is important for database administrators and developers to keep applications fast and reliable.
Progress0 / 4 steps