NOT NULL Constraints
A `NOT NULL` constraint prevents a database column from containing SQL `NULL`. It is a basic data-integrity rule that ensures a required value is present for every row.
A `NOT NULL` constraint is defined on a table column to require that column to have a value. An `INSERT` or `UPDATE` that attempts to assign SQL `NULL` to the column is rejected by the database.
`NULL` means that a value is absent or unknown; it is different from an empty string such as `''`, the number `0`, or `false`. A `NOT NULL` column therefore does not guarantee that the value is meaningful, only that it is not SQL `NULL`.
You can declare the constraint when creating a table, for example `name VARCHAR(100) NOT NULL`. You can also add or remove a `NOT NULL` requirement with database-specific `ALTER TABLE` syntax, subject to existing data and the database system's rules.
For Java backend applications, `NOT NULL` belongs to the database's integrity layer. Java validation such as Bean Validation can provide earlier and friendlier error messages, but it should not be treated as a replacement for the database constraint when the field is genuinely required.
Key points
- `NOT NULL` prevents SQL `NULL` in a column.
- `NULL` is different from `0`, `false`, and an empty string.
- The database rejects inserts or updates that violate the constraint.
- `NOT NULL` enforces presence, not semantic validity.
- Application-level validation can complement, but should not replace, database integrity constraints.
Interview questions
What does a NOT NULL constraint do?
A `NOT NULL` constraint requires a column to contain a non-`NULL` value for every row. The database rejects an insert or update that tries to store SQL `NULL` in that column. It is used to enforce required data at the database level.
Is NOT NULL enough to guarantee that a field contains valid data?
No. `NOT NULL` only guarantees that the value is not SQL `NULL`. It does not check whether a string has the expected format, whether a number is within a valid range, or whether a value has meaningful business semantics. Those requirements can be handled with constraints such as `CHECK` and with application validation.
Why should a backend application use NOT NULL if Java already validates the field?
Application validation improves the user-facing error path, but it does not protect the database from every possible writer. Other services, scripts, migrations, administrative tools, or bugs can bypass Java validation. A database constraint provides a final integrity boundary for all writers. Using both layers can therefore provide early feedback and durable data protection.
What should you consider before adding NOT NULL to an existing column?
First, inspect whether existing rows contain `NULL`. Those rows must be backfilled or otherwise resolved before the column can safely become non-nullable. You should also consider application deployment order so new writes and old application versions remain compatible during the migration. The exact migration strategy depends on the database and the production deployment model.