1

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.

1
  • C'mon, you could really have tried a simple standard unique constraint and found that it already does what you want. Commented Mar 18, 2021 at 2:14

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.