tags:

views:

24

answers:

2

Using MySQL I can run the query:

SHOW CREATE TABLE MyTable;

And it will return the create table statement for the specificed table. This is useful if you have a table already created, and want to create the same table on another database.

Is it possible to get the insert statement for an already existing row, or set of rows? Some tables have many columns, and it would be nice for me to be able to get an insert statement to transfer rows over to another database without having to write out the insert statement, or without exporting the data to CSV and then importing the same data into the other database.

Just to clarify, what I want is something that would work as follows:

SHOW INSERT Select * FROM MyTable WHERE ID = 10;

And have the following returned for me:

INSERT INTO MyTable(ID,Col1,Col2,Col3) VALUES (10,'hello world','some value','2010-10-20');
A: 

Since you copied the table with the SQL produced by SHOW CREATE TABLE MyTable, you could just do the following to load the data into the new table.

INSERT INTO dest_db.dest_table SELECT * FROM source_db.source_table;

If you really want the INSERT statements, then the only way that I know of is to use mysqldump http://dev.mysql.com/doc/refman/5.1/en/mysqldump.htm. You can give it options to just dump data for a specific table and even limit rows.

Rob Prouse
The databases in question exist on physically different machines, on different networks, so this method wouldn't work for me, but works fine for databases running on the same instance of MySQL
Kibbee
Are you trying to programatically move subsets of data between machines? Couldn't you slave the second machine to mirror data, then move your subsets between databases on the slave?
Rob Prouse
Let's, for argument's sake say I have a table that contains Time Zones. Now, for some reason a new time zone is created (perhaps with a 15 minute offset or something like that). So I add the new row to the development database that contains all the information about the new timezone and do all the necessary testing. When it comes time to move that timezone information to the production database I have to create an insert statement from scratch, or do a export/import. It would be nice to be able to just get the insert, and then include it in the scripts to bring the new timezone live.
Kibbee
I don't have the original insert because I may have inserted it using a GUI tool, or I may have had to change it 3 times from the original values I inserted.
Kibbee
+1  A: 

I use the program SQLYOG where I can make a select query, point atscreenshot the results and choose export as sql. This gives me the insert statements.

Henrik