Re: Seeking quick way to clone a row, but give it a new pk. - Mailing list pgsql-sql

From Aarni Ruuhimäki
Subject Re: Seeking quick way to clone a row, but give it a new pk.
Date
Msg-id 200702081132.58609.aarni@kymi.com
Whole thread Raw
In response to Re: Seeking quick way to clone a row, but give it a new pk.  (Bryce Nesbitt <bryce1@obviously.com>)
List pgsql-sql
On Thursday 08 February 2007 09:19, Bryce Nesbitt wrote:
> >
> > 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"

It will, because you are copying all columns, including the pk.

Try:

INSERT INTO mytable (colname_1, colname_2, colname_3)
SELECT (colname_1, colname_2, colname_3)
FROM mytable WHERE pk = 123;

BR,
--
Aarni Ruuhimäki



pgsql-sql by date:

Previous
From: Richard Huxton
Date:
Subject: Re: metaphone and nysiis in postgres
Next
From: Aarni Ruuhimäki
Date:
Subject: Re: interval as hours or minutes ?