pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Kellerer <spam_eater@gmx.net>
To: pgsql-admin@postgresql.org
Subject: Re: Is it possible to create a CHECK constraint for my use case?
Date: Tue, 31 Jan 2012 09:33:19 +0100
Message-ID: <jg88u6$l9m$1@dough.gmane.org> (raw)
In-Reply-To: <006c01ccdfef$e0396f60$a0ac4e20$@com>
References: <006c01ccdfef$e0396f60$a0ac4e20$@com>

Gnanakumar, 31.01.2012 09:11:
> Hi,
>
> Our Production server is running PostgreSQL v8.2.22 on CentOS5.2.

You know that 8.2 is "end-of-live"?
http://www.postgresql.org/support/versioning/


> There are 2 columns in the table: "managertype" numeric(1) and "managerid"
> numeric(10).  "managertype" accepts only 2 valid values, either '1' or '2'.
>
> managertype  |	managerid
> ---------------------------------
>          1           |  null values NOT allowed
>          2           | null values allowed
> ---------------------------------
>
> We want to enable a CHECK constraint based on 2 columns data in the table in
> such a way that if "managertype" is 1, then null values are not allowed in
> "managerid" column.  In other words, I want to create a CHECK constraint
> something like this: IF (managertype = 1) THEN managerid IS NOT NULL.
>
> Is it possible to create a CHECK constraint for my use case explained above?


Something like this:

check ( (managertype = 2) or (managertype = 1 and managerid is not null) )

Not sure if the condition (managertype = 2) is actually needed, if the managertype is already constrained to 1/2 but I can't test it right now.

Thomas




view thread (3+ messages)  latest in thread

Message-ID: <jg88u6$l9m$1@dough.gmane.org>
Permalink:  ../jg88u6$l9m$1@dough.gmane.org/
Also on:    postgresql.org/message-id/jg88u6$l9m$1@dough.gmane.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-admin@postgresql.org
  Cc: spam_eater@gmx.net
  Subject: Re: Is it possible to create a CHECK constraint for my use case?
  In-Reply-To: <jg88u6$l9m$1@dough.gmane.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox