Re: [GENERAL] select for insert?

From: Jose Soares <jose(at)sferacarta(dot)com>
To: Lincoln Yeoh <lylyeoh(at)mecomb(dot)com>
Cc: "pgsql-general(at)postgreSQL(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: [GENERAL] select for insert?
Date: 2000-01-18 13:41:34
Message-ID: 38846D8E.7B6D852E@sferacarta.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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é

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Marc Tardif 2000-01-18 14:55:47 info about udmsearch
Previous Message John Brothers 2000-01-18 12:20:00 Primary Key Indexing problem