liveBook · Manning

Not-null constraint

As explained in Just Use Postgres!

A NOT NULL constraint requires a column value to be present rather than SQL NULL.

A NOT NULL constraint requires a column to contain a value instead of SQL NULL. It protects data integrity by rejecting writes that omit a required value. section 2.3.1

Require catalog fields

CREATE TABLE products.catalog (\n id SERIAL PRIMARY KEY,\n name VARCHAR(100) NOT NULL,\n description TEXT NOT NULL,\n category TEXT,\n price NUMERIC(10, 2)\n);\n\nThe catalog requires both name and description, while category and price remain nullable. Inspect the result with \\d products.catalog; psql reports name and description as “not null.”

Add the constraint later

ALTER TABLE products.reviews\n ALTER COLUMN review SET NOT NULL,\n ADD CONSTRAINT review_rank_check CHECK (rank BETWEEN 1 AND 5);\n\nDefine required constraints during CREATE TABLE when they are known; use ALTER TABLE when strengthening an existing design. section 2.3.1

How Just Use Postgres! covers it

How deep it goes
Defines the rule, shows column declarations and ALTER TABLE usage, and stops short of internal constraint mechanics.
Examples
Uses product catalog name and description columns, customer review text, and psql schema output.
What it assumes
Assumes basic SQL table knowledge and teaches constraint purpose through table definitions and database-level validation.
What you can do after
Operate/configure PostgreSQL tables by declaring required columns and adding NOT NULL constraints with psql and SQL.
How it connects
Connects NOT NULL with data integrity, primary keys, CHECK constraints, CREATE TABLE, and ALTER TABLE.

Also explained in

Compare how 6 books explain not-null constraint →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Just Use Postgres!. The section links point to the parts of the book it draws on.