Re: most idiomatic way to "update or insert"?

From: Duane Lee - EGOVX <DLee(at)mail(dot)maricopa(dot)gov>
To: "'Mark Harrison'" <mh(at)pixar(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: most idiomatic way to "update or insert"?
Date: 2004-08-05 00:52:56
Message-ID: 64EDC403A1417B4299488BAE87CA7CBF01CD0F0A@maricopa_xcng0
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

You could always do a SELECT and if the row exists then UPDATE otherwise
INSERT. Or INSERT and if you get an error then UPDATE.

Duane

-----Original Message-----
From: Mark Harrison [mailto:mh(at)pixar(dot)com]
Sent: Wednesday, August 04, 2004 4:26 PM
To: pgsql-general(at)postgresql(dot)org
Subject: [GENERAL] most idiomatic way to "update or insert"?

So I have some data that I want to put into a table. If the
row already exists (as defined by the primary key), I would
like to update the row. Otherwise, I would like to insert
the row.

I've been doing something like

delete from foo where name = 'xx';
insert into foo values('xx',1,2,...);

but I've been wondering if there's a more idiomatic or canonical
way to do this.

Many TIA,
Mark

--
Mark Harrison
Pixar Animation Studios

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Browse pgsql-general by date

  From Date Subject
Next Message Christopher Kings-Lynne 2004-08-05 01:28:23 Re: enforcing a join type
Previous Message Peter Darley 2004-08-04 23:51:31 Re: most idiomatic way to "update or insert"?