Duplicating a table row while honouring key constraints

From: Gordon <gordon(dot)mcvey(at)ntlworld(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Duplicating a table row while honouring key constraints
Date: 2007-11-12 12:05:36
Message-ID: 1194869136.999146.322950@19g2000hsx.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I'm developing a web application in PHP and Postgres that will
basically serve as a CMS. I want to implement a feature to allow
users to make copies of documents or folders, so this will require the
appropriate rows to be duplicated.

If possible I'd like to do this with SQL queries and avoid SELECTing
the row, munging it in PHP and INSERTING it back. I suspect that this
is probably the way I'll have to go, but if it could be done entirely
in SQL that would be nice.

At first I thought INSERT INTO table_name SELECT * from table_name
where primary_key = unique_value would do it, but that would obviously
violate the primary key uniqueness constraint. I'm wondering if
there's a way to do this where I only grab the data to be copied and
let the database work out the new primary key itself.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Karsten Hilbert 2007-11-12 12:07:35 Re: Regression in 8.3?
Previous Message Trevor Talbot 2007-11-12 12:00:04 Re: 8.2.3: Server crashes on Windows using Eclipse/Junit