agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: Vik Fearing <vik.fearing@dalibo.com>
To: Sergey Konoplev <gray.ru@gmail.com>
To: AlexK <alkuzo@gmail.com>
Cc: pgsql-sql <pgsql-sql@postgresql.org>
Subject: Re: Can I use a constraint to make sure all array elements are positive?
Date: Wed, 19 Mar 2014 23:35:03 +0100
Message-ID: <532A1B97.4000902@dalibo.com> (raw)
In-Reply-To: <CAL_0b1vXy66xuJB5bO9_YtTCdUJDq5k+rgZ=uR5mAoBg+S_PZA@mail.gmail.com>
References: <1395266376754-5796846.post@n5.nabble.com>
	<CAL_0b1vXy66xuJB5bO9_YtTCdUJDq5k+rgZ=uR5mAoBg+S_PZA@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>

On 03/19/2014 11:20 PM, Sergey Konoplev wrote:
> On Wed, Mar 19, 2014 at 2:59 PM, AlexK <alkuzo@gmail.com> 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



view thread (4+ messages)

Message-ID: <532A1B97.4000902@dalibo.com>
Permalink:  ../532A1B97.4000902@dalibo.com/
Also on:    postgresql.org/message-id/532A1B97.4000902@dalibo.com

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-sql@postgresql.org
  Cc: vik.fearing@dalibo.com, gray.ru@gmail.com, alkuzo@gmail.com
  Subject: Re: Can I use a constraint to make sure all array elements are positive?
  In-Reply-To: <532A1B97.4000902@dalibo.com>

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

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