liveBook · Manning

Not-null constraint

As explained in Grokking Relational Database Design

A NOT NULL constraint requires a column to contain a non-NULL value.

A NOT NULL constraint requires a column to contain a value. It prevents missing or unknown values from entering places where the database needs reliable information. section 6.10 section 1.10

A product table

In a product table, name, description, price, and manufacturer can be declared NOT NULL. Every inserted product must then provide all four values. Without the constraint, a missing price could make calculations produce NULL because arithmetic involving an unknown value remains unknown. section 1.10

CREATE TABLE product (
 product_id INT PRIMARY KEY,
 name TEXT NOT NULL,
 description TEXT NOT NULL,
 price DECIMAL(5, 2) NOT NULL,
 manufacturer TEXT NOT NULL
);

This CREATE TABLE definition shows NOT NULL applied alongside data types and a primary key. section 1.10

Apply NOT NULL when a column must always have information:

  • Mark critical operational data such as usernames, email addresses, passwords, names, or login times. section 6.10
  • Use it for legally or compliantly required values, such as date of birth in an option-trading application. section 6.10
  • Consider it for foreign keys when the relationship requires every row to reference another table. section 6.10
  • Do not repeat NOT NULL for a primary key: PRIMARY KEY already implies and enforces it. section 6.10

Choose the constraint from the data requirements. Review each attribute, identify information that cannot be missing, and add NOT NULL in the column definition. section 8.11

How Grokking Relational Database Design covers it

How deep it goes
Defines the constraint, explains NULL-related problems, and demonstrates column syntax without covering database-engine internals.
Examples
Demonstrates product columns, user-account fields, legal data, and foreign-key relationships as candidates for NOT NULL.
What it assumes
Assumes basic SQL tables and columns while defining NULL, data types, and constraint selection in practical terms.
What you can do after
Apply NOT NULL by identifying required attributes and declaring the constraint in CREATE TABLE statements.
How it connects
Connects NOT NULL with NULL values, primary keys, foreign keys, unique constraints, and requirement analysis.

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 Grokking Relational Database Design. The section links point to the parts of the book it draws on.