Bird
Raised Fist0
MySQLquery~20 mins

Why triggers automate responses in MySQL - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Trigger Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Purpose of Triggers in Databases
Why are triggers used to automate responses in a database?
AThey replace the need for database backups.
BThey manually require users to run scripts after data changes.
CThey automatically execute predefined actions when specific database events occur.
DThey slow down database performance by adding unnecessary steps.
Attempts:
2 left
💡 Hint
Think about what happens automatically when data changes.
❓ query_result
intermediate
2:00remaining
Output of a Trigger on Insert
Given a trigger that logs every new row inserted into a table, what will be the result after inserting a row?
MySQL
CREATE TRIGGER log_insert AFTER INSERT ON employees FOR EACH ROW INSERT INTO audit_log(action, emp_id) VALUES ('insert', NEW.id);

INSERT INTO employees (id, name) VALUES (1, 'Alice');

SELECT * FROM audit_log WHERE emp_id = 1;
ASyntax error due to trigger definition.
BNo rows in audit_log because triggers do not run on insert.
CMultiple rows with emp_id 1 in audit_log.
DOne row with action 'insert' and emp_id 1 in audit_log.
Attempts:
2 left
💡 Hint
Triggers run automatically after the insert event.
📝 Syntax
advanced
2:00remaining
Identify the Syntax Error in Trigger Creation
Which option contains a syntax error when creating a trigger to update a timestamp column on row update?
MySQL
CREATE TRIGGER update_timestamp BEFORE UPDATE ON orders FOR EACH ROW SET NEW.updated_at = NOW();
ACREATE TRIGGER update_timestamp BEFORE UPDATE ON orders FOR EACH ROW BEGIN NEW.updated_at = NOW(); END;
BCREATE TRIGGER update_timestamp BEFORE UPDATE ON orders FOR EACH ROW BEGIN SET NEW.updated_at = NOW(); END;
CCREATE TRIGGER update_timestamp BEFORE UPDATE ON orders FOR EACH ROW SET NEW.updated_at = NOW();
DCREATE TRIGGER update_timestamp BEFORE UPDATE ON orders FOR EACH ROW BEGIN SET updated_at = NOW(); END;
Attempts:
2 left
💡 Hint
Check if the trigger body syntax is correct for MySQL.
❓ optimization
advanced
2:00remaining
Optimizing Trigger Performance
Which practice helps optimize triggers to avoid slowing down database operations?
AKeep trigger logic simple and avoid complex queries inside triggers.
BAdd multiple nested triggers for the same event to handle all logic.
CUse triggers to perform heavy data processing and reporting.
DDisable all triggers to improve performance.
Attempts:
2 left
💡 Hint
Think about what makes database operations slower.
🔧 Debug
expert
3:00remaining
Debugging a Trigger Causing Infinite Loop
A trigger on table sales updates a column in the same table, causing an infinite loop. Which option correctly fixes this issue?
AChange the trigger to AFTER DELETE instead of AFTER UPDATE.
BModify the trigger to check if the update is necessary before running the update statement.
CRemove the trigger entirely to stop the loop.
DAdd a delay inside the trigger to slow down the loop.
Attempts:
2 left
💡 Hint
Prevent the trigger from updating when no change is needed.

Practice

(1/5)
1. What is the main purpose of a trigger in MySQL?
easy
A. To backup the database automatically
B. To create new tables in the database
C. To manually update data by user command
D. To automatically run code when data changes

Solution

  1. Step 1: Understand what triggers do

    Triggers run automatically when certain data changes happen in the database, like insert, update, or delete.
  2. Step 2: Compare options with trigger purpose

    Only To automatically run code when data changes describes automatic code running on data changes, which matches the trigger's role.
  3. Final Answer:

    To automatically run code when data changes -> Option D
  4. Quick Check:

    Trigger = automatic response to data change [OK]
Hint: Triggers run code automatically on data changes [OK]
Common Mistakes:
  • Thinking triggers create tables
  • Confusing triggers with manual commands
  • Assuming triggers backup data
2. Which of the following is the correct way to start creating a trigger in MySQL?
easy
A. CREATE TRIGGER trigger_name BEFORE INSERT ON table_name FOR EACH ROW
B. MAKE TRIGGER trigger_name AFTER DELETE ON table_name
C. START TRIGGER trigger_name ON table_name
D. BEGIN TRIGGER trigger_name BEFORE UPDATE

Solution

  1. Step 1: Recall MySQL trigger syntax

    The correct syntax starts with CREATE TRIGGER, followed by trigger name, timing (BEFORE/AFTER), event (INSERT/UPDATE/DELETE), and table name.
  2. Step 2: Check each option's syntax

    Only CREATE TRIGGER trigger_name BEFORE INSERT ON table_name FOR EACH ROW matches the correct syntax format exactly.
  3. Final Answer:

    CREATE TRIGGER trigger_name BEFORE INSERT ON table_name FOR EACH ROW -> Option A
  4. Quick Check:

    Correct syntax starts with CREATE TRIGGER [OK]
Hint: Trigger syntax starts with CREATE TRIGGER [OK]
Common Mistakes:
  • Using MAKE or START instead of CREATE
  • Missing FOR EACH ROW clause
  • Wrong order of keywords
3. Given this trigger code:
CREATE TRIGGER update_stock AFTER INSERT ON sales FOR EACH ROW BEGIN UPDATE products SET stock = stock - NEW.quantity WHERE product_id = NEW.product_id; END;

What happens when a new sale is inserted?
medium
A. The stock of the sold product decreases by the sold quantity
B. The stock of all products increases
C. The sales table is deleted
D. Nothing happens automatically

Solution

  1. Step 1: Analyze the trigger event and action

    The trigger runs AFTER INSERT on sales table, so after a sale is added, it updates products stock.
  2. Step 2: Understand the update statement

    The update reduces stock by NEW.quantity for the product matching NEW.product_id, meaning stock decreases for sold product.
  3. Final Answer:

    The stock of the sold product decreases by the sold quantity -> Option A
  4. Quick Check:

    AFTER INSERT trigger updates stock correctly [OK]
Hint: AFTER INSERT triggers run after new rows added [OK]
Common Mistakes:
  • Thinking stock increases instead of decreases
  • Assuming trigger deletes tables
  • Believing trigger does nothing automatically
4. This trigger code causes an error:
CREATE TRIGGER wrong_trigger BEFORE UPDATE ON orders BEGIN SET NEW.status = 'processed'; END;

What is the main problem?
medium
A. Cannot assign values to NEW in triggers
B. Missing FOR EACH ROW clause after table name
C. Cannot use BEFORE UPDATE triggers in MySQL
D. Trigger name cannot have underscores

Solution

  1. Step 1: Check trigger syntax requirements

    MySQL requires FOR EACH ROW clause after the table name in triggers that act on rows.
  2. Step 2: Identify missing part in code

    The code misses FOR EACH ROW after ON orders, causing syntax error.
  3. Final Answer:

    Missing FOR EACH ROW clause after table name -> Option B
  4. Quick Check:

    FOR EACH ROW is mandatory in row-level triggers [OK]
Hint: Always include FOR EACH ROW in row triggers [OK]
Common Mistakes:
  • Thinking BEFORE UPDATE triggers are not allowed
  • Believing NEW cannot be assigned
  • Assuming trigger names cannot have underscores
5. You want to ensure that when a user's email is updated, a log entry is automatically created in a separate table. Which trigger setup best automates this?
hard
A. CREATE TRIGGER log_email_change AFTER DELETE ON users FOR EACH ROW BEGIN INSERT INTO email_log(user_id, old_email, new_email) VALUES (NEW.id, OLD.email, NEW.email); END;
B. CREATE TRIGGER log_email_change BEFORE INSERT ON users FOR EACH ROW BEGIN INSERT INTO email_log(user_id, old_email, new_email) VALUES (NEW.id, OLD.email, NEW.email); END;
C. CREATE TRIGGER log_email_change AFTER UPDATE ON users FOR EACH ROW BEGIN INSERT INTO email_log(user_id, old_email, new_email) VALUES (NEW.id, OLD.email, NEW.email); END;
D. CREATE TRIGGER log_email_change BEFORE UPDATE ON users BEGIN INSERT INTO email_log(user_id, old_email, new_email) VALUES (NEW.id, OLD.email, NEW.email); END;

Solution

  1. Step 1: Identify correct trigger timing and event

    We want to log after the email changes, so AFTER UPDATE is correct to capture new and old values.
  2. Step 2: Check trigger syntax and usage of OLD and NEW

    CREATE TRIGGER log_email_change AFTER UPDATE ON users FOR EACH ROW BEGIN INSERT INTO email_log(user_id, old_email, new_email) VALUES (NEW.id, OLD.email, NEW.email); END; uses FOR EACH ROW and correctly inserts old and new email values into log table after update.
  3. Final Answer:

    CREATE TRIGGER log_email_change AFTER UPDATE ON users FOR EACH ROW BEGIN INSERT INTO email_log(user_id, old_email, new_email) VALUES (NEW.id, OLD.email, NEW.email); END; -> Option C
  4. Quick Check:

    AFTER UPDATE with OLD and NEW logs changes [OK]
Hint: Use AFTER UPDATE with OLD and NEW for logging changes [OK]
Common Mistakes:
  • Using BEFORE INSERT instead of AFTER UPDATE
  • Missing FOR EACH ROW clause
  • Using AFTER DELETE when updating data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard