Using DELETE Triggers in MySQL
📖 Scenario: You manage a small online store database. You want to keep track of every product that gets deleted from the products table by saving its details in a separate deleted_products table.
🎯 Goal: Create a DELETE trigger in MySQL that automatically copies the deleted product's information into the deleted_products table whenever a product is removed from the products table.
📋 What You'll Learn
Create a
products table with columns product_id (INT), product_name (VARCHAR(50)), and price (DECIMAL(10,2))Create a
deleted_products table with the same columns as products plus a deleted_at column (DATETIME)Create a DELETE trigger named
after_product_delete on the products tableThe trigger should insert the deleted row's data into
deleted_products with the current timestamp in deleted_at💡 Why This Matters
🌍 Real World
DELETE triggers help keep audit logs or backups automatically when data is removed, which is important for data safety and tracking changes in real-world databases.
💼 Career
Understanding triggers is useful for database administrators and backend developers who maintain data integrity and automate database tasks.
Progress0 / 4 steps