Re: mysql's "replace into..." - Mailing list pgsql-sql

From Josh Berkus
Subject Re: mysql's "replace into..."
Date
Msg-id 3A9D7654.453E40A9@agliodbs.com
Whole thread Raw
In response to RE: mysql's "replace into..."  ("Diehl, Jeffrey" <jdiehl@sandia.gov>)
List pgsql-sql
Jeff,

> Thanx for your help.  I found replace into to be a nice convience since my
> primary keys are often expressions build from several fields which I want to
> keep unique.  Replace into enforces this uniqueness.  I was learning SQL
> when I started this project so I didn't know this was non-standard.  Bummer.

No, what you're describing are called "composite keys".  They are a
standard part of SQL, but most developers end up using numerical
surrogate keys because they are easier to handle.

> But since my primary keys are expressions, I can't use the technique you
> suggested.  I'm thinking of simply trying the insert and checking to see if
> I get an error.  If I get an error, I do an update...  Not sure how well
> this will work, tho.

Better to do it the other way. Search for that key; if it's returned, do
an update; if the results are NULL, do an insert.  If your table has few
fields (<15) you can even do this through a function, passing the field
values as parameters of the function.
                -Josh Berkus

-- 
______AGLIO DATABASE SOLUTIONS___________________________                                       Josh Berkus  Complete
informationtechnology      josh@agliodbs.com   and data management solutions       (415) 565-7293  for law firms, small
businesses      fax  621-2533   and non-profit organizations.       San Francisco
 


pgsql-sql by date:

Previous
From: George Young
Date:
Subject: plpgsql notify trigger
Next
From: Kyle
Date:
Subject: Re: Daily Digest V1 #282