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

From: Aarni Ruuhimäki <aarni(at)kymi(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Cc: Philip Hallstrom <postgresql(at)philip(dot)pjkh(dot)com>
Subject: Re: Seeking quick way to clone a row, but give it a new pk.
Date: 2007-02-08 09:32:58
Message-ID: 200702081132.58609.aarni@kymi.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Aarni Ruuhimäki 2007-02-08 10:01:18 Re: interval as hours or minutes ?
Previous Message Richard Huxton 2007-02-08 09:05:34 Re: metaphone and nysiis in postgres