-1

how can update a unique index in mysql without getting duplicate entry error?

i want to update it if there is no duplicate value and in case of duplicate entry just do nothing , neither update row neither get an error .

i found out ON DUPLICATE KEY does not work in update query .

Shadow
  • 33,525
  • 10
  • 51
  • 64
abootorabi
  • 53
  • 5

1 Answers1

1

Use update ignore ... statement instead of simple update ..., just be aware of the side effects (last sentence of the quote below). As mysql manual on update syntax says:

With the IGNORE modifier, the update statement does not abort even if errors occur during the update. Rows for which duplicate-key conflicts occur on a unique key value are not updated. Rows updated to values that would cause data conversion errors are updated to the closest valid values instead.

If the side effects are not acceptable to you, then you need to check for duplicate values before updating with a select ... for update statement to lock the record to be updated. A third alternative would be to ignore duplicate key errors in your application's error handling of this statement. I would go with the last solution, to be honest.

Shadow
  • 33,525
  • 10
  • 51
  • 64