views:

129

answers:

3

Hi all,

My company's currently moving our databases around, shifting one set of tables out from the old MySQL instance into the new. We've done some development prior to this migration, and some tables' structure has been altered from the original (eg. columns were dropped).

So currently I've dumped the data from the old database and am now attempting to reinsert them into the new table. Of course, the import borks when it tries to insert rows with more fields than the table has.

What's the best way (preferably scriptable, because I foresee myself having to do this a few more times) to import only the fields I need into the new table?

A: 

If you're using MySQL 5.1, a powerful, although maybe in this case overkill, solution is to do an xml mysqldump and use an XSLT to transform it. Unfortunately re-importing that xml file isn't supported in 5.0, you'll need 5.1, 5.4, or 6.0

Jeff Mc
A: 

First of all, create new database with old structure, or temp tables in current database. Then run script with insert statements for each row, but in values must be only those fields that are in new structure.

insert into newTable select row1,row2 from tempTable
x2
+1  A: 

Update the following to suit:

SELECT 'INSERT INTO NEW_TABLE ... ('+ to.column +');'
  FROM OLD_TABLE ot

You need an INSERT statement for the table on the new database, with column list. Then populate the value portion accordingly based on the values in the old table. Run in the old environment, and you'll have your inserts with data for the new environment - just copy'n'paste into a script.

Mind though that datatypes have to be handled accordingly - dates (incl. time), and strings will have to be handled because you're dealing in text.

OMG Ponies