Requiring a Present Column Value

A not-null constraint requires a column value to be present. It does not by itself require a nonempty string or a value in a particular interval.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a required title
  2. Check user-facing validation too
  3. Choose a field whose absence prevents a meaningful row
  4. Verify existing data before tightening the rule

Trace a required title

An omitted title may use a default if one applies. A supplied null fails a not-null requirement. In PostgreSQL an empty string is a present value and needs another rule if it is unacceptable. See SQL absence and emptiness.

Check user-facing validation too

The storage constraint keeps unsupported writes from committing. A form can explain the requirement before saving. Form validation gives users an earlier repair path.[1][2]

Choose a field whose absence prevents a meaningful row

A delivery row requiring a destination uses a presence constraint on the destination field. Inserting null fails under that constraint. If empty text is also invalid, a separate rule must enforce it for text types that distinguish empty from null. See null versus empty and zero.

destination = null → rejected by NOT NULL
destination = "" → present string, may need another check
destination = "A7" → passes presence check

Verify existing data before tightening the rule

Older rows may contain unknown destinations. Decide which can be backfilled from evidence or should retain an alternate explicit state. Test migration with representative older rows.

References

  1. PostgreSQL Global Development Group, Data Definition ↩
  2. PostgreSQL Global Development Group, SQL Language Tutorial ↩