FULL_STACK_DEVELOPER_2026_COMPLETE - Phase 07 Data Types, NULL and Constraints
VOLUME 03 - DATABASE ENGINEERING
Data Types, NULL and Constraints
How PostgreSQL represents values and rejects invalid persistent states
Persistence becomes understandable when the database stops being magic.
Application SQL / ORM PostgreSQL
NestJS business logic database commands durable relational state
Phase 07 focus
understand the database before Prisma
Figure - Phase 07 isolates PostgreSQL fundamentals so later ORM code has a clear relational meaning.
Book 2 of Phase 07
FULL_STACK_DEVELOPER_2026_COMPLETE - Phase 07 Data Types, NULL and Constraints
Data types are part of the contract
Choosing a column type tells PostgreSQL what values can be represented and what operations make sense. Good types reduce
ambiguity and make invalid states harder to store.
Application value Useful PostgreSQL type Why
Task title / description text Variable-length text without an arbitrary storage limit.
Completed flag boolean Represents true/false directly.
Priority number smallint / integer Numeric ordering; pair with CHECK for allowed range.
Money numeric(p,s) Exact decimal arithmetic when a monetary amount is
truly needed.
Created timestamp timestamptz Timestamp with time-zone semantics normalized by
PostgreSQL.
Identifier bigint identity or uuid Choice depends on system boundaries and identity
strategy.
Structured flexible metadata jsonb Useful when structure is genuinely variable; not a
substitute for normal columns.
Prefer modern identity columns for generated numeric IDs
Generated identity example
CREATE TABLE practice_tasks (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
title text NOT NULL
);
PostgreSQL supports identity columns as the SQL-standard way to generate values. The course uses them for fundamentals.
Later, TaskFlow may use UUIDs where that better matches authentication or distributed identity requirements.
NULL means missing/unknown, not empty
NULL is not the same as an empty string, zero or false. Comparisons involving NULL require special reasoning. Use IS NULL
and IS NOT NULL, not = NULL.
Correct NULL check
SELECT id, title
FROM tasks
WHERE due_at IS NULL;
Three-valued logic
SQL conditions can evaluate to TRUE, FALSE or UNKNOWN. A comparison such as due_at = NULL does not mean what
JavaScript developers often expect.
FULL_STACK_DEVELOPER_2026_COMPLETE - Phase 07 Data Types, NULL and Constraints
Constraints encode durable rules
Incoming row
candidate data
Type checks
is the value compatible?
NOT NULL / CHECK
does the value satisfy local rules?
UNIQUE / PRIMARY KEY
does identity remain valid?
FOREIGN KEY
does the referenced row exist?
Accepted row
database invariant preserved
Figure - Database constraints reject invalid states before they become persistent application data.
Constraint Protects
NOT NULL A required value cannot be absent.
CHECK A value or combination of values must satisfy an expression.
UNIQUE No two rows may share the constrained value combination.
PRIMARY KEY The table has one chosen unique non-null row identity.
FOREIGN KEY A relationship must point to a valid referenced row (unless nullable by design).
A small constraint-rich table
CREATE TABLE practice_tasks (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
title text NOT NULL,
completed boolean NOT NULL DEFAULT false,
priority smallint NOT NULL DEFAULT 2,
due_at timestamptz,
CONSTRAINT practice_tasks_priority_check
CHECK (priority BETWEEN 1 AND 3)
);
Defaults are convenience, constraints are rules
A default supplies a value when an INSERT omits that column. It does not make arbitrary incoming values valid. A CHECK or
other constraint is what rejects invalid persisted values. Keep those responsibilities separate.
Do not use JSON for everything
PostgreSQL has excellent jsonb support, but a flexible JSON document is not automatically the best model for strongly related
business data. If the application routinely filters, joins or constrains a field, a normal typed column is usually easier to validate,
query and index. JSON is useful when the domain is genuinely document-like or extensible.