Re: Seeking quick way to clone a row, but give it a new pk.

From: Achilleas Mantzios <achill(at)matrix(dot)gatewaynet(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Seeking quick way to clone a row, but give it a new pk.
Date: 2007-02-08 07:58:12
Message-ID: 200702080958.12748.achill@matrix.gatewaynet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Στις Πέμπτη 08 Φεβρουάριος 2007 09:19, ο/η Bryce Nesbitt έγραψε:
> 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 = 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=9;
> 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.

--
Achilleas Mantzios

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Huxton 2007-02-08 09:05:34 Re: metaphone and nysiis in postgres
Previous Message Bryce Nesbitt 2007-02-08 07:19:46 Re: Seeking quick way to clone a row, but give it a new pk.