Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a trigger in PostgreSQL?
A trigger is a special procedure that automatically runs when certain events happen in the database, like inserting or updating data.
Click to reveal answer
beginner
How can triggers help with data validation?
Triggers can check data before it is saved and stop the action if the data does not meet rules, ensuring only valid data is stored.
Click to reveal answer
intermediate
What is the difference between BEFORE and AFTER triggers?
BEFORE triggers run before the data change happens, so they can prevent invalid data. AFTER triggers run after the change, useful for logging or actions that don't block data.
Click to reveal answer
intermediate
Write a simple trigger function in PostgreSQL that checks if a new user's age is at least 18.
CREATE 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;
Click to reveal answer
intermediate
How do you attach a trigger function to a table for INSERT events?
Use CREATE TRIGGER with the table name, specify BEFORE INSERT, and link the trigger function. Example: CREATE TRIGGER validate_age BEFORE INSERT ON users FOR EACH ROW EXECUTE FUNCTION check_age();
Click to reveal answer
What event would you use a BEFORE trigger for?
ATo create a new table
BTo log data after it is inserted
CTo delete data automatically
DTo check data before it is inserted or updated
✗ Incorrect
BEFORE triggers run before data changes, so they are perfect for checking or validating data.
Which language is commonly used to write PostgreSQL trigger functions?
APL/pgSQL
BPython
CJavaScript
DHTML
✗ Incorrect
PL/pgSQL is the built-in procedural language for writing trigger functions in PostgreSQL.
What happens if a trigger raises an exception during data validation?
AThe data change continues anyway
BThe data change is stopped and an error is shown
CThe database restarts
DThe trigger is ignored
✗ Incorrect
Raising an exception in a trigger stops the data change and returns an error to the user.
Which statement correctly creates a trigger for validating data on INSERT?
ACREATE TRIGGER validate BEFORE UPDATE ON table EXECUTE FUNCTION validate_func();
BCREATE TRIGGER validate AFTER DELETE ON table EXECUTE FUNCTION validate_func();
CCREATE TRIGGER validate BEFORE INSERT ON table FOR EACH ROW EXECUTE FUNCTION validate_func();
DCREATE TRIGGER validate ON table FOR EACH STATEMENT EXECUTE FUNCTION validate_func();
✗ Incorrect
The correct syntax includes BEFORE INSERT, FOR EACH ROW, and EXECUTE FUNCTION.
Triggers are useful for:
AAutomatically enforcing rules on data changes
BManually editing data in tables
CBacking up the database
DCreating user accounts
✗ Incorrect
Triggers automatically run code to enforce rules or actions when data changes.
Explain how a trigger can be used to validate data before inserting it into a table.
Think about the timing of the trigger and what happens if data is wrong.
You got /4 concepts.
Describe the steps to create and attach a trigger function for data validation in PostgreSQL.
Consider both the function and the trigger creation commands.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a trigger in PostgreSQL for data validation?
easy
A. To create new tables based on existing ones
B. To speed up query execution by indexing data
C. To automatically check and enforce rules on data before it is saved
D. To backup the database automatically
Solution
Step 1: Understand trigger role
Triggers run automatically when data changes, allowing checks on data.
Step 2: Identify validation purpose
Data validation means checking data correctness before saving it.
Final Answer:
To automatically check and enforce rules on data before it is saved -> Option C
Quick Check:
Trigger = automatic data check [OK]
Hint: Triggers run automatically to check data before saving [OK]
Common Mistakes:
Thinking triggers speed up queries
Confusing triggers with backups
Assuming triggers create tables
2. Which of the following is the correct way to declare a BEFORE INSERT trigger in PostgreSQL?
easy
A. CREATE TRIGGER trg BEFORE INSERT ON table_name EXECUTE FUNCTION func_name;
B. CREATE TRIGGER trg AFTER INSERT ON table_name EXECUTE PROCEDURE func_name();
C. CREATE TRIGGER trg BEFORE INSERT ON table_name EXECUTE PROCEDURE func_name();
D. CREATE TRIGGER trg BEFORE INSERT ON table_name EXECUTE FUNCTION func_name();
Solution
Step 1: Recall PostgreSQL trigger syntax
PostgreSQL uses EXECUTE FUNCTION for triggers since version 11.
Step 2: Identify correct timing and syntax
BEFORE INSERT triggers run before inserting data; syntax must match.
Final Answer:
CREATE TRIGGER trg BEFORE INSERT ON table_name EXECUTE FUNCTION func_name(); -> Option D
Quick Check:
BEFORE INSERT + EXECUTE FUNCTION = CREATE TRIGGER trg BEFORE INSERT ON table_name EXECUTE FUNCTION func_name(); [OK]
Hint: Use EXECUTE FUNCTION for triggers in PostgreSQL 11+ [OK]
Common Mistakes:
Using EXECUTE PROCEDURE instead of EXECUTE FUNCTION
Confusing BEFORE and AFTER timing
Missing parentheses after function name
3. Given this trigger function to prevent negative prices:
CREATE FUNCTION check_price() RETURNS trigger AS $$ BEGIN IF NEW.price < 0 THEN RAISE EXCEPTION 'Price cannot be negative'; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql;
What happens if you try to insert a row with price = -5?
medium
A. An error is raised and the insert is stopped
B. The row is inserted with price -5
C. The price is automatically set to 0
D. The trigger is ignored and insert proceeds
Solution
Step 1: Analyze trigger function logic
The function checks if NEW.price is less than 0 and raises an exception if true.
Step 2: Understand RAISE EXCEPTION effect
RAISE EXCEPTION stops the operation and returns an error to the user.
Final Answer:
An error is raised and the insert is stopped -> Option A
Quick Check:
Negative price triggers error [OK]
Hint: RAISE EXCEPTION stops insert on invalid data [OK]
Common Mistakes:
Assuming data is inserted anyway
Thinking price auto-corrects
Ignoring trigger effects
4. You wrote this trigger function:
CREATE FUNCTION validate_age() RETURNS trigger AS $$ BEGIN IF NEW.age < 18 THEN RAISE EXCEPTION 'Age must be 18 or older'; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql;
But when inserting age = 15, no error occurs. What is the likely mistake?
medium
A. RAISE EXCEPTION syntax is incorrect
B. The trigger is not attached to the table
C. The function does not return NEW
D. The trigger function is missing LANGUAGE plpgsql
Solution
Step 1: Check function correctness
The function correctly raises exception and returns NEW, syntax is fine.
Step 2: Consider trigger attachment
If no error occurs, likely the trigger is not linked to the table to run the function.
Final Answer:
The trigger is not attached to the table -> Option B
Quick Check:
Trigger must be attached to run function [OK]
Hint: Attach trigger to table to activate validation [OK]
Common Mistakes:
Forgetting to create the trigger after function
Assuming function runs without trigger
Misreading RAISE EXCEPTION syntax
5. You want to ensure that a user's email is unique and not empty using a trigger. Which approach correctly combines data validation and uniqueness check in PostgreSQL?
hard
A. Create a BEFORE INSERT OR UPDATE trigger that raises exception if NEW.email is empty or exists in the table
B. Use a UNIQUE constraint on email column only, no trigger needed
C. Create an AFTER INSERT trigger that deletes duplicates after insertion
D. Use a trigger that sets empty emails to a default value
Solution
Step 1: Understand validation needs
Email must be non-empty and unique before saving data.
Step 2: Choose trigger timing and logic
BEFORE INSERT OR UPDATE trigger can check NEW.email and query table for duplicates, raising exception if invalid.
Final Answer:
Create a BEFORE INSERT OR UPDATE trigger that raises exception if NEW.email is empty or exists in the table -> Option A
Quick Check:
Validate and check uniqueness before insert/update [OK]
Hint: Use BEFORE trigger to check and stop invalid data [OK]
Common Mistakes:
Relying only on UNIQUE constraint without empty check