Given the following simple table structure (SQL Server 2008), I want to be able to maintain the uniqueness of a numerical sequence column, but I want to be able to update this value for any record (s).
CREATE TABLE MYLIST( ID int NOT NULL IDENTITY(1, 1) , TITLE varchar(50) NOT NULL , SEQUENCE int NOT NULL , CONSTRAINT pk_mylist_id PRIMARY KEY(ID) , CONSTRAINT uq_mylist_sequence UNIQUE(SEQUENCE) );
My interface allows me to iterate over the order of the elements and , I will know, before doing the update, which the non-overlapping sequence should be in , but how can I perform the update without encountering a violation of a single restriction?
For example, let's say I have these entries:
ID TITLE SEQUENCE 1 APPLE 1 2 BANANA 2 3 CHERRY 3
And I want to update their serial numbers to the following:
ID TITLE SEQUENCE 1 APPLE 3 2 BANANA 1 3 CHERRY 2
But in fact, I could deal with several dozen elements. Sequence numbers must not overlap. I was thinking of trying to use triggers or temporarily disable the restriction, but this seems to be creating more problems. I use C # and LINQ-to-SQL, but I am open to strictly database solutions.
source share