3

I have a table with columns: c1, c2, c3, c4 And I am trying to add a CONSTRAINT check The conditions are:

  1. if c4 is not null then c1,c2,c3 all must not null.
  2. else if c4 is null then c1,c2,c3 are optional but at least one field must be non-null.

Here is my sql command:

ADD CONSTRAINT quatereulchk CHECK( 
    (c1 IS NOT NULL AND c2 IS NOT NULL AND c3 IS NOT NULL AND c4 IS NOT NULL) 
    OR 
    (c4 IS NULL AND c1 IS NOT NULL OR c2 IS NOT NULL OR c3 IS NOT NULL)
    );

I tested with pg_admin however the constrain is not working. Thanks!

7
  • Hint: check precedence of AND and OR. If we talked about maths your DDL expects that 2 * 2 + 2 equals to 8 Commented Nov 16, 2015 at 21:28
  • I see, in my case is that means AND is higher than OR? Thank you for your hint! Commented Nov 16, 2015 at 21:54
  • 1
    Yep (and not only in this case but everywhere) Commented Nov 16, 2015 at 21:54
  • 1
    "BTW really like the way of answering question, giving hint instead of giving answer." --- that's nice to hear :-) Unfortunately very few people here respect that Commented Nov 16, 2015 at 22:06
  • 3
    The website is based on the idea of answering questions. It's hard to blame people that follow that plan. Commented Nov 16, 2015 at 22:26

2 Answers 2

4

Try grouping the "c1 IS NOT NULL OR c2 IS NOT NULL OR c3 IS NOT NULL" in the second case, for example:

ADD CONSTRAINT quatereulchk CHECK( (c1 IS NOT NULL AND c2 IS NOT NULL AND c3 IS NOT NULL AND c4 IS NOT NULL) OR (c4 IS NULL AND (c1 IS NOT NULL OR c2 IS NOT NULL OR c3 IS NOT NULL)) )
Sign up to request clarification or add additional context in comments.

Comments

2

Use coalesce():

ADD CONSTRAINT quatereulchk CHECK( 
    (c1 IS NOT NULL AND c2 IS NOT NULL AND c3 IS NOT NULL AND c4 IS NOT NULL) 
    OR 
    (c4 IS NULL AND COALESCE(c1, c2, c3) IS NOT NULL)
    );

Some tests:

insert into example values
(null, null, null, null);
ERROR:  new row for relation "example" violates check constraint "quatereulchk"
DETAIL:  Failing row contains (null, null, null, null).

insert into example values
(null, null, 3, null);
INSERT 0 1

insert into example values
(null, 2, 3, 4);
ERROR:  new row for relation "example" violates check constraint "quatereulchk"
DETAIL:  Failing row contains (null, 2, 3, 4).

insert into example values
(1, 2, 3, 4);
INSERT 0 1

6 Comments

Thank you for your reply I tries with c1,c2,c3,c4 => (1.1, null, null,null) and its not working
Ops, just a typo, should be coalesce(c1, c2, c3). The answer edited.
Now it works for me ;D. Just my curiosity. Between Manuel Saucedo's answer above and your answer, which way is faster?
@Lester it does not matter
@Lester - I do not think there is a significant difference.
|

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.