MySQL Duplicate entry for key on UPDATE
Asked Answered
A

1

5

I have a unique index on a column named label, but for some strange reason why I try and do an update like:

UPDATE books SET label = 'foo bar', title = 'something new', modified = UTC_TIMESTAMP();

And there already exists a row with label = 'foo bar' this errors:

 #1062 - Duplicate entry 'foo bar' for key 'label'

How can I make MySQL do the update? This shouldn't be breaking because technically there is still just one row with the key foo bar.

Thanks.

Amontillado answered 8/7, 2012 at 5:58 Comment(0)
V
10

This SQL query is trying to update every single record in the books table with those values, because you have no WHERE clause. Its failing because you can only have one record with that label value yet the query wants to set them all to it.

I think possibly you are not executing the query you intended. Maybe you meant to update the title and time for the record with that label. Check your syntax.

Voroshilovgrad answered 8/7, 2012 at 6:3 Comment(1)
No problem. A Justin helping a Justin is only natural ;) If it solved the problem dont forget to hit the green check!Voroshilovgrad

© 2022 - 2024 — McMap. All rights reserved.