0

I have a website with a very old and badly designed database (MySQL). I'm creating a new version of the website and I have defined another database (MySQL) which is of course better designed (normalized e.a.)

Now I have the problem of transferring the data of the old database to the new database. I cannot use mysqldump as the layout of the tables and references e.a. are completely different, although some of the attributes are the same.

So my question is what is the best practices to this kind of work. E.g.

A table Customer with attributes name, street,something unimportant to new Customer table with attributes name,street,something new and important

What about tables with Foreign Keys e.a. ?

Kind regards

Jens Buysse

1
  • 1
    If you've not been careful enough to start from current structure and keep the ALTER TABLE statements, you'll need to code a custom data migration script. No standard third-party tool will do it for you. Commented Nov 4, 2016 at 10:20

3 Answers 3

1

There's not really well defined method of doing this - it's more of a manual task, defining how to transform the data. A form of ETL (Extract, Transform and Load) as it were.

Start with your base tables and write some insert statements to get the core data in there, transformed as applicable. Then move onto the "next level" of related data and repeat until all of your info is copied over.

Note that often times the new, ideal schema cannot be fully implemented (with all constraints, etc) with the legacy database as some pieces of information may be missing. In these cases you have to either a) generate the data as required during the load, or b) turn off constraints so you can load the legacy data until such data is cleaned up enough to enforce them.

All in all, it's a very per-scenario type of thing that tends to be handled manually in most cases. To the best of my knowledge, anyway.

Finally, it's been a while since I worked with MySQL, but most database engines allow for two-part naming of some sort, so you can access two separate databases (on the same server) from within a single script, so your insert statements might end up looking like this:

INSERT INTO newdb.newtable (thisfield, thatfield) 
SELECT thisfield, thatfield FROM olddb.oldtable;

(with applicable transformations applied, of course)

Sign up to request clarification or add additional context in comments.

Comments

1

Start from old sql script and remove manually all the unimportant columns that not exist in new db structure:

INSERT INTO customers ('name','surname','age') VALUES ('john','brown', 27);

become

INSERT INTO customers ('name','surname') VALUES ('john','brown');

Assign a default value to new columns inserted in new db and remove this default value after executing insert script if needed.

Use some editors, copy/replace generated script to make it simple. Or copy old tables in temporary tables and select required fields from them to insert data into new tables.

INSERT INTO customer (
    SELECT 'name','surname' FROM customers_old
)

An automated approach in this case is difficult to find, since db structures are made by you and manual approach is better here in my opinion.

1 Comment

Does MySQL have the INSERT INTO thisdb.thistable (fields) SELECT fields FROM thatdb.thattable syntax? (it's been years since I've used it)
0

you can probably write a program in language of your choice to move data from 1 DB (table) to other with only required column data to be moved. you can find a lot of example for same on web

To handle foreign key scenario you can move data from the column that due to normalization needs to be moved to some other table, you can handle this with the program itself as program will work on each entry of the row so you are free to move each entry in the row to whatever table you want (or maintain a temporary excel sheet where each sheet represent a table and add data to this excel as needed ) then add data to your newly created table schema from this excel by importing it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.