How to avoid inserting duplicate records in table type sql server for a simple table with two rows key-1 and key-2 -
i created simple table 2 columns, let's id-1 , id-2.
the combination must unique, solved because primary key.
so 1,1 1,2 1,3 taken care of.
but how can avoid combinations 2,1 , 1,2 , 1,3 , 3,1 not allowed?
create 2 other columns computed based on lowest value of first column, put unique index on 2 computed columns.
create table [dbo].[test1]( [id1] [int] not null, [id2] [int] not null, [id1a] (case when [id1]<[id2] [id1] else [id2] end), [id2a] (case when [id1]>[id2] [id1] else [id2] end), constraint [pk_test1] primary key clustered ( [id1a] asc, [id2a] asc )
Comments
Post a Comment