NOT NULL Constraint
A NOT NULL constraint requires a column to contain a value for every inserted or updated row. It is used to enforce that required data is present in a database table.
A `NOT NULL` constraint is a database rule applied to a column. It prevents that column from containing the SQL `NULL` value. For example, a `users.email` column can be declared `NOT NULL` when every user must have an email address.
The constraint is checked when a row is inserted or when an existing row is updated. An insert that omits a required column, or explicitly supplies `NULL`, fails unless another mechanism such as a default value supplies a non-null value.
`NULL` does not mean an empty string, zero, or `false`. It represents the absence or unknown value in SQL. Therefore, a `NOT NULL` column may still contain an empty string if the database and column type allow it; `NOT NULL` only prohibits SQL `NULL`.
In Java backend applications, the database constraint should be treated as an important integrity guarantee even when application validation also checks the field. Database enforcement protects data regardless of which application, script, or service performs the write.
Key points
- `NOT NULL` prevents SQL `NULL` values in a column.
- The rule applies to both inserts and updates.
- `NULL` is different from `0`, `false`, and an empty string.
- A default value can provide a value when an insert omits the column.
- Database constraints provide protection independently of application-level validation.
Interview questions
Is NULL the same as an empty string or zero?
No. SQL `NULL` represents the absence or unknown value, while an empty string is a string value and zero is a numeric value. A `NOT NULL` constraint rejects SQL `NULL`, but it does not by itself reject an empty string or zero. Whether those values are acceptable must be handled separately if the application requires stronger validation.
What does a NOT NULL constraint do?
A `NOT NULL` constraint prevents a column from storing SQL `NULL`. It is useful when a field is required for every row, such as a user's email address. The database checks the constraint when rows are inserted or updated. It is a database-level data-integrity rule.
What happens if an INSERT does not provide a value for a NOT NULL column?
The result depends on whether the column has a default value and on the database's insert semantics. If no value is supplied and no applicable default provides one, the insert violates the `NOT NULL` requirement. If the statement explicitly supplies `NULL`, the constraint is violated. A valid non-NULL value must ultimately be stored in the column.
Why use a database NOT NULL constraint if the Java application already validates the field?
Application validation improves user-facing error handling, but it does not guarantee that every write passes through the same Java code. Other services, scripts, administrative tools, or future code may write to the database directly. A database constraint provides a final integrity boundary for all clients. In practice, both application validation and database constraints can be useful because they address different layers.