I have a table which contains a unique integer field containing consecutive values. When I try to increment these values using the method below I violate the unique constraint. Is there a method for doing this successfully?
CREATE TABLE numbers(num INT UNIQUE NOT NULL)
UPDATE numbers SET num=num+1
This has to be a bug. It obviously won’t induce a constraint violation, and it works in SQL Server. In fact, I am quite certain it is a bug, since I can make it succeed if I insert the numbers in descending order:
The correctness of an UPDATE should not depend on the order of rows in the table.
As a simple work-around, you could do this: