CREATE TABLE testtable (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO testtable (name) VALUES ('data1');
In the first row ‘id’ will be ‘1’
Now I want to copy that row so I get a second row, with all the data the same as the first row, but it should have id ‘2’
This is what I was thinking of :
INSERT INTO testtable SELECT * FROM testtable WHERE ID=1 ;
But this gives an error, because it tries to insert a value that should be unique.
So is there a way to get around this ? To copy a row and let it have an auto-incremented value at the same time ? Thanks
Donboe : Ok, but that was just some code to show how the table was built and what could be in it.
In reality there will be about 50 columns, and they will be filled long before I have to do the part with the copy. I also don’t know exactly what columns will be used in the final version of the first part. So that’s why I would not like to do a select query on all columns of one row, and insert them back with a new id. I want to play safe, and copy everything that there is at that moment, and get a new row with a new auto-incremented id.
I’m currently testing out using a normal MEDIUMINT and decide the exact value in the php script self, so without auto-increment. But that doesnt seem perfect either.
I guess that would work. You would need to manually decide the next id by first selecting (max) id and then do +1, so auto-increment should be off. And it also requires you to know all the needed column names from the start, wich didnt really suited my specific situation. I wanted to select just everything except the id, no matter how much or little it was.