The snippet library.
Small, practical examples. Search by task or narrow things down below.
Apply a default when an insert omits a column
SQL Server: A default supplies an omitted value; an explicit NULL still follows the column’s nullability rules.
SQL Server✓ Tested
Block deletion of a referenced parent
SQL Server: The default foreign-key action prevents deletion while a child still refers to the parent.
SQL Server✓ Tested
Enforce a bounded numeric range
SQL Server: A named CHECK constraint keeps numeric values inside an inclusive interval.
SQL Server✓ Tested
Keep children while clearing a deleted relationship
SQL Server: ON DELETE SET NULL requires a nullable foreign-key column.
SQL Server✓ Tested
Make a pair of columns unique together
SQL Server: A composite primary key permits repeated individual values while forbidding a duplicate combination.
SQL Server✓ Tested
Reference a parent identified by two columns
SQL Server: A composite foreign key checks the complete referenced key rather than either column independently.
SQL Server✓ Tested
Reject a negative quantity with CHECK
SQL Server: Combine NOT NULL and CHECK because an unknown NULL check result is normally accepted.
SQL Server✓ Tested
Reject duplicate business identifiers
SQL Server: A UNIQUE constraint protects a business key independently of the primary key.
SQL Server✓ Tested
Remove dependent rows with a declared cascade
SQL Server: ON DELETE CASCADE removes children when their referenced parent is deleted.
SQL Server✓ Tested
Require a value with NOT NULL
SQL Server: Declare required fields at the database boundary, not only in application validation.
SQL Server✓ Tested