Duplicating Rows

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
BigAbe
Forum Commoner
Posts: 66
Joined: Fri Mar 31, 2006 7:41 pm

Duplicating Rows

Post by BigAbe »

Is there any shortcut to automatically duplicate a row into the same table?

The only catch is that my PK is autoincrementing and I want to duplicate one row as a new row.

Thanks!

-- Abe --
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

BigAbe
Forum Commoner
Posts: 66
Joined: Fri Mar 31, 2006 7:41 pm

Post by BigAbe »

Maybe I'm extremely lazy, but this sounds like a bit too much for one operation.

Is there any other way to just copy one row?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

It's actually not that difficult, and other then inserting a record, I don't see another method of doing it.
BigAbe
Forum Commoner
Posts: 66
Joined: Fri Mar 31, 2006 7:41 pm

Post by BigAbe »

astions wrote:It's actually not that difficult, and other then inserting a record, I don't see another method of doing it.
Fair enough - I guess I wish programming were simpler than it is.

Or maybe not... If that were so, I might be out of a job. 8)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If you don't mind me asking, what is the benefit to having duplicate data (less the PK) in a table?
BigAbe
Forum Commoner
Posts: 66
Joined: Fri Mar 31, 2006 7:41 pm

Post by BigAbe »

Everah wrote:If you don't mind me asking, what is the benefit to having duplicate data (less the PK) in a table?
Not so much a benefit, just an easier way to manage my data.

I'm in the finishing stages of building a full-on classified system for my university.

One of the parts is like a control panel, where users can view their currently running ads and previous ads. My system allows them to repost or delete ads with one click.

Because we want to keep a complete record of all ads being posted (not trying to be "big-brother", just CYA), when ads are deleted, we simply "deactivate" them versus actually deleting them.

So, when a user wants to repost their ad, we set the current ad to inactive, and repost the very same ad under a new row with the same exact information, just a different PK.

Does this make sense?

-- Abe --
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Yeah. You might be able to achieve that with SELECT INTO, but I am not certain.
Post Reply