I’m creating a constraint in a table so that none of 8 columns must be equal to each other.
I wanted to do it like this:
alter table mytable
add constraint no_duplicate_values
check (col_1 <> col_2 <> col__3 <> col__4 <> col__5 <> col__6 <> col__7 <> col__8);
But of course this doesn’t work because <>
only accepts two operands, left and right.
Is there an elegant, single-statement alternative that’s equivalent?
2
Answers
To do that you need to use logical AND operator. There is no elegant way BTW, as far as I know. Could you try this one:
I would suggest a simple helper function for a more or less generic solution.
and then
Demo
However, as Thorsten Kettner comments, it may be worth revisiting your data design.