Create an AFTER INSERT Trigger in MySQL
📖 Scenario: You manage a small online store database. You want to keep track of every new order inserted into the orders table by automatically recording a log entry in a separate order_logs table.
🎯 Goal: Build an AFTER INSERT trigger in MySQL that automatically inserts a log record into order_logs whenever a new order is added to orders.
📋 What You'll Learn
Create a table called
orders with columns order_id (INT, primary key) and customer_name (VARCHAR(100))Create a table called
order_logs with columns log_id (INT, primary key, auto-increment), order_id (INT), and log_message (VARCHAR(255))Create an
AFTER INSERT trigger on orders that inserts a log message into order_logs with the new order_id and a message like 'New order inserted'Use exact table and column names as specified
💡 Why This Matters
🌍 Real World
AFTER INSERT triggers are used in real databases to automate logging, auditing, or cascading actions when new data is added.
💼 Career
Database developers and administrators use triggers to enforce business rules and maintain data integrity automatically.
Progress0 / 4 steps