Reputation: 71
Is it possible to migrate related data but with different column names from one database to another database? and im talking about very large amount of data here. someone has an idea? I have tried exporting it to CSV and importing to the otherdatabase but i get errors saying:
invalid column count in CSV input in line 1
So if anyone has an efficient and effective way of doing this please share. Or if anyone can guide mo through this CSV mapping in excell on how to properly do it, i would really appreciate
Upvotes: 7
Views: 34530
Reputation: 31
Beware!!!
INSERT INTO dbname.tablename(fields...);
SELECT * FROM olddatabase.oldtable;
will only work if id's follow.
Select fields ommitting ID so that auto-increment can give a new ID to the imported rows.
Upvotes: 0
Reputation: 21047
There are two cases in this:
You just need to insert the data in the destination table:
insert into dbDestination.tblDestination (field1, field2, ...)
select ...
from dbSource.tblSource
Notes
select
statement must include the fields you need to copy to the destination table.select
statement must be in the same order as the fields specified in the field list in the insert
portionI would export the data to a plain text file, and then import it. I personally prefer .csv files, but it's up to you.
You have two possibilities: To use select... into outfile
or to use the system terminal (command window).
a. Using select... into outfile
and load data
In the server where dbSource
is:
select ...
from dbSource.tblSource
into outfile [your destination file]
...
Copy the file to the destination server.
In the server where dbDestination
is:
load data local infile [your file] ...
Notes
select
statement must be in the same order as the fields specified in the field list in the insert
portionselect... into outfile
and load data...
b. Using the terminal
In Linux (or other Unix systems), open a terminal window and enter the following command:
$ mysql -h [sourceServer] -u [yourUser] -p[yourPassword] dbSource -e"select ..." | sed 's/\t/,/g' > yourDestinationFile.csv
Copy the file to the destination server
Start MySQL console, and use `load data ...``
Notes
select
statement must be in the same order as the fields specified in the field list in the insert
portion| sed 's/\t/,/g'
part converts the output of the mysql query to a .csv file. You can use another separator instead of ,
. For further reference about sed
check http://lowfatlinux.com/linux-sed.htmlignore 1 lines
at the end of the load data...
sentence.To copy data from one database to another is a very simple task. Hope this points you in the right direction.
A word of advice: Download the reference manual for your MySQL version and keep it at hand. You can find most of your solutions there.
Upvotes: 12
Reputation: 1
Use this:
INSERT INTO dbname.tablename(fields...);
SELECT * FROM olddatabase.oldtable;
Upvotes: 0
Reputation: 189
A simple way to do this is with the following syntax:
INSERT INTO Database.Table(field 1, field 2,...)
SELECT * FROM Database2.Table2;
Upvotes: 1