Hi there,

I created a new table and forgot to set the primary key with an auto_increment.

I went back and use:
ALTER TABLE table ADD PRIMARY KEY AUTO_INCREMENT (column);

However, now whenever I try to insert a new row of data, I get the error message "Duplicate entry '0' for primary key'.

It's obviously still not using the auto increment! I've checked that describe table; shows it with the auto_incremement, and I've also tried re-setting the value of auto_increment, but no luck.

Can anyone help?

Recommended Answers

All 2 Replies

Do you already have records in your table?

Yes. And I just discovered that that is what was causing the error.

Because the record I created before I set the auto_increment had a primary key = 0, when I added the auto_increment it got confused since it doesn't accept keys less than 0.

So I changed the id that was causing the issue and works fine now :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.