Postgres 9.3
I have two integer type columns 'a' and 'b'. Valid options are:
I'm trying to add a check/constraint which prevents the invalid option:
I'd be grateful for any help.
Thanks in advance Dan
Looks like your condition is actually a logical implication: if a is null
then b is null
. From our symbolic logic course (PHIL 140 in my case), we know that:
(P -> Q) <-> (-P | Q)
where ->
is implication and <->
is logical equivalence.
Applying that to our implication and SQLizing it yields:
a is not null or b is null
so a simple check(a is not null or b is null)
constraint is sufficient.
Demo: http://sqlfiddle.com/#!15/bebee/1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With