Re: INSERT OR UPDATE?

From: "Dean Gibson (DB Administrator)" <postgresql4(at)ultimeth(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: INSERT OR UPDATE?
Date: 2005-10-09 16:41:09
Message-ID: 43494825.7070806@ultimeth.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Try (for simple cases):

DELETE FROM my.table WHERE somecondition;
INSERT INTO my.table (somefield) VALUES ('$someval');

In complex cases it may be necessary to INSERT the values into a
temporary table, which is then used to condition the DELETE before
INSERTing the temporary table into your permanent table.

One advantage of the above is that it works for multiple rows, whereas
your technique does not.

-- Dean

On 2005-10-08 23:15, smorrey(at)gmail(dot)com wrote:

>Hello all,
>
>I am writing an app in PHP that uses a PostGres database.
>One thing i have noticed is that what should/could be a single line of
>SQL code takes about 6 lines of PHP. This seem wasteful and redundant
>to me.
>
>Here is a sample of what I'm talking about ($db is a PDO already
>defined and created).
>
>$query[1] = "UPDATE my.table, SET somefield = '$someval' WHERE
>somecondition";
>$query[2] = "INSERT INTO my.table (somefield) VALUES ('$someval')";
>if(!$db->query($query[1])){
> $db->query($query[2]);
>}
>
>What I'm curious to know is if there is some way to simplify this,
>either buy some PHP builtin or extension, or possibly something in SQL
>I am missing. It seems to me that "UPDATE OR INSERT", should be valid,
>but I can't seem to find anything relevant at all about it.
>
>Anyways I hope you don't mind, but I'm crossposting this to
>pgsql.general and comp.lang.php to see if I can get some information on
>the subject.
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 4: Have you searched our list archives?
>
> http://archives.postgresql.org
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jim C. Nasby 2005-10-09 16:51:21 Re: Oracle buys Innobase
Previous Message Chris Browne 2005-10-09 16:18:23 Re: Oracle buys Innobase