Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have three relational tables with data in MYSQL db1, and the same schemas with data in MYSQL db2, I need to combine the data into MYSQL db2. what's the right way to do it?
should I dump the db1 tables individually (with the option of adding a high number to the ids so that the data don't conflict)?

share|improve this question
up vote 1 down vote accepted

You can simply append the data by doing something like this:

INSERT INTO `db2`.`table` SELECT NULL, `col2`, `col3` FROM `db1`.`table`

This assumes that the schema is so that the first column is the auto incrementing ID. By selecting NULL for that, new keys will be generated, all other columns should be selected separately and will form the rest of these new records.

If you order the select from the above statement by the ID and they are contiguous, you can simply add the first inserted ID value to the foreign keys for other tables.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.