Common SQL Errors

MySQL Error 1171: ER_PRIMARY_CANT_HAVE_NULL - How to Fix and Prevent

Galaxy Team
August 6, 2025

MySQL throws ER_PRIMARY_CANT_HAVE_NULL (SQLSTATE 42000) when any column in a PRIMARY KEY or composite PRIMARY KEY is defined as NULL or inserts NULL.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error 1171 ER_PRIMARY_CANT_HAVE_NULL?

MySQL Error 1171: ER_PRIMARY_CANT_HAVE_NULL happens when a PRIMARY KEY column can hold NULL. MySQL requires every column in a PRIMARY KEY to be declared NOT NULL, and every insert must supply a non-NULL value. Redefine the column as NOT NULL or remove it from the PRIMARY KEY to solve the problem.

Error Highlights

Typical Error Message

All parts of a PRIMARY KEY must be NOT NULL; if you need

Error Type

Constraint Error

Language

MySQL

Symbol

ER_PRIMARY_CANT_HAVE_NULL

Error Code

1171

SQL State

42000

Explanation

Table of Contents

What is MySQL Error 1171: ER_PRIMARY_CANT_HAVE_NULL?

MySQL raises error 1171 with message “All parts of a PRIMARY KEY must be NOT NULL” when it detects a column that can store NULL values inside a PRIMARY KEY definition. The engine refuses to create, alter, or insert data that violates this rule.

A PRIMARY KEY enforces entity uniqueness and row identity. Because NULL means “unknown,” allowing NULL in any key column would break uniqueness guarantees.

MySQL therefore blocks the operation and returns SQLSTATE 42000.

.

When Does This Error Appear?

The error surfaces while running CREATE TABLE, ALTER TABLE, or INSERT/UPDATE statements. It can also trigger during bulk loads or migrations if schema scripts overlook NOT NULL requirements.

Developers usually notice it after adding a composite PRIMARY KEY where one part is nullable or when an application tries to insert NULL into an existing key column.

Why Is It Critical to Resolve?

Ignoring the error leaves the table without a valid primary key, harming referential integrity, join performance, and replication. Production inserts will continue failing until the schema is fixed, causing downtime and data loss risks.

Common Causes

Nullable column definition

The column is declared without NOT NULL, making it nullable by default.

Adding the column to a PRIMARY KEY instantly triggers error 1171.

Composite key with mixed nullability

One column in a multi-column PRIMARY KEY allows NULL while the others are NOT NULL, violating the all-or-nothing rule.

Schema migration oversights

Automated migration tools may create tables with NULL defaults, especially when porting from databases that permit NULL keys.

Application insert of NULL

The table definition is correct, but an INSERT or UPDATE supplies NULL to a key column, breaching the NOT NULL constraint.

.

Related Errors

FAQs

Can I allow NULL in a primary key?

No. SQL standards and MySQL forbid NULL in any PRIMARY KEY column. Use a UNIQUE index if NULL must be allowed.

Does AUTO_INCREMENT imply NOT NULL?

Yes. Declaring AUTO_INCREMENT automatically adds NOT NULL, preventing error 1171 on that column.

Will converting to InnoDB change this rule?

No. All MySQL storage engines enforce the NOT NULL requirement on primary keys.

How does Galaxy help prevent this error?

Galaxy’s schema-aware AI copilot scans DDL, highlights nullable key columns, and proposes corrected ALTER TABLE commands before execution.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo