How to add a primary key to a MySQL table?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Adding a primary key to an existing MySQL table is easy only when the data already satisfies the primary-key rules. The candidate column, or column combination, must be unique and non-null before ALTER TABLE ... ADD PRIMARY KEY can succeed.
Check the existing data first
A primary key is both a uniqueness constraint and the table's main row identifier. If the current data has duplicates or NULL values, MySQL will reject the schema change.
Suppose the table looks like this:
Before promoting order_id to the primary key, check for duplicates:
Then check for missing values:
If either query returns rows, fix the data first. The correct fix might be cleanup, deduplication, or adding a new surrogate key column if the existing business field is not stable enough to be the row identifier.
Add the primary key with ALTER TABLE
Once the candidate column is valid, the schema change itself is simple:
MySQL automatically creates the supporting index. You can verify the result with:
If new rows should receive generated numeric identifiers, modify the column first:
That is a common migration path when an older table was created without proper identity semantics.
Composite primary keys are valid too
A primary key does not have to be one column. Join tables often use a composite primary key because the row identity is the combination itself.
This works well when the paired values are short, stable, and already define uniqueness naturally.
If downstream tables need to reference this table often, or if one part of the composite key may change, a separate surrogate key can still be more convenient.
Sometimes a new key column is the better solution
If the existing data column contains duplicates, can change over time, or has business meaning that should remain editable, do not force it into the role of primary key. Add a new identity column instead.
Then keep the business column separately constrained with UNIQUE if needed. This usually produces a cleaner schema for foreign keys and long-term maintenance.
Common Pitfalls
- Trying to add a primary key before checking for duplicates or
NULLvalues. - Forgetting that a table can have only one primary key at a time.
- Confusing
UNIQUEwithPRIMARY KEYand treating them as interchangeable. - Adding a natural key that may change later and then regretting it when foreign keys depend on it.
- Running a large
ALTER TABLEin production without considering lock time or migration impact.
Summary
- A MySQL primary key candidate must already be unique and non-null.
- Check the data first, then use
ALTER TABLE ... ADD PRIMARY KEY (...). - Add
AUTO_INCREMENTwhen the key should generate new numeric row identifiers. - Composite primary keys are valid when row identity naturally depends on multiple columns.
- If the existing column is unstable or duplicated, add a new surrogate key instead of forcing a bad primary key.
Related reading
- how to add an empty or data map in dynamodb?
- How to add columns dynamically in a column family in cassandra using cql
- How to add item to dynamodb if a field does not exist or matches a condition?
- How to add not null constraint to existing column in MySQL
- How to add primary sort key to an already existing table in AWS dynamo db?
- How to add second node in NosDB opensource?
- How to allow a pods in Kubernetes access external docker container ip like mysql
- How to allow all the HTTPS URLs to sync in CouchbaseLite Android

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.