Maison >base de données >tutoriel mysql >Importing Into MySQL from Other Databases_MySQL
Your data may originate from many sources, from an application, atom feed, or even from another database. In the first two cases, you have application code and/or stored procedures processing the data; importing directly from another Database Management System (DBMS) can be accomplished using a variety of tools from command line utilities to professional grade DBMS management software. The tool(s) that you employ will depend largely on the import source(s) and target(s). As we will see here today, the easiest data transfers involve databases of the same type (i.e. MySQL to MySQL) that reside on the same server. Conversely, databases of different types are far more challenging to work with because different vendors each have their own proprietary tools and SQL extensions, making it unfeasible to achieve a seamless import process. Thankfully, there are tools that can abstract each vendor’s particular language so that data may be transferred between databases without being an expert in any one of them.
Whatever tools you use to manage your MySQL database(s), if you can execute an SQL statement, then you can copy data from one table into another so long as both databases reside on the same server. For database migrations between servers, there are tools that have cropped up over the years to help with those.
Under the best of circumstances, your databases both reside on the same network – perhaps even on the same server. Assuming that you can connect to both at the same time, that would allow you to SELECT from one and INSERT into the other. The syntax for that particular type of statement appends a SELECT clause to the INSERT:
insert into destination_database.destination_table (field1, field2, ...) select field1, field2, ... from source_database.source_table
Here’s a statement that copies all of the data from one MySQL database table to another:
INSERT INTO navicat_imports_db.applicants (first_name, id, last_name)SELECT first_name, id, last_name FROM test.applicants
Copying data one table at a time as we did above can become a tedious process. For transferring several tables at once, the mysqldump command line tool may be utilized to create .sql files that may then be read and executed on the destination database.
First, the following command is executed on the source database to dump its contents to a file:
mysqldump -u [username] -p [database_name] > [dumpfilename.sql]
A similar command is run on the target database to import the data:
mysql -u [username] -p [database_name] <p>SQL files are an ideal transfer vehicle because they can create database objects as well as populate data.</p><h3>phpMyAdmin</h3><p>For online databases, many web hosts offer phpMyAdmin to manage your MySQL databases. To migrate a database, select it in the left column list, click the Export link, and save the database to a file. Then on the new server, select the target DB in the left column, click the Import link, and choose the file you just exported. It`s a lot like running mysqldump, but from a GUI.</p><h2>Importing Data from Other Databases</h2><p>For importing data from other (i.e. non-MySQL) databases, you can either find a utility that generates .sql or XML files that you can then import into your MySQL database or you can use a database management system that has the capacity to connect to multiple heterogeneous database servers at once. We’re going to look at two such products.</p><h3>MySQL Workbench</h3><p> MySQL`s free Workbench application supports migrations from Microsoft SQL Server, PostgreSQL, Sybase ASE, Sybase SQL Anywhere, SQLite, and others. Much more than a migration utility, it also includes features like server health monitoring and SQL data modeling. Just be aware that many users have complained about MySQL Workbench being slow and sluggish (and sometimes crashing) while merely typing queries. The user interface is considered to be clunky and unintuitive as well. But most importantly, Oracle has not been keen on fixing any outstanding bugs to make this tool better since their primary focus is on the income generating behemoth that is the Oracle flagship DBMS. </p><h3>Navicat Premium</h3><p>For the management of multiple databases in a commercial environment, Navicat Premium is a far more robust solution. It`s geared towards database administrators and developers of small to medium sized businesses. Its best feature is that it allows you to connect to multiple databases simultaneously as well as migrate data between them in a seamless and consistent way. It also supports other database objects including Stored Procedures, Events, Triggers, Functions, and Views. Supported databases include MySQL, MariaDB, SQL Server, Oracle, PostgreSQL, and SQLite, among others.</p><p> The trial version of Navicat Premium for MySQL may be downloaded from the company’s website . The 30-day trial version of the software is fully functional and identical to the full version so you can get the full impression of all its features. Moreover, registering with PremiumSoft via the location 3 links gives you free email support during the 30-day trial. </p><p>After you’ve downloaded the installation program, launch it and follow the instructions on each screen of the wizard to complete the installation.</p><p>For the remainder of this article, I’m going to demonstrate how to use the Navicat Database Admin Tool to acquire data from an SQLite database. SQLite is well known for its zero-configuration, which means no complex setup is needed!</p><h2>Setting up SQLite</h2><ol start="1" type="1"> <li> <strong>Download the files</strong> <br> Go to the SQLite download page , and download the appropriate files for your operating system. For Windows, you will need the sqlite-shell-win32-*.zip and sqlite-dll-win32-*.zip zipped files. For Linux, download sqlite-autoconf-*.tar.gz from source code section. For Max OS X, download sqlite-shell-osx-x86-3080500.zip and te-analyzer-osx-x86-3080500.zip precompiled binaries. </li> <li> <strong>Unpack to a directory</strong> </li> <ol start="1" type="a"> <li> In Windows, Create a folder such as <em>C:/>sqlite</em> and unzip the two zipped files in this folder, which will extract sqlite3.def, sqlite3.dll and sqlite3.exe files. Add the new folder to your PATH environment variable. <em>Tip: save yourself some trouble by unpacking them to C:/WINDOWS/system32 or any other directory that is already in your PATH.</em> </li> <li>Enter the following commands on Linux:</li> </ol> </ol><pre class="brush:php;toolbar:false">$tar xvfz sqlite-autoconf-3071502.tar.gz $cd sqlite-autoconf-3071502 $./configure --prefix=/usr/local $make $make install
Start up the Database Server
C:/sqlite>sqlite3 applicants_copy.db SQLite version 3.8.5 2014-06-04 14:06:34 Enter ".help" for usage hints. Enter SQL statements terminated with a ";" sqlite>
To start working with your source and target databases, you must establish a connection to them using the connection manager. Let’s begin with MySQL. In Navicat Premium:
Figure 1: Connection Menu
Figure 2: The New Connection Dialog
Figure 3: Open Connection Command
That will give you access to all the databases running on that server.
Figure 4: SQLite - New Connection Dialog
Execute the following SQL statements in MySQL to create and populate a table called “applicants”.
DROP TABLE IF EXISTS `applicants`; CREATE TABLE `applicants` ( `id` int(11) NOT NULL, `last_name` varchar(100) NOT NULL, `first_name` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; INSERT INTO `applicants` VALUES ('1', 'Gravelle', 'Rob'); INSERT INTO `applicants` VALUES ('2', 'Bundy', 'Al'); INSERT INTO `applicants` VALUES ('3', 'Richard', 'Little');
Follow these steps in Navicat Premium to migrate the applicants table from MySQL into the SQLite database:
Figure 5: Data Transfer Dialog - General Tab
Selecting the File radio button produces an SQL file for a given DBMS. It’s not required but helpful to see what kind of SQL statements are being created behind the scenes.
Figure 6: Data Transfer Dialog with File Target
The Advanced tab contains additional options that pertain to the target database type selected. For example, the following options would not appear for transfers to SQLite: Lock source tables, Lock target tables, Use extended insert statements, Use delayed insert statements, Run multiple insert statements, and Create target database/schema.
Here is the full list of possible attributes:
option inserts records using complete insert syntax:
Example:
INSERT INTO `applicants` (`id`, `last_name`, `first_name`) VALUES ('1', 'Gravelle', 'Rob');INSERT INTO `applicants` (`id`, `last_name`, `first_name`) VALUES ('2', 'Bundy', 'Al');INSERT INTO `applicants` (`id`, `last_name`, `first_name`) VALUES ('3', 'Richard', 'Little');
option uses the shorter extended insert syntax:
Example:
INSERT INTO `users` VALUES ('1', 'Gravelle', 'Rob'), ('2', 'Bundy', 'Al'), ('3', 'Richard', 'Little');
Use delayed insert statements
Inserts records using DELAYED insert SQL statements such as:
INSERT DELAYED INTO `users` VALUES ('1', 'Gravelle', 'Rob');INSERT DELAYED INTO `users` VALUES ('2', 'Bundy', 'Al');INSERT DELAYED INTO `users` VALUES ('3', 'Richard', 'Little');
Figure 7: Data Transfer Dialog - Advanced Tab for SQLite
Click the Start button to perform the import.
Progress will be relayed on the Message Log tab.
Figure 8: Data Transfer Dialog - Message Log Tab
You can save all of your import settings as a profile using the Save button at the bottom of the dialog on the left-hand side. All you need to do is supply a profile name:
Figure 9: Save Data Transfer Profile Dialog
The profile name will then appear in the list on the Profiles tab so that next time you open the Data Transfer wizard you don’t have to re-enter them from scratch. Just select the saved profile and click on the Load button to repopulate all of your import settings. The Profiles tab also has a Delete button to remove profiles.
Figure 10: Data Transfer Dialog - Profiles Tab
Figure 11: Imported applicants Table in SQLite Database
There are a variety of ways to import data into your MySQL databases, from command line utilities to professional grade GUI applications like Navicat Premium. Which you decide to go with ultimately depends on the size of your business. A home-made WordPress blog likely won’t be migrating data on the same scale as a mid-sized corporation. That being said, a tool like Navicat Premium isn’t out of reach to smaller users because there are non-commercial licenses available for all three of the big supported operating systems. The Non-Commercial Edition for Windows is $299.00 USD compared with $699.00 USD for the commercial one. For Mac (version 11) and Linux (version 11), the non-commercial license sells for $299.00 USD as well, while the commercial license goes for $599.00 USD.
See all articles by Rob Gravelle