sql / expert
Snippet
Global Consistency with Schema-Level Assertions
Assertions are powerful schema-level constraints that can validate logic across multiple tables. Unlike triggers, they are declarative and ensure the database never enters an invalid state, regardless of the operation performed.
snippet.sql
1
2
3
4
5
CREATE ASSERTION balance_safetyCHECK (NOT EXISTS (SELECT 1 FROM accountsWHERE type = 'SAVINGS' AND balance < 0));
Breakdown
1
CREATE ASSERTION
Defines a global constraint independent of a single table's lifecycle.
2
CHECK (NOT EXISTS (...))
The logic that must always evaluate to true for every transaction commit.