Re: [GENERAL] select for insert?

From: Lincoln Yeoh <lylyeoh(at)mecomb(dot)com>
To: "pgsql-general(at)postgreSQL(dot)org" <pgsql-general(at)postgreSQL(dot)org>
Subject: Re: [GENERAL] select for insert?
Date: 2000-01-18 08:13:26
Message-ID: 3.0.5.32.20000118161326.008f1b80@pop.mecomb.po.my
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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.

Is using a multicolumn unique index the best way to avoid such problems?
Any other suggestions?
Somehow I prefer to avoid getting exceptions from databases.

Is there a "select for insert" command?
e.g. while in transactions if a select for insert has the same criteria,
it's not executed until the other prior matching transactions are
completed. How yucky is that? <grin>

Thanks,

Link.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message John Brothers 2000-01-18 12:20:00 Primary Key Indexing problem
Previous Message Robert 2000-01-18 07:33:37 Re: [GENERAL] Two serial fields question