Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →This error means an insert tried to use primary-key value 0, but that value already exists. It does not, by itself, prove that MySQL lost the table’s AUTO_INCREMENT counter. First check the table definition, the SQL mode on the application’s connection, and the exact INSERT the application sent.
What the error means—and what it does not
Duplicate entry '0' for key 'PRIMARY' is a uniqueness violation: the insert attempted to create a row whose primary-key value is zero, and a row with that value already exists. The message identifies the conflicting value; it does not explain why the application supplied it or establish that the auto-increment counter is broken.
As an Amazon Associate I earn from qualifying purchases.
For an indexed AUTO_INCREMENT column in MySQL, inserting NULL or, normally, 0 asks MySQL to generate the next sequence value. But the session mode NO_AUTO_VALUE_ON_ZERO changes the treatment of zero: with that mode active, zero is inserted as a literal value instead. If zero is already the primary key of another row, the insert fails.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
The MySQL Reference Manual describes the usual behavior in its CREATE TABLE statement documentation: “When you insert a value of NULL (recommended) or 0 into an indexed AUTO_INCREMENT column, the column is set to the next sequence value.” The Server SQL Modes documentation explains the exception: “NO_AUTO_VALUE_ON_ZERO suppresses this behavior for 0 so that only NULL generates the next sequence number.” These statements describe Oracle MySQL; check the documentation for your specific server and version before assuming identical behavior in a MySQL-compatible product.
#1 Best Overall
Check these three things before changing anything
1. Confirm the key column is configured as intended
Inspect the table definition and verify that the primary-key column is the column you expect and is declared AUTO_INCREMENT. For example, run:
SHOW CREATE TABLE your_table;
Replace your_table with the affected table’s name. If the intended ID column is not an auto-increment column, or the application is inserting into a different column than expected, changing the counter will not fix the underlying problem.
Rank #2
2. Check SQL mode on the failing connection
On the same connection that runs the failing insert, check the active session mode:
SELECT @@SESSION.sql_mode;
Look for NO_AUTO_VALUE_ON_ZERO. An administrator’s separate SQL shell may use a different session mode from the application, so a check made only in that shell can give the wrong answer about the failing connection.
3. Inspect the exact INSERT and the existing row
Capture the SQL the application actually sends, including its column list and values. Determine whether it includes the ID column and supplies 0, DEFAULT, or another explicit value. Also check whether zero is already present:
SELECT * FROM your_table WHERE id = 0;
Replace id with the primary-key column name. The duplicate error says zero collides with a unique key; this query helps confirm the row behind that collision. The SQL and table definition matter together: an insert that omits the ID behaves differently from one that explicitly supplies a value.
Choose a fix that matches the cause
If the application should get a generated ID
The most direct fix is to stop sending zero as the ID. Omit the auto-increment column from the insert so MySQL can allocate a value. If the column is declared NOT NULL, inserting NULL is another documented way to request the next sequence value. Do not rely on zero to mean “generate an ID” when NO_AUTO_VALUE_ON_ZERO is active.
If the application deliberately needs to preserve zero values
Do not remove NO_AUTO_VALUE_ON_ZERO without checking why it is enabled and whether other inserts rely on literal zero values. MySQL documents that mysqldump includes a statement enabling this mode so zero values can be preserved during reloads: “For this reason, mysqldump automatically includes in its output a statement that enables NO_AUTO_VALUE_ON_ZERO.” See the MySQL SQL mode documentation. If a legacy application sends zero but expects a generated ID, correcting that insert path is generally more targeted than changing a mode that may serve other workflows.
Best Value
If the auto-increment counter appears incorrect
Consider a counter adjustment only after verifying that the intended column is actually AUTO_INCREMENT, examining the table’s existing data, and identifying the storage engine and server version. For InnoDB, MySQL states that ALTER TABLE ... AUTO_INCREMENT = N “can only change the auto-increment counter value to a value larger than the current maximum.” See AUTO_INCREMENT Handling in InnoDB. Setting a counter is therefore not a universal remedy for an insert that explicitly supplies zero.
Which approach has the narrowest impact?
| Approach | Root cause addressed | Scope and trade-off |
|---|---|---|
Fix the application’s INSERT |
It sends zero when a generated ID is intended. | Targets the insert path; preserves the existing SQL mode and its behavior for other inserts and dump reloads. |
| Change session or server SQL mode | The active NO_AUTO_VALUE_ON_ZERO mode makes zero literal, when changing that behavior is actually intended. |
Session scope can be limited to a connection; a broader configuration change can affect other workloads and workflows that preserve zero values. |
| Adjust the auto-increment counter | A counter position is demonstrably wrong after checking the table state and engine-specific behavior. | Does not correct an insert that explicitly supplies zero; InnoDB will not set the counter at or below the current maximum. |
These are not interchangeable fixes. Match the remedy to the evidence: correct the insert when it sends the wrong value, reconsider SQL mode only when its zero-handling behavior is the problem and changing it is safe, and investigate the counter only when the table and insert behavior support that diagnosis.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




