I have a non-directed graph table in my SQL Server (2008) database and I want a constraint to prevent duplicates.
My link table has 2 columns, (City1, City2), both integer references into the Cities table. I can use the following:
ALTER TABLE dbo.CityConnections
ADD CONSTRAINT CK_CityConnections_OneWayOnly UNIQUE (City1, City2)
Which does half of what I want but this does not prevent adding a “reverse” connection.
Google showed me
... UNIQUE (MIN(City1,City2), MAX(City1,City2))
Which looks like it will do exactly what I want but the syntax is invalid .
I have also tried a complicated constraint using CASE but I couldn’t get this syntax correct either.
How can I achieve this?
Assuming that you can control your entry to the table, the following should work;