Triggers help check data automatically when you add or change it. This keeps your data correct and clean.
When you want to make sure a new entry meets rules before saving.
When you want to check data changes and stop wrong updates.
When you want to log or fix data automatically after changes.
When you want to enforce complex rules that normal constraints can't handle.
Syntax
PostgreSQL
CREATE FUNCTION function_name() RETURNS trigger AS $$
BEGIN
-- validation logic here
IF (condition fails) THEN
RAISE EXCEPTION 'Error message';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER trigger_name
BEFORE INSERTORUPDATEON table_name
FOR EACH ROW EXECUTE FUNCTION function_name();
The function runs before or after data changes depending on trigger timing.
Use RAISE EXCEPTION to stop wrong data from being saved.
Examples
This trigger checks that the age is not negative before saving a user.
PostgreSQL
CREATE FUNCTION check_age() RETURNS trigger AS $$
BEGIN
IF NEW.age < 0THEN
RAISE EXCEPTION 'Age cannot be negative';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER validate_age
BEFORE INSERTORUPDATEON users
FOR EACH ROW EXECUTE FUNCTION check_age();
This trigger ensures the email has a basic valid format before saving.
PostgreSQL
CREATE FUNCTION check_email() RETURNS trigger AS $$
BEGIN
IF NEW.email NOTLIKE'%@%.%'THEN
RAISE EXCEPTION 'Invalid email format';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER validate_email
BEFORE INSERTORUPDATEON contacts
FOR EACH ROW EXECUTE FUNCTION check_email();
Sample Program
This example creates a table for employees and a trigger to check that salary is not negative. The first insert works, the second fails with an error.
PostgreSQL
CREATETABLE employees (
id SERIALPRIMARYKEY,
name TEXTNOTNULL,
salary NUMERIC NOTNULL
);
CREATE FUNCTION check_salary() RETURNS trigger AS $$
BEGIN
IF NEW.salary < 0THEN
RAISE EXCEPTION 'Salary cannot be negative';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER validate_salary
BEFORE INSERTORUPDATEON employees
FOR EACH ROW EXECUTE FUNCTION check_salary();
-- Try inserting a valid rowINSERTINTO employees (name, salary) VALUES ('Alice', 5000);
-- Try inserting an invalid rowINSERTINTO employees (name, salary) VALUES ('Bob', -1000);
OutputSuccess
Important Notes
Triggers run automatically and can stop bad data with errors.
Use BEFORE triggers to check or change data before saving.
Always test triggers carefully to avoid blocking valid data.
Summary
Triggers help check data automatically when inserting or updating.
Use RAISE EXCEPTION inside trigger functions to stop wrong data.
Triggers run BEFORE or AFTER data changes depending on need.
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
Using AFTER trigger to fix duplicates (too late)
Setting default instead of raising error
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.