0
0
PostgreSQLquery~10 mins

Trigger for data validation in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Trigger for data validation
Insert/Update/Delete operation
Trigger fires BEFORE/AFTER operation
Trigger function runs
Validation passes
Operation allowed
End
When a data change happens, the trigger runs a function that checks the data. If the data is valid, the change continues; if not, it stops with an error.
Execution Sample
PostgreSQL
CREATE FUNCTION validate_age() RETURNS trigger AS $$
BEGIN
  IF NEW.age < 0 THEN
    RAISE EXCEPTION 'Age cannot be negative';
  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER check_age BEFORE INSERT OR UPDATE ON persons
FOR EACH ROW EXECUTE FUNCTION validate_age();
This trigger checks that the 'age' field is not negative before inserting or updating a row in the 'persons' table.
Execution Table
StepOperationTrigger Fired?Validation CheckResultAction
1Insert person with age=25Yes25 < 0 ?FalseInsert allowed
2Insert person with age=-5Yes-5 < 0 ?TrueError raised, insert aborted
3Update person age to 30Yes30 < 0 ?FalseUpdate allowed
4Update person age to -1Yes-1 < 0 ?TrueError raised, update aborted
5Delete personNoNo validation neededN/ADelete allowed
💡 Operations stop or continue based on validation result; errors abort the operation.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
NEW.ageN/A25-530-1N/A
Validation resultN/AFalseTrueFalseTrueN/A
Operation statusN/AAllowedAbortedAllowedAbortedAllowed
Key Moments - 2 Insights
Why does the trigger stop the insert when age is negative?
Because the validation check in the trigger function returns True for NEW.age < 0, it raises an exception which aborts the operation, as shown in execution_table rows 2 and 4.
Does the trigger run on DELETE operations?
No, the trigger is defined for BEFORE INSERT OR UPDATE only, so it does not run on DELETE, as shown in execution_table row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 2 when inserting age -5?
AInsert allowed
BError raised, insert aborted
CTrigger does not fire
DAge is set to 0 automatically
💡 Hint
Check the 'Result' and 'Action' columns in execution_table row 2.
At which step does the trigger NOT fire?
AStep 1
BStep 3
CStep 5
DStep 4
💡 Hint
Look at the 'Trigger Fired?' column in execution_table.
If the trigger function did not return NEW at the end, what would happen?
AThe operation would fail with an error
BThe operation would still succeed
CThe trigger would run twice
DThe data would be inserted with NULL values
💡 Hint
In PostgreSQL triggers, returning NEW is required to allow the operation to proceed.
Concept Snapshot
Trigger for data validation:
- Define a trigger function that checks data conditions.
- Attach trigger BEFORE INSERT or UPDATE on a table.
- If validation fails, raise an exception to stop operation.
- If validation passes, return NEW to continue.
- Triggers help enforce rules automatically on data changes.
Full Transcript
A trigger for data validation in PostgreSQL runs automatically before data changes like insert or update. It calls a function that checks if the new data meets rules, such as age not being negative. If the data is invalid, the function raises an error and stops the change. If valid, it returns the new data to allow the change. This process helps keep data clean and correct without manual checks.