liveBook · Manning

Not-null constraint

As explained in Build Financial Software with Generative AI (From Scratch)

A NOT NULL constraint requires a database column to contain a value rather than SQL NULL.

A NOT NULL constraint requires a database column to contain a value instead of SQL NULL. It protects fields that must exist for a record to be meaningful. section 5.1

In an ACH exceptions table, every exception must identify its file, record number, and exception code, so those columns are declared NOT NULL. Optional record-type references remain nullable because an exception may involve only one record type. section 8.4.1

Use NOT NULL when:

  • A value is required for every row, such as an exception's file identifier or record number. section 8.4.1
  • A field should not be confused with an optional relationship; nullable foreign keys can represent information that applies only in some cases. section 8.4.1

Required columns in an ACH exceptions table

CREATE TABLE ach_exceptions (\n ach_exceptions_id UUID PRIMARY KEY,\n ach_files_id UUID NOT NULL,\n record_number NUMERIC NOT NULL,\n exception_code VARCHAR(3) NOT NULL\n); section 8.4.1

Listing 8.3 Creating the exceptions table
CREATE TABLE ach_exceptions (
    ach_exceptions_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    ach_files_id UUID NOT NULL 
       REFERENCES ach_files(ach_files_id) 
       ON DELETE CASCADE ON UPDATE CASCADE,
    ach_records_type_5_id UUID    #1
       REFERENCES ach_records_type_5(ach_records_type_5_id)  #1
       ON DELETE CASCADE ON UPDATE CASCADE,  #1
    ach_records_type_6_id UUID REFERENCES     #1
       ach_records_type_6(ach_records_type_6_id)   #1
       ON DELETE CASCADE ON UPDATE CASCADE,  #1
    record_number NUMERIC NOT NULL, #1
    exception_code VARCHAR(3) NOT NULL REFERENCES  #2
    ach_exception_codes(exception_code)              #2
                                ON UPDATE CASCADE    #2
);

Inspect the complete exceptions-table definition, including required fields, nullable foreign keys, and cascading relationships. section 8.4.1

How Build Financial Software with Generative AI (From Scratch) covers it

How deep it goes
Defines the constraint and applies it in schemas, but does not descend into database engine internals.
Examples
Uses ACH file identifiers, record numbers, exception codes, and nullable record-type references in database table definitions.
What it assumes
Assumes basic relational-table concepts while introducing field constraints as part of data-integrity design.
What you can do after
Operate/configure database schemas by marking fields that every stored row must populate.
How it connects
Connects NOT NULL with foreign keys, primary keys, UNIQUE constraints, DEFAULT values, and data integrity.

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 Build Financial Software with Generative AI (From Scratch). The section links point to the parts of the book it draws on.