Loading Depth Study
Preparing your workspace
Constraints enforce rules on stored data so invalid or inconsistent records are less likely to enter the database.
Common constraints include NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, and sometimes default values. These rules move important integrity checks closer to the data itself. NULL represents the absence or unknown state of a value and is not the same as an empty string, zero, or false. Clear data rules make applications more reliable.
A user email may need to be unique, a course title may be required, and an enrollment may require a valid user_id and course_id.
CREATE TABLE users ( id uuid PRIMARY KEY, email text NOT NULL UNIQUE, age int CHECK (age >= 0), bio text );
The database enforces required, unique, and valid values while allowing bio to remain optional.