Challenge - 5 Problems
Trigger Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a trigger function enforcing age validation
Consider a PostgreSQL table
users with a trigger that prevents inserting rows where age is less than 18. What will happen if you run the following insert?INSERT INTO users (id, name, age) VALUES (1, 'Alice', 16);
PostgreSQL
CREATE OR REPLACE FUNCTION check_age() RETURNS trigger AS $$ BEGIN IF NEW.age < 18 THEN RAISE EXCEPTION 'Age must be at least 18'; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql; CREATE TRIGGER age_check BEFORE INSERT ON users FOR EACH ROW EXECUTE FUNCTION check_age();
Attempts:
2 left
💡 Hint
Triggers can raise exceptions to stop invalid data from being inserted.
✗ Incorrect
The trigger function checks the age before insert. If age is less than 18, it raises an exception, which stops the insert and returns an error.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this trigger function
Which option contains the correct syntax for a PostgreSQL trigger function that validates a non-empty
username field before insert?PostgreSQL
CREATE OR REPLACE FUNCTION validate_username() RETURNS trigger AS $$ BEGIN IF NEW.username = '' THEN RAISE EXCEPTION 'Username cannot be empty'; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql;
Attempts:
2 left
💡 Hint
Check the use of 'CREATE OR REPLACE FUNCTION' and the correct comparison operator.
✗ Incorrect
Option D uses the correct syntax: 'CREATE OR REPLACE FUNCTION', single '=' for comparison, and 'RAISE EXCEPTION' to throw errors.
❓ optimization
advanced2:00remaining
Optimizing a trigger for multiple column validations
You want to create a trigger that validates both
email and phone columns on insert or update. Which option is the most efficient way to write the trigger function?Attempts:
2 left
💡 Hint
Minimize the number of triggers for better performance.
✗ Incorrect
Combining validations in a single BEFORE trigger reduces overhead and ensures all validations happen before data is saved.
🔧 Debug
advanced2:00remaining
Debugging a trigger that does not prevent invalid data
A trigger is supposed to prevent inserting rows with
salary less than 0, but invalid rows are still inserted. Which option explains the most likely cause?PostgreSQL
CREATE OR REPLACE FUNCTION check_salary() RETURNS trigger AS $$ BEGIN IF NEW.salary < 0 THEN RAISE EXCEPTION 'Salary cannot be negative'; END IF; RETURN NULL; END; $$ LANGUAGE plpgsql; CREATE TRIGGER salary_check BEFORE INSERT ON employees FOR EACH ROW EXECUTE FUNCTION check_salary();
Attempts:
2 left
💡 Hint
Check what the trigger function returns to control the insert.
✗ Incorrect
Returning NULL in a BEFORE trigger tells PostgreSQL to skip the insert for that row silently, so no error is raised and the row is not inserted. But if the row is still inserted, the trigger might not be firing correctly or the return value is mishandled.
🧠 Conceptual
expert2:00remaining
Understanding trigger timing and data validation
Which statement best describes why a BEFORE trigger is preferred over an AFTER trigger for data validation in PostgreSQL?
Attempts:
2 left
💡 Hint
Think about when the data is checked relative to insertion.
✗ Incorrect
BEFORE triggers execute before the data is saved, so they can check and stop invalid data from being inserted. AFTER triggers run after insertion, so they cannot prevent the insert but can react to it.