Common SQL Errors

MySQL Error 1173: ER_REQUIRES_PRIMARY_KEY – How to Fix and Prevent

Galaxy Team
August 6, 2025

The error arises when you create or alter a table using a storage engine that mandates a PRIMARY KEY but the table definition lacks one.

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 1173 ER_REQUIRES_PRIMARY_KEY?

MySQL Error 1173 ER_REQUIRES_PRIMARY_KEY appears when you create or alter a table with an engine such as InnoDB or NDB that requires a PRIMARY KEY but none exists. Add a PRIMARY KEY column or mark an existing unique NOT NULL column as the PRIMARY KEY to resolve the issue.

Error Highlights

Typical Error Message

This table type requires a primary key

Error Type

Schema Definition Error

Language

MySQL

Symbol

ER_REQUIRES_PRIMARY_KEY

Error Code

1173

SQL State

42000

Explanation

Table of Contents

What is MySQL Error 1173 ER_REQUIRES_PRIMARY_KEY?

Error 1173 triggers when MySQL processes a CREATE TABLE or ALTER TABLE statement that uses a storage engine demanding a PRIMARY KEY but finds none defined in the table.

InnoDB, NDB Cluster, and several third-party engines rely on a unique, non-null key to build clustered indexes and ensure row identity.

Lacking that key, the engine refuses the operation and MySQL returns ER_REQUIRES_PRIMARY_KEY.

Fixing the problem quickly matters because missing primary keys cause performance issues, replication failures, and broken ORM assumptions.

.

What Causes This Error?

The primary cause is creating a new table without declaring a PRIMARY KEY while using InnoDB or NDB. MySQL validates the schema before execution and aborts when the requirement is unmet.

The same validation runs during ALTER TABLE when converting an existing MyISAM table to InnoDB, adding partitioning, or changing the storage engine in a replication setup.

How to Fix MySQL Error 1173 ER_REQUIRES_PRIMARY_KEY

Add a PRIMARY KEY column that uniquely identifies each row. An AUTO_INCREMENT integer is the simplest option and avoids NULLs.

If a suitable UNIQUE NOT NULL column already exists, mark it as the PRIMARY KEY instead. This avoids adding redundant columns and preserves existing data models.

Common Scenarios and Solutions

Creating log tables in InnoDB without keys is common. Declare an id BIGINT AUTO_INCREMENT PRIMARY KEY to satisfy the engine.

Altering legacy MyISAM tables during migrations frequently raises 1173. Issue ALTER TABLE t ADD PRIMARY KEY(id); before switching the engine.

Best Practices to Avoid This Error

Enforce a design rule that every transactional table includes a single-column numeric primary key. Code reviews and linters can automate the check.

Use MySQL Workbench or Galaxy to visualize schemas and verify key definitions before deployment. Continuous integration can run CREATE DATABASE scripts to catch 1173 early.

Related Errors and Solutions

Error 1503 (ER_PARSE_ERROR) surfaces if the PRIMARY KEY syntax is malformed. Error 1068 complains when multiple primary keys exist. Checking key definitions addresses both quickly.

Common Causes

Storage engine that mandates primary keys

InnoDB and NDB refuse tables without a PRIMARY KEY because they require a clustered index for row lookup.

Missing PRIMARY KEY in CREATE TABLE

Developers forget to append PRIMARY KEY(id) at the end of the column list when designing new tables.

Engine conversion via ALTER TABLE

Switching MyISAM tables to InnoDB without adding a primary key first immediately raises error 1173.

Replication or import scripts

Dump files generated from engines that allow keyless tables fail on restore when the target server defaults to InnoDB.

.

Related Errors

FAQs

Does every InnoDB table need a PRIMARY KEY?

Yes. InnoDB uses the primary key as its clustered index. Without it the engine refuses the table, resulting in error 1173.

Can I use a composite primary key?

Composite keys are valid as long as all columns are NOT NULL and guarantee uniqueness. Ensure they align with your query patterns.

How do I add a primary key without downtime?

Use pt-online-schema-change or gh-ost to add the key on a shadow table and swap it in place, avoiding locks on large tables.

Why did my dump restore fail with 1173?

The source database allowed keyless tables under MyISAM. The target defaults to InnoDB, which requires a primary key, so the import stops.

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