Skip to content
Advertisement

non UNIQUE constraints

So, I got in this situation: I create a table (let say model_a), in this table I have 2 cols (let say col1 and col2). I need a constraint that satisfy this reality:

It’s not UNIQUE constraint, you can duplicate the first row.

The only one time this constraint active is col1 is the same but col2 is different.

Need help 🙂 Thanks a lot!

Advertisement

Answer

I am thinking of a unique index on the least/greatest value of both columns, that applies only to rows where the values are not equal:

Demo on DB Fiddle:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement