Let’s say a table has 3 columns can I then make it so that the mix between column 1 & 2 is unique if (and only if) column 3 is equal to x?
That would make this work:
----------------------------------------------------- | A | B | C | ----------------------------------------------------- | 1 | 2 | x | ----------------------------------------------------- | 2 | 1 | x | ----------------------------------------------------- | 0 | 0 | y | ----------------------------------------------------- | 0 | 0 | y | -----------------------------------------------------
And this wouldn’t:
| A | B | C | ----------------------------------------------------- | 2 | 2 | x | ----------------------------------------------------- | 2 | 2 | x | ----------------------------------------------------- | 0 | 0 | y | ----------------------------------------------------- | 0 | 0 | y | -----------------------------------------------------
You can create a trigger:
I’d recommend maintaining an index on
(A, B, C)to keep this reasonably performant.However, this won’t yield the same benefits with
INSERT ... ON DUPLICATE KEY UPDATE,REPLACE, etc. as a genuineUNIQUEkey.