Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WQP5I-0005T3-JQ for pgsql-sql@arkaria.postgresql.org; Wed, 19 Mar 2014 22:35:40 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WQP5H-0004Ci-Te for pgsql-sql@arkaria.postgresql.org; Wed, 19 Mar 2014 22:35:39 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WQP5H-0004Cc-8t for pgsql-sql@postgresql.org; Wed, 19 Mar 2014 22:35:39 +0000 Received: from mimolette.dalibo.net ([212.85.154.222]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WQP5E-0005Xd-D6 for pgsql-sql@postgresql.org; Wed, 19 Mar 2014 22:35:38 +0000 Received: from [192.168.0.38] (ip-68.net-81-220-133.standre.rev.numericable.fr [81.220.133.68]) by mimolette.dalibo.net (Postfix) with ESMTPSA id C1991682000E; Wed, 19 Mar 2014 23:35:05 +0100 (CET) Message-ID: <532A1B97.4000902@dalibo.com> Date: Wed, 19 Mar 2014 23:35:03 +0100 From: Vik Fearing User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 MIME-Version: 1.0 To: Sergey Konoplev , AlexK CC: pgsql-sql Subject: Re: Can I use a constraint to make sure all array elements are positive? References: <1395266376754-5796846.post@n5.nabble.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org On 03/19/2014 11:20 PM, Sergey Konoplev wrote: > On Wed, Mar 19, 2014 at 2:59 PM, AlexK wrote: >> One of the columns in my table is FLOAT[] NOT NULL. Can I use a constraint to >> make sure all array elements are positive? > Sure, you can. Use all() in the CHECK constraint: > > CREATE TABLE c (f float[] NOT NULL CHECK (0 < all(f))); > CREATE TABLE > > INSERT INTO c VALUES (array[1,2,3]); > INSERT 0 1 > > INSERT INTO c VALUES (array[1,2,3,0]); > ERROR: new row for relation "c" violates check constraint "c_f_check" > DETAIL: Failing row contains ({1,2,3,0}). D'oh! Much better than my solution. -- Vik -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql