Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 0A5D4632BB6 for ; Wed, 8 Apr 2009 00:08:48 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 27334-10 for ; Wed, 8 Apr 2009 00:08:47 -0300 (ADT) X-Greylist: delayed 00:21:21.6516 by SQLgrey-1.7.6 Received: from mailproxy.safepages.com (mailproxy.safepages.com [216.126.204.70]) by mail.postgresql.org (Postfix) with ESMTP id F2CAB632A18 for ; Wed, 8 Apr 2009 00:08:46 -0300 (ADT) Received: from kyoto.home (5131-133-241.239.popsite.net [64.24.133.241]) by mailproxy.safepages.com (Postfix) with ESMTP id 4AA4F177624 for ; Tue, 7 Apr 2009 20:47:22 -0600 (MDT) Received: from kyoto.home (kyoto.home [127.0.0.1]) by kyoto.home (Postfix) with ESMTP id DDE6C45EFA6 for ; Tue, 7 Apr 2009 20:47:17 -0600 (MDT) Message-ID: <49DC1034.1060705@frii.com> Date: Tue, 07 Apr 2009 20:47:16 -0600 From: Stuart McGraw User-Agent: Thunderbird 2.0.0.19 (X11/20090105) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: changing multiple pk's in one update Content-Type: text/plain; charset=ISO-2022-JP Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200904/43 X-Sequence-Number: 32389 Hello all, I have a table with a primary key column that contains sequential numbers. Sometimes I need to shift them all up or down by a fixed amount. For example, if I have four rows with primary keys, 2, 3, 4, 5, I might want to shift them down by 1 by doing: UPDATE mytable SET id=id-1 (where "id" is the pk column) so that the pk's are now 1, 2, 3, 4. When I try to shift them up by using +1 in the above update statement, I get (not surprisingly) a duplicate key error. I also realize that the -1 case above works only by luck. So my question: Is there some way, perhaps with ORDER BY, that I can achieve the change I want with a single update statement? (If I have an unused key range large enough, I suppose I could update all the keys to that range, and then back to my target range but the requires two updates (there are a lot of foreign keys referencing these primary keys) and requires that I have an available range, so a single update statement would be preferable.) Thanks for any enlightenment.