📖 Scenario: You manage an online store database. You want to keep track of every new order placed by customers by automatically recording details into a separate log table.
🎯 Goal: Build an INSERT trigger on the orders table that automatically inserts a record into the order_log table whenever a new order is added.
📋 What You'll Learn
Create a table called orders with columns order_id (integer), customer_name (varchar), and order_total (decimal).
Create a table called order_log with columns log_id (integer, auto-increment), order_id (integer), and log_message (varchar).
Create an INSERT trigger named log_new_order on the orders table.
The trigger should insert a log entry into order_log with the order_id and a message like 'New order added'.
💡 Why This Matters
🌍 Real World
Automatically logging changes in a database helps keep track of important events without manual effort.
💼 Career
Database triggers are commonly used in real jobs to enforce business rules and audit data changes.
Progress0 / 4 steps
1
Create the orders table
Create a table called orders with columns order_id as INTEGER, customer_name as VARCHAR(100), and order_total as DECIMAL(10,2).
SQL
Hint
Use CREATE TABLE orders and define the columns with their types exactly as specified.
2
Create the order_log table
Create a table called order_log with columns log_id as INTEGER AUTO_INCREMENT PRIMARY KEY, order_id as INTEGER, and log_message as VARCHAR(255).
SQL
Hint
Remember to set log_id as the primary key and auto-increment it.
3
Create the INSERT trigger log_new_order
Create an INSERT trigger named log_new_order on the orders table that runs AFTER insert.
SQL
Hint
Use NEW.order_id to get the inserted order's ID inside the trigger.
4
Complete the trigger with proper delimiter handling
Add the delimiter commands before and after the trigger creation to allow the trigger body to be parsed correctly.
SQL
Hint
Use DELIMITER $$ before and DELIMITER ; after the trigger to handle the semicolons inside the trigger body.
Practice
(1/5)
1. What is the main purpose of an INSERT trigger in a database?
easy
A. To update existing rows in a table
B. To automatically run code when a new row is added to a table
C. To delete rows from a table
D. To create a new table
Solution
Step 1: Understand what triggers do
Triggers are special procedures that run automatically in response to certain events in the database.
Step 2: Focus on INSERT triggers
An INSERT trigger specifically runs when a new row is added to a table.
Final Answer:
To automatically run code when a new row is added to a table -> Option B
Quick Check:
INSERT trigger = runs on new row insert [OK]
Hint: INSERT triggers run on new rows added [OK]
Common Mistakes:
Confusing INSERT triggers with UPDATE or DELETE triggers
Thinking triggers create or drop tables
Believing triggers run manually
2. Which of the following is the correct syntax to create an INSERT trigger in SQL?
easy
A. CREATE TRIGGER trg AFTER INSERT ON table_name FOR EACH ROW BEGIN ... END;
B. CREATE TRIGGER trg BEFORE DELETE ON table_name FOR EACH ROW BEGIN ... END;
C. CREATE TRIGGER trg AFTER UPDATE ON table_name FOR EACH ROW BEGIN ... END;
D. CREATE TRIGGER trg ON table_name INSERT BEGIN ... END;
Solution
Step 1: Identify correct event and timing
For an INSERT trigger, the event is INSERT and timing can be BEFORE or AFTER. CREATE TRIGGER trg AFTER INSERT ON table_name FOR EACH ROW BEGIN ... END; uses AFTER INSERT which is valid.
Step 2: Check syntax structure
CREATE TRIGGER trg AFTER INSERT ON table_name FOR EACH ROW BEGIN ... END; uses correct syntax: CREATE TRIGGER name AFTER INSERT ON table FOR EACH ROW BEGIN ... END;
Final Answer:
CREATE TRIGGER trg AFTER INSERT ON table_name FOR EACH ROW BEGIN ... END; -> Option A
Quick Check:
Correct INSERT trigger syntax = CREATE TRIGGER trg AFTER INSERT ON table_name FOR EACH ROW BEGIN ... END; [OK]
Hint: INSERT triggers use AFTER or BEFORE INSERT ON table [OK]
Common Mistakes:
Using DELETE or UPDATE instead of INSERT
Wrong order of keywords
Missing FOR EACH ROW clause
3. Given this trigger code:
CREATE TRIGGER trg_log AFTER INSERT ON employees FOR EACH ROW BEGIN INSERT INTO log_table VALUES (NEW.id, NEW.name); END;
What happens when a new employee row is inserted?
medium
A. A new row is added to log_table with the inserted employee's id and name
B. The employees table is deleted
C. The trigger causes a syntax error
D. Nothing happens because NEW is invalid
Solution
Step 1: Understand the trigger action
The trigger runs AFTER a new row is inserted into employees. It inserts a row into log_table using NEW.id and NEW.name.
Step 2: Confirm NEW usage
NEW refers to the newly inserted row's values, so the insert into log_table uses correct values.
Final Answer:
A new row is added to log_table with the inserted employee's id and name -> Option A
Quick Check:
NEW accesses inserted row data [OK]
Hint: NEW accesses inserted row values inside INSERT triggers [OK]
Common Mistakes:
Thinking NEW is invalid outside triggers
Assuming trigger deletes data
Confusing AFTER and BEFORE timing
4. Identify the error in this trigger:
CREATE TRIGGER trg_check BEFORE INSERT ON orders FOR EACH ROW BEGIN IF NEW.quantity < 0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Quantity cannot be negative'; END IF; END;
medium
A. Trigger must be AFTER INSERT, not BEFORE INSERT
B. NEW cannot be used in BEFORE INSERT triggers
C. SIGNAL SQLSTATE is invalid syntax
D. Missing delimiter to separate statements inside BEGIN...END
Solution
Step 1: Check trigger syntax for multiple statements
Inside BEGIN...END, multiple statements need delimiters (like semicolons) or the delimiter must be changed before creating the trigger.
Step 2: Confirm other parts are valid
NEW can be used in BEFORE INSERT triggers, SIGNAL SQLSTATE is valid in many SQL dialects, and BEFORE INSERT is allowed.
Final Answer:
Missing delimiter to separate statements inside BEGIN...END -> Option D
Quick Check:
Multiple statements need delimiters [OK]
Hint: Use delimiters inside BEGIN...END blocks [OK]
Common Mistakes:
Forgetting to change delimiter before trigger creation
Misusing NEW in triggers
Confusing BEFORE and AFTER timing rules
5. You want to create an INSERT trigger that prevents inserting duplicate email addresses into a users table. Which approach is best?
hard
A. Use a trigger to insert the duplicate email into a log table
B. Use an AFTER INSERT trigger to delete the row if email is duplicate
C. Use a BEFORE INSERT trigger to check if NEW.email exists and raise an error if yes
D. Use a trigger to update the existing row with the new email
Solution
Step 1: Understand when to prevent duplicates
To stop duplicates before they happen, use a BEFORE INSERT trigger to check if the email already exists.
Step 2: Why other options fail
AFTER INSERT triggers run after insertion, so deleting duplicates after insert is inefficient and can cause issues. Updating existing rows or logging duplicates does not prevent duplicates.
Final Answer:
Use a BEFORE INSERT trigger to check if NEW.email exists and raise an error if yes -> Option C
Quick Check:
Prevent duplicates before insert with BEFORE INSERT trigger [OK]
Hint: Prevent duplicates before insert with BEFORE INSERT trigger [OK]
Common Mistakes:
Trying to fix duplicates after insertion
Using triggers to update instead of prevent
Ignoring unique constraints and relying only on triggers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.