Goul
Goul

Reputation: 583

Reversible unique constraint in SQL Server

I'm looking to add a unique constraint to a 2 columns table that would be reversible. In other words, if I have:

Table Linking
-------------
Link1 | Link2
-------------
1     |   2

The above entry would be considered the same as:

Table Linking
-------------
Link1 | Link2
-------------
2     |   1

... so I don't want to be able to insert the second case.

Not sure if this is possible, but if so thank you for your help!

cheers

Upvotes: 0

Views: 100

Answers (1)

erikkallen
erikkallen

Reputation: 34391

I guess you could compute link_min = min(link1, link2) and link_max = max(link1, link2) and put a unique constraint on (link_min, link_max).

Or you could put in a constraint CHECK(link1 < link2) and always have the links in that order.

Upvotes: 2

Related Questions