Wednesday, May 22, 2013

MySQL Error on UPDATE - Integrity constraint violation: 1062 Duplicate entry

I was doing a MySQL UPDATE operation and obviously it shouldn't create new records. So why would it throw an duplicate entry exception? It can make you scratching your head for a bit of time if you're not aware.

The problem is actually quite simple, you are updating multiple records, having the PRIMARY KEY or UNIQUE KEY included in the input data and the resulting UPDATE operation matches more than one row. Hence one or more rows having a different PRIMARY KEY or UNIQUE KEY is being updated with the input value which already exists in another row and the attempt to update the other rows throws a duplicate error.

Fix this by simply using the UNIQUE KEY or PRIMARY KEY in the WHERE condition.
  • Related Links Widget for Blogspot

No comments: