I have the following table definition:
CREATE TABLE [Car]
(
CarID int NOT NULL PRIMARY KEY IDENTITY(1,1),
FirstColorID int FOREIGN KEY REFERENCES Colors(ColorID),
SecondColorID int FOREIGN KEY REFERENCES Colors(ColorID),
UNIQUE(FirstColorID, SecondColorID)
)
I want the two Color columns to be unique, regardless of the combination they appear in.
E.g. attemping:
INSERT INTO Car (FirstColorID, SecondColorID) VALUES (1, 2); --should succeed
but then trying the same after that first record exists with the colors reversed should fail:
INSERT INTO Car (FirstColorID, SecondColorID) VALUES (2, 1); --should violate constraint/check
I have this problem solved using high-level code, but I’d rather the constraint be enforced directly in the database, preferably without involving things like triggers.
Thanks.
You could maybe create computed column, something like:
UPDATE (You should test it before, i did just very quick testing)
Idea:
Integer is 4 byts. If i put 2 integers together- i get 8 bytes. If i order them- i get unique BigInt value (8 bytes).
So what i do:
So: