Updating MySQL primary key
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Updating a MySQL primary key can mean two different operations: changing the value stored in a primary-key column, or changing which column or columns form the primary key definition. The first is done with UPDATE, while the second is a schema change that usually uses ALTER TABLE and requires more care because foreign keys, indexes, and application assumptions may all be affected.
Understand Which Change You Actually Need
Developers often say "update the primary key" when they really mean one of these:
- change a row's existing primary-key value
- replace the primary key with another column or composite key
- convert an integer key to
AUTO_INCREMENT
Those are not the same operation. The SQL, the risk, and the migration plan differ substantially.
Changing the Value of a Primary-Key Column
If the primary key is not generated automatically and you really need to change a specific row's key value, you can use UPDATE just as you would for another column.
That only succeeds safely if the new value is still unique and does not violate any foreign-key relationship.
If related tables reference that key, you need to know whether the foreign keys are defined with ON UPDATE CASCADE. If they are not, the update may fail or leave application logic broken.
A parent-child example:
With ON UPDATE CASCADE, changing customers.customer_id can propagate to orders.customer_id automatically.
Changing Which Column Is the Primary Key
If you want a different column to become the primary key, that is a schema migration.
This is much riskier than changing one row value because MySQL enforces uniqueness and non-null requirements on primary keys. Before running a migration like this, confirm that the target column:
- contains no duplicates
- contains no
NULLvalues - is appropriate for long-term identity semantics
Natural keys such as email addresses often look convenient at first and become painful later when business rules change.
Working Safely With Foreign Keys
Primary keys are often referenced all over the schema. Before changing either the key value or key definition, inspect dependent constraints.
Useful diagnostic query:
That tells you which child tables depend on the target table. It is a good habit to inspect those dependencies before any key migration, not after something fails in production.
Prefer Surrogate Keys for Stability
A frequent design lesson here is that keys that might change are poor primary keys. If your identifier can be edited by users or business rules, consider storing it as a unique secondary column and using a stable surrogate key as the primary key.
For example:
Then email can change when needed without forcing a primary-key update across related tables.
Use Transactions and Migration Planning
For small data corrections, a transaction can help you validate and roll back if needed.
For schema-level primary-key changes, use a migration plan, not an ad hoc console edit. Application deployments, ORM mappings, and downstream queries may all need coordinated updates.
Common Pitfalls
The biggest mistake is changing a primary key without checking dependent foreign keys. Even when the database blocks the change safely, the failure can happen mid-deployment and leave you scrambling.
Another mistake is using a mutable business field as the primary key when the field is likely to change. That design creates avoidable migration pain later.
People also forget that changing the primary key definition changes the table's main unique index. On large tables, that can be expensive and may require maintenance planning.
Finally, do not assume UPDATE and ALTER TABLE solve the same problem. One changes data values; the other changes schema structure.
Summary
- Updating a primary key value and redefining the primary key are different operations.
- Use
UPDATEto change a row's key value, but only after checking uniqueness and foreign-key effects. - Use
ALTER TABLEto change which column or columns form the primary key. - Inspect foreign-key dependencies before touching a primary key.
- Stable surrogate keys usually make future schema changes much easier.

