Reputation: 423
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]+$')
Upvotes: 1
Views: 793
Reputation: 121604
You do not have to use regexp when a_type <> 'a'
:
check (a_type <> 'a' or a_type = 'a' and b ~* '^[0-9]+$')
Upvotes: 1