Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 870A89FB2B2 for ; Thu, 8 Feb 2007 03:58:43 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 81785-08 for ; Thu, 8 Feb 2007 03:58:15 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from matrix.gatewaynet.com (host2.dynacom.ondsl.gr [62.103.35.210]) by postgresql.org (Postfix) with ESMTP id 423CC9FA36A for ; Thu, 8 Feb 2007 03:58:15 -0400 (AST) Received: from host3.dynacom.ondsl.gr ([62.103.35.211] helo=ip216.internal.net) by matrix.gatewaynet.com with esmtp (Exim 4.62) (envelope-from ) id 1HF4AX-0003Sb-6S for pgsql-sql@postgresql.org; Thu, 08 Feb 2007 09:58:13 +0200 From: Achilleas Mantzios Organization: Dynacom Tankers Mgmt To: pgsql-sql@postgresql.org Subject: Re: Seeking quick way to clone a row, but give it a new pk. Date: Thu, 8 Feb 2007 09:58:12 +0200 User-Agent: KMail/1.9.3 References: <45CA50E6.6090006@obviously.com> <20070207181622.I3035@bravo.pjkh.com> <45CACF12.1060800@obviously.com> In-Reply-To: <45CACF12.1060800@obviously.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200702080958.12748.achill@matrix.gatewaynet.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200702/76 X-Sequence-Number: 27410 =CE=A3=CF=84=CE=B9=CF=82 =CE=A0=CE=AD=CE=BC=CF=80=CF=84=CE=B7 08 =CE=A6=CE= =B5=CE=B2=CF=81=CE=BF=CF=85=CE=AC=CF=81=CE=B9=CE=BF=CF=82 2007 09:19, =CE= =BF/=CE=B7 Bryce Nesbitt =CE=AD=CE=B3=CF=81=CE=B1=CF=88=CE=B5: > Philip Hallstrom wrote: > >> I need to create some nearly identical copies of rows in a complicated > >> table. > >> > >> Is there a handy syntax that would let me copy a existing row, but get= a > >> new primary key for the copy? I'd then go in an edit the 1 or 2 > >> additional columns that differ. The duplicate would be in the same > >> table as the original. > >> > >> This would save me a bunch of typing. Can it be done? > > > > INSERT INTO mytable SELECT * FROM mytable WHERE pk =3D 123; > > > > Or something close to that... I suspect if you changed the '*' to the > > columns you wanted you could also work in the other columns you want > > to change as well... > > But that will violate the unique primary key constraint: > > insert into xx_plan_rule select * from xx_plan_rule where rule_id=3D9; > ERROR: duplicate key violates unique constraint "xx_plan_rule_pkey" If you have that requirement often i would recommend writing a program taking the tablename,id as args, read DB metadata and act accordingly. =2D-=20 Achilleas Mantzios