Multiple clauses in one check constraint?
-
27-10-2019 - |
Question
I'm trying to get multiple clauses in one check constraint, separated by an OR
:
check (stop>start or stop = NULL)
where start and stop are timestamp fields. So, stop
can be left empty, but if it's specified, it has to be later than start
.
Am I getting this entirely wrong? I haven't found anything on timestamp comparisons or if OR
is allowed in check constraints.
This is in postgres 8.4.8.
Solution
You should use stop IS NULL
, not stop = NULL
.
OTHER TIPS
This is allowed, but:
stop = NULL
is meaningless; you meanstop IS NULL
.- there's no need to specify
OR stop IS NULL
anyway, because check constraints only forbid something from being "false", andstop > start
will be "null" (neither "true" nor "false") ifstop
is null.
For more information about check constraints, see http://www.postgresql.org/docs/8.4/interactive/ddl-constraints.html.
It should be noted that a check constraint is satisfied if the check expression evaluates to true or the null value.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow