Reputation: 9692
Is there an easy way to SELECT a row and INSERT it into another table that has exactly the same columns? It needs to reserve the id and be able to move it back later on.
STEPS:
Move entire row to an identical temporary table (with a different name of-course) while using the same id.
Delete the row from old table (I know this, dont worry ;))
Move the row back from the temporary table into the original table, while using the same original id.
Delete row from temporary table.
I know how to do this, but there are probably much better, cleaner and faster ways to achieve this in one or two queries. My main concern is that when the table gets refactored, the query should still do its job without skipping newer columns.
I hope someone can come with a good suggestion :)
Upvotes: 1
Views: 1045
Reputation: 3368
Assuming the tables have identical columns, you should be able to just do this:
INSERT INTO newtable SELECT * FROM oldtable WHERE id = somevalue
Upvotes: 4
Reputation: 87
You can do this :
INSERT INTO tbltemp (Select * from table1 where someid=somevalue)
Or you could move the entire table into other one :
INSERT INTO tbltemp (select * from table1);
What ever you do, be careful that the record/records which you need to move/copy must not include any primary key values which are already there in target table.
Upvotes: 0
Reputation: 7187
You use CREATE TABLE ... SELECT
to create a table and then work with it. This will create a table with columns from SELECT
in your case the original table. Then you can delete the rows from original table, move the rows back to the original table using INSERT INTO ... SELECT
followed by dropping the temporary table. This way you don't need to maintain two tables and the temporary table will always get the latest columns from the original table.
See mysql manual. Following example copied from manual.
mysql> SELECT * FROM foo;
+---+
| n |
+---+
| 1 |
+---+
mysql> CREATE TABLE bar (m INT) SELECT n FROM foo;
Query OK, 1 row affected (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM bar;
+------+---+
| m | n |
+------+---+
| NULL | 1 |
+------+---+
1 row in set (0.00 sec)
So you need something like this
CREATE TABLE bar SELECT * FROM foo WHERE fooColumn = 'whatever';
DELETE FROM foo where fooColumn <> 'whatever';
INSERT INTO foo SELECT * FROM bar;
DROP TABLE bar
if you want to avoid creating and dropping tables too often, just create the temporary table once.
INSERT INTO bar SELECT * FROM foo WHERE fooColumn = 'whatever';
DELETE FROM foo where fooColumn <> 'whatever';
INSERT INTO foo SELECT * FROM bar;
But make sure you keep the temporary and the original table in sync all the time i.e. if you add or drop a column in the original table, make sure that is replicated in the temporary table immediately as INSERT INTO ... SELECT
only copies data not schema.
Upvotes: 1
Reputation: 13
Here are two alternate ideas I can think of:
Aside from the initial schema change required by option 1, both options will work regardless of any changes to the schema of the original table. If you are trying to avoid maintaining the second table, I'd consider these.
If you do wish to copy to the holding table and delete from the original table, and later copy back to the original table, and delete from the holding table, be sure to wrap those operations into a transaction so that you don't end up with duplicated data or data loss in the event of things not going as planned.
Upvotes: 1