Challenge - 5 Problems
BEFORE Trigger Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of BEFORE INSERT trigger modifying data
Consider a table users with columns
What will be the content of the table after running
id (int) and username (varchar). A BEFORE INSERT trigger changes the username to uppercase before insertion.What will be the content of the table after running
INSERT INTO users (id, username) VALUES (1, 'alice');?SQL
CREATE TABLE users (id INT, username VARCHAR(20)); DELIMITER $$ CREATE TRIGGER before_insert_users BEFORE INSERT ON users FOR EACH ROW BEGIN SET NEW.username = UPPER(NEW.username); END$$ DELIMITER ; INSERT INTO users (id, username) VALUES (1, 'alice'); SELECT * FROM users;
Attempts:
2 left
💡 Hint
BEFORE triggers can modify the data before it is saved.
✗ Incorrect
The BEFORE INSERT trigger changes the username to uppercase before the row is inserted, so the stored username is 'ALICE'.
❓ query_result
intermediate2:00remaining
Effect of BEFORE UPDATE trigger on data
Given a table
What will be the price after executing
products with columns id and price, a BEFORE UPDATE trigger doubles the price if the new price is less than 100.What will be the price after executing
UPDATE products SET price = 50 WHERE id = 1; if the original price was 80?SQL
CREATE TABLE products (id INT, price INT); INSERT INTO products VALUES (1, 80); DELIMITER $$ CREATE TRIGGER before_update_products BEFORE UPDATE ON products FOR EACH ROW BEGIN IF NEW.price < 100 THEN SET NEW.price = NEW.price * 2; END IF; END$$ DELIMITER ; UPDATE products SET price = 50 WHERE id = 1; SELECT price FROM products WHERE id = 1;
Attempts:
2 left
💡 Hint
BEFORE UPDATE triggers can change the new values before saving.
✗ Incorrect
The trigger doubles the new price if it is less than 100, so 50 becomes 100 before update.
📝 Syntax
advanced2:00remaining
Identify the syntax error in BEFORE INSERT trigger
Which option contains a syntax error in this BEFORE INSERT trigger definition?
SQL
CREATE TRIGGER before_insert_test BEFORE INSERT ON test FOR EACH ROW BEGIN SET NEW.value = NEW.value + 1; END;
Attempts:
2 left
💡 Hint
In MySQL, triggers require changing the delimiter to define the body.
✗ Incorrect
Without changing the delimiter, the parser ends the statement early causing syntax error.
🧠 Conceptual
advanced2:00remaining
When does a BEFORE trigger execute?
Choose the correct statement about when a BEFORE trigger executes in relation to the triggering SQL statement.
Attempts:
2 left
💡 Hint
Think about when you want to modify data before it is saved.
✗ Incorrect
BEFORE triggers run before each affected row is changed, allowing modification or validation of data.
🔧 Debug
expert3:00remaining
Why does this BEFORE INSERT trigger cause an infinite loop?
A BEFORE INSERT trigger on table
What is the main reason this causes an infinite loop?
orders tries to insert a row into the same table inside its body.What is the main reason this causes an infinite loop?
SQL
CREATE TRIGGER before_insert_orders BEFORE INSERT ON orders FOR EACH ROW BEGIN INSERT INTO orders (id, amount) VALUES (NEW.id + 1, NEW.amount); END;
Attempts:
2 left
💡 Hint
Think about what happens when the trigger's INSERT runs.
✗ Incorrect
The trigger's INSERT causes the trigger to fire again, creating an infinite recursion.