How can you create a unique constraint on a combination of two values in two columns.
meaning
column1 column2
2 1
looking for the constraint to disallow
column1 column2
1 2
How can you create a unique constraint on a combination of two values in two columns.
meaning
column1 column2
2 1
looking for the constraint to disallow
column1 column2
1 2
A unique constraint on 2 columns only prevents those exact 2 values being inserted (switching them is allowed):
So you need A TRIGGER like this (ORACLE):
Warning: syntax not checked.
If your database allows expressions in an index you can do something like this (ANSI SQL):
Note this is a unique index not a unique constraint. The only difference for most DBMS is that you can't have a unique index as the target of a foreign key, but otherwise they serve the same purpose.
If your DBMS does not have
least()
orgreatest()
you can replace that using a CASE expression:Looking at the documentation, found this for the ORACLE SGBD :
Chapter "Specifying the Index Associated with a Constraint" on the page ORACLE documentation.
Hop this help.