2022
MySQLERRORNotableData IntegrityHIGH confidence

Invalid use of NULL value

Production Risk

Low — INSERT or UPDATE is rejected; no data is modified.

What this means

NULL was used in a context that does not allow it, such as inserting NULL into a NOT NULL column in strict mode, or using NULL in a primary key.

Why it happens
  1. 1Inserting NULL into a NOT NULL column with strict SQL mode enabled.
  2. 2Setting a primary key column to NULL.
  3. 3Using NULL in a context that requires a definite value.
How to reproduce
trigger — this will error
trigger — this will error
INSERT INTO users (id, name) VALUES (1, NULL); -- name is NOT NULL

expected output

ERROR 2022 (23000): Column 'name' cannot be null.

Fix 1

Provide a non-NULL value for the column

Provide a non-NULL value for the column
INSERT INTO users (id, name) VALUES (1, 'Unknown');

Why this works

A non-NULL value satisfies the NOT NULL constraint.

Fix 2

Set a DEFAULT value on the column

Set a DEFAULT value on the column
ALTER TABLE users MODIFY name VARCHAR(100) NOT NULL DEFAULT 'Unknown';

Why this works

A default value is used when no value is explicitly provided.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 2022 ER_INVALID_USE_OF_NULL

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MySQL errors