SQL error · INSERT, UPDATE, DELETE and schema

NOT NULL rejects a missing value

Problem: A required column receives NULL from an insert, an update or a transformation.

Solution: Fix the source of the value; leave the column out only when a valid DEFAULT exists.

Why it happens: NOT NULL rejects a missing value

Problem operation
CREATE TABLE products (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  active INTEGER NOT NULL DEFAULT 1
);

INSERT INTO products (id, name)
VALUES (1, NULL);

The column name is present in the INSERT, but its value is explicitly NULL. The database must reject it because it breaks the declared rule.

Check the result with small data

Before the INSERT, we can check which rows are missing a required field.

Self-contained query · SQLite
WITH input(name, price) AS (
  VALUES (NULL, 25), ('Keyboard', 40)
)
SELECT price, CASE WHEN name IS NULL
  THEN 'Reject' ELSE 'Valid' END AS validation
FROM input ORDER BY price;
Query result
pricevalidation
25Reject
40Valid

The row with a price of 25 must be rejected because the name is missing. Do not replace it with an empty string without checking whether that is correct data.

Check your understanding: Would it be acceptable to leave name empty if the schema only says NOT NULL?

Show answer

The constraint would accept it, because an empty string is not NULL. But it is not a valid name: if it must not be allowed, add a rule such as CHECK (name <> '').

“NOT NULL rejects a missing value” error messages by engine

The exact wording depends on the database. Compare these common messages with the one in your console.

EngineTypical message
PostgreSQLERROR: null value in column "name" of relation "products" violates not-null constraint
MySQLERROR 1048 (23000): Column 'name' cannot be null
SQL ServerMsg 515, Level 16: Cannot insert the value NULL into column 'name', table 'store.dbo.products'; column does not allow nulls. INSERT fails.
OracleORA-01400: cannot insert NULL into ("ESQUEMA"."PRODUCTOS"."NOMBRE")
SQLiteRuntime error: NOT NULL constraint failed: products.name

How to fix it in SQL

Valid insert
INSERT INTO products (id, name)
VALUES (1, 'Keyboard');

Also note that active can be omitted because it has DEFAULT 1. That doesn't mean that DEFAULT turns an explicit NULL into 1: they are different ideas. If you supply NULL to a column NOT NULL, the operation is still invalid.

How to check it

Inspect the value reaching the column and run the corrected INSERT on a copy.

How to avoid it next time

DEFAULT does not, on its own, replace an explicit NULL.

Related cases worth checking

If the missing value is legitimate, the question changes: perhaps the constraint does not represent the domain correctly. Before altering the table, confirm whether "unknown" or "not provided yet" is a valid state for that column. If it is, document the model change; if not, fix the flow that produces the missing value.