insert into table select * from table where primarykey=1
I just want to copy one row to insert into the same table (i.e., I want to duplicate an existing row in the table) but I want to do this without having to list all the columns after the "select", because this table has too many columns.
But when I do this, I get the error:
Duplicate entry 'xxx' for key 1
I can handle this by creating another table with the same columns as a temporary container for the record I want to copy:
create table oldtable_temp like oldtable;
insert into oldtable_temp select * from oldtable where key=1;
update oldtable_tem set key=2;
insert into oldtable select * from oldtable where key=2;
Is there a simpler way to solve this?
Update 07/07/2014 - The answer based on my answer, by Grim..., is a better solution as it improves on my solution below, so I'd suggest using that.
You can do this without listing all the columns with the following syntax:
You may decide to change the primary key in another way.
I just had to do this and this was my manual solution:
If you don't know what the PRIMARY field is, look back at your phpmyadmin page, click on the 'Structure' tab and at the bottom of the page under 'Indexes' it will show you which 'Field' has a 'Keyname' value 'PRIMARY'.
Kind of a long way around, but if you don't want to deal with markup and just need to duplicate a single row there you go.
I used Leonard Challis's technique with a few changes:
As a temp table, there should never be more than one record, so you don't have to worry about the primary key. Setting it to null allows MySQL to choose the value itself, so there's no risk of creating a duplicate.
If you want to be super-sure you're only getting one row to insert, you could add LIMIT 1 to the end of the INSERT INTO line.
Note that I also appended the primary key value (1 in this case) to my temporary table name.
Sorry for the necropost but this is what I turned up with google and since I found this helpful but problematic I wanted to contribute an important modification for anyone else who digs this up.
First off, I'm using SQL Server, not MySQL, but I think it should work similarly. I used Leonard Challis' solution because it was simplest and met the need, however there's a problem with this - if you simply take the PK and increment it by 1 then what happens if you've added other records since the row in question was added. I decided it was best to just let the system handle the autoincrementing of the PK, so I did the following:
I believe this would work similarly in MySQL, but I can't test this, sorry
Some of the following was gleaned off of this site. This is what I did to duplicate a record in a table with any number of fields:
This also assumes you have an AI field at the beginning of the table
I used Grim's technique with a little change: If someone looking for this query is because can't do a simple query due to primary key problem:
With my MySql install 5.6.26, key isn't nullable and produce an error:
So after create temporary table I change the primary key to a be nullable.