How can I create a UNIQUE constraint of multiple columns, that is applied only when the columns are not NULL? So assuming that I create a constraint for column a and b, 1 2 and 1 2 cannot co-exist but 1 NULL and 1 NULL can exist together.
-
C'mon, you could really have tried a simple standard unique constraint and found that it already does what you want.sticky bit– sticky bit2021-03-18 02:14:02 +00:00Commented Mar 18, 2021 at 2:14
Add a comment
|
1 Answer
Actually, adding a unique constraint across the two columns would, by default, allow entering (1, NULL) twice. The reason for this is that NULL is treated as "unknown," and therefore Postgres will treat this as not being duplicate.
Check the demo to see this working in action.