Assertions & Triggers
Some rules span tables or need procedural logic. Assertions and triggers let the database enforce behavior that a simple constraint cannot.
In plain English
An assertion is a house rule; a trigger is an alarm that rings when something changes.
The one idea
An assertion states an invariant that must always hold; a trigger reacts to an event and runs procedural code.
Some rules span several rows or tables and cannot be expressed as a simple CHECK. Triggers can enforce them, but they also hide behavior from the application.
By the end
- 01Explain what an assertion checks.
- 02Distinguish before and after triggers.
- 03Know row-level versus statement-level triggers.
- 04Recognize the risks of trigger logic.
Assertions
An assertion is a condition that the database must always satisfy. It can reference multiple tables and is checked when relevant changes occur. Assertions are declarative and powerful, but support varies across DBMSs and complex assertions can be expensive to check.
CREATE ASSERTION valid_enrollment
CHECK (
NOT EXISTS (
SELECT * FROM Enroll
WHERE grade IS NOT NULL
AND student_id NOT IN (SELECT id FROM Student)
)
);Trigger events and timing
A trigger fires on INSERT, UPDATE, or DELETE. A BEFORE trigger can validate or modify the incoming row; an AFTER trigger can propagate changes or write an audit record. A row-level trigger fires once per affected row; a statement-level trigger fires once per statement.
CREATE TRIGGER audit_update
AFTER UPDATE ON Student
FOR EACH ROW
BEGIN
INSERT INTO Audit(student_id, changed_at)
VALUES (OLD.id, CURRENT_TIMESTAMP);
END;Risks and best practices
Triggers hide logic in the database, can fire recursively, and can make bulk operations slow or unpredictable. Keep them small, document them, and avoid using them for business workflows that belong in the application. Assertions are better for declarative invariants; triggers are better for auditing and tightly coupled integrity rules.
Do not oversell a course
Course has capacity 30. Enrollment rows are inserted one at a time.
Try a CHECK constraint on Course.
It cannot see the count of enrollment rows, so it cannot express the rule alone.
Create a BEFORE INSERT trigger on Enrollment.
The trigger counts current enrollments and rejects the insert when the course is full.
Two inserts race through the count.
Without locking or a serializable transaction, both may see 29 and both may insert.
ConclusionA trigger is not automatically atomic. Cross-row invariants need concurrency control as well as trigger logic.
The trade-off
No free correctness.
Gain
Complex, cross-table rules enforced centrally.
Cost
Hidden control flow, cascading triggers, portability problems, and harder debugging.
Verdict
Use constraints where possible; use triggers for auditing or carefully scoped cross-row rules.
Recap
What to keep.
Takeaways
- +Assertions declare global conditions that must hold.
- +Triggers run procedural code on data changes.
- +BEFORE triggers can validate or modify; AFTER triggers can propagate or audit.
- +Row-level triggers fire per row; statement-level triggers fire per statement.
- +Triggers can hide complexity and cause recursive side effects.
Traps
- !Using triggers for complex business workflows.
- !Forgetting that a trigger may fire during a bulk load.
- !Assuming every DBMS supports assertions identically.
Focus
- →Write a small AFTER trigger for auditing.
- →Explain the difference between BEFORE and AFTER.
- →Know when an assertion is preferable to a trigger.
Check yourself
Before you turn the page.
Which trigger timing can modify the incoming row before it is stored?
What is an assertion?