Re: [GENERAL] select for insert? - Mailing list pgsql-general

From Jose Soares
Subject Re: [GENERAL] select for insert?
Date
Msg-id 38846D8E.7B6D852E@sferacarta.com
Whole thread Raw
In response to Re: [GENERAL] stored procedure revisited  (Peter Mount <peter@retep.org.uk>)
List pgsql-general

Lincoln Yeoh wrote:

> Hi everyone,
>
> say I have a table test
> create table test (a int, b text);
>
> and I want to ensure that 'b' is unique for a particular 'a' ('a' not being
> unique).
>
> Right now I can do something like
> begin transaction
> select count(*) from test where b =$value;
> if count = 0 then insert into test (a,b) values ($a, $value);
>
> But there is still a chance of a duplicate being inserted.

use unique constraint on column a as in:

CREATE TABLE test (a char unique, b int);

José



pgsql-general by date:

Previous
From: John Brothers
Date:
Subject: Primary Key Indexing problem
Next
From: "omid omoomi"
Date:
Subject: Re: [GENERAL] select for insert?