Re: Can I use a constraint to make sure all array elements are positive?

From: Vik Fearing <vik(dot)fearing(at)dalibo(dot)com>
To: Sergey Konoplev <gray(dot)ru(at)gmail(dot)com>, AlexK <alkuzo(at)gmail(dot)com>
Cc: pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Can I use a constraint to make sure all array elements are positive?
Date: 2014-03-19 22:35:03
Message-ID: 532A1B97.4000902@dalibo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Bhim Kumar 2014-03-20 08:01:02 SQL Query for Foreign constraint
Previous Message Sergey Konoplev 2014-03-19 22:20:57 Re: Can I use a constraint to make sure all array elements are positive?