Thread: Questions concerning check constraints

Questions concerning check constraints

From
"Christian Rengstl"
Date:
Hi list,

I have two question concerning check constraints:
1) Is it possible to refer to a column in a different table, ie
entering a value in mytable.x should only be allowed if mytable2.y=1 for
example? I know that it is possible to use triggers to do that, but I
think adding a simple check constraint might be simpler.
2) Is it possible to issue custom error messages in case a constraint
is violated?

Thanks!



Christian Rengstl M.A.
Klinik und Poliklinik für Innere Medizin II
Kardiologie - Forschung
Universitätsklinikum Regensburg
B3 1.388
Franz-Josef-Strauss-Allee 11
93053 Regensburg
Tel.: +49-941-944-7230




Re: Questions concerning check constraints

From
hubert depesz lubaczewski
Date:
On Fri, Nov 02, 2007 at 10:04:06AM +0100, Christian Rengstl wrote:
> 1) Is it possible to refer to a column in a different table, ie
> entering a value in mytable.x should only be allowed if mytable2.y=1 for
> example? I know that it is possible to use triggers to do that, but I
> think adding a simple check constraint might be simpler.

you would need to make a wrapper function for the actual checking, and
then add check (function(column) = true) or something like this.

> 2) Is it possible to issue custom error messages in case a constraint
> is violated?

if you have function based checks - sure, "raise exception"

depesz

--
quicksil1er: "postgres is excellent, but like any DB it requires a
highly paid DBA.  here's my CV!" :)
http://www.depesz.com/ - blog dla ciebie (i moje CV)