What are constraints in PostgreSQL?

Answer

Constraints enforce rules on data to maintain integrity. Types: NOT NULL (column cannot be NULL), UNIQUE (all values in column must be distinct; NULLs are not considered duplicates — multiple NULLs allowed), PRIMARY KEY (NOT NULL + UNIQUE — identifies each row), FOREIGN KEY (enforces referential integrity to another table), CHECK (arbitrary boolean condition: CHECK (age >= 18), CHECK (price > 0)), EXCLUSION (ensures no two rows conflict based on operators — used for time ranges, e.g., no overlapping reservations). Constraints can be column-level or table-level. Name constraints for clear error messages: CONSTRAINT chk_age CHECK (age >= 0).