Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk4 min

How to Fix “Duplicate entry ‘0’ for key ‘PRIMARY’” in MySQL

A duplicate primary-key value of 0 does not prove MySQL lost its AUTO_INCREMENT counter. Check the table, application session SQL mode, and emitted INSERT first.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

2. Check SQL mode on the failing connection

On the same connection that runs the failing insert, check the active session mode:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.