Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 0DB955E060A for ; Tue, 31 Jan 2012 04:32:28 -0400 (AST) Received: from plane.gmane.org ([80.91.229.3]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Rs98d-0003uA-Sy for pgsql-admin@postgresql.org; Tue, 31 Jan 2012 08:32:28 +0000 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1Rs98R-0004Rw-1d for pgsql-admin@postgresql.org; Tue, 31 Jan 2012 09:32:15 +0100 Received: from 217.110.94.67 ([217.110.94.67]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 31 Jan 2012 09:32:15 +0100 Received: from spam_eater by 217.110.94.67 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 31 Jan 2012 09:32:15 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-admin@postgresql.org From: Thomas Kellerer Subject: Re: Is it possible to create a CHECK constraint for my use case? Date: Tue, 31 Jan 2012 09:33:19 +0100 Lines: 33 Message-ID: References: <006c01ccdfef$e0396f60$a0ac4e20$@com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 217.110.94.67 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 In-Reply-To: <006c01ccdfef$e0396f60$a0ac4e20$@com> X-Pg-Spam-Score: -0.7 (/) X-Archive-Number: 201201/147 X-Sequence-Number: 39059 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