1

I'm trying to create a conditional check constraint in postgresql. When a_type is 'a', I want the b to only contain digit. When a_type is not 'a', I want b to contain any characters. How would I accomplish this? I have this now:

EDIT: I think this should work.

CONSTRAINT test CHECK (a_type <> 'a' AND b ~* '^.$') OR (a_type = 'a' AND b ~* '^[0-9]+$')

1 Answer 1

1

You do not have to use regexp when a_type <> 'a':

check (a_type <> 'a' or a_type = 'a' and b ~* '^[0-9]+$') 
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.