搜尋
首頁資料庫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.

Copying Data from One MySQL Database to Another

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.

SQL Statement

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

mysqldump

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 id="phpMyAdmin">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 id="Importing-Data-from-Other-Databases">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 id="MySQL-Workbench">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 id="Navicat-Premium">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 id="Setting-up-SQLite">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
    1. SQLite comes pre-installed on Mac OS Leopard or later, but you can update it if you need to, using Homebrew .
  1. Start up the Database Server


    At the shell or DOS prompt, enter “sqlite3” followed by the database name of “applicants_copy.db”.  That will start up the database server, create the new schema, and set it as the working schema.  You should see the version information and sqlite> prompt:
    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>

Connecting To your Databases

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:

  1. Click the Connection button on the far top-left of the application window and select MySQL from the dropdown menu:

    Connection Menu Figure 1: Connection Menu

  1. On the New Connection screen:
    1. Give your connection a good descriptive name.
    2. By default, the MySQL server listens for connections on port 3306.  Don’t change this unless you need to.
    3. Supply credentials for an account that possesses table modification rights.
    4. You can verify your information by clicking the Test Connection button. If it comes back with a “Connection successful!” message, you can either go to another tab to enter more specialized connection information or simply hit the OK button to save your info.

The New Connection Dialog Figure 2: The New Connection Dialog

  1. To use your credentials to establish a connection to your database, either select File > Open Connection or right-click on your connection in the list under the Connection button and select Open Connection from the popup menu:

Open Connection Command

Figure 3: Open Connection Command

That will give you access to all the databases running on that server.

  1. Once again, access the Connection dropdown menu on the far top-left of the application window and select SQLite from the list.
  2. That will open the New Connection dialog specific to SQLite.  The Connection Name field is the only one that it has in common with the MySQL connection properties. SQLite is file-based, so we can open an existing one or create a new version 2 or 3 database.  Since we did create a new database at the command prompt, there should be a file under the SQLite root directory.

SQLite - New Connection Dialog

Figure 4: SQLite - New Connection Dialog

  1. Click OK to close the dialog and open the connection. You should now see the Applicants Copy database in the Connections list.
  2. Connect to the Applicants Copy database. Tip: Besides the methods mentioned above in point 2, you can also open a database connection by double-clicking it in the Connections list.

Create the Applicant MySQL Table

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');

Import the Applicants Table from MySQL to SQLite

Follow these steps in Navicat Premium to migrate the applicants table from MySQL into the SQLite database:

  1. Select Tools > Data Transfer... from the main menu to launch the Data Transfer wizard.
  2. On the General tab of the Data Transfer dialog, begin by selecting the Connection and Database of the data source.  That will populate the Database Objects tree with all of the Tables, Views, Procedures, Functions, and Events that you can choose from.  But note that objects that do not exist in the database cannot be selected.  Tip: Selecting the Connection and Database in the Connections list before launching the Data Transfer wizard will automatically populate them in the dialog.
  3. Check the box beside the applicants table to import it.
  4. Select the Applicants Copy SQLite Connection as the Target for the import.  If you have not explicitly added databases to the connection using the SQLite ATTACH DATABASE statement, you will only have one database called “main”.

Data Transfer Dialog - General Tab Figure 5: Data Transfer Dialog - General Tab

Saving Data to a Text File

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.

Data Transfer Dialog with File Target Figure 6: Data Transfer Dialog with File Target

  1. Set advanced options.

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:

    • Create tables
      Creates tables in the target database and/or schema if required.
    • Include indexes
      Imports indexes on the table.
    • Include foreign key constraints  
      Imports foreign keys on the table.
    • Convert object name to
      Performs a transformation on object names by converting them to Lower case or Upper case during the import process.
    • Insert records  
      Transfers all records to be to the destination database and/or schema.  Conversely, leaving this option unchecked will not add any data to the destination database and/or schema.
    • Lock source tables
      Locks the tables in the source database and/or schema so that update on the table is not allowed once the data transfer has begun.
    • Lock target tables
      Locks the tables in the target database and/or schema during the data transfer process so that no other rows may be appended.
    • Use transaction
      Check this option to use transactions during the data transfer process.
    • Rows may be added using either complete or extended insert statements.
      The Use complete insert statements

      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');
    • The Use extended insert statements 

      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');
  • Run multiple insert statements
    Makes the data transfer process faster by running multiple insert statements in each execution.
  • Use hexadecimal format for BLOB 
    Inserts BLOB data in hexadecimal format.
  • Continue on error
    Ignores errors that are encountered during the transfer process so that errors on one record won’t cause the entire import process to be aborted.
  • Drop target objects before create
    If database objects already exist in the target database and/or schema, the existing objects will be deleted before the new one is created.
  • Create target database/schema if not exist
    Creates a new database/schema if the database/schema specified in target server does not exist.

Data Transfer Dialog - Advanced Tab for SQLite Figure 7: Data Transfer Dialog - Advanced Tab for SQLite

  1. Click the Start button to perform the import. 

    Progress will be relayed on the Message Log tab.

Data Transfer Dialog - Message Log Tab Figure 8: Data Transfer Dialog - Message Log Tab

Saving Import Settings

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:

Save Data Transfer Profile Dialog 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.

Data Transfer Dialog - Profiles Tab Figure 10: Data Transfer Dialog - Profiles Tab

  1. Click the Close button to dismiss the Data Transfer dialog and return to the main application.
  2. Refreshing the schema list using the F5 key should now show the new applicants table in the main database of the SQLite Applicants Copy schema.

Imported applicants Table in SQLite Database Figure 11: Imported applicants Table in SQLite Database

Conclusion

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

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
MySQL:世界上最受歡迎的數據庫的簡介MySQL:世界上最受歡迎的數據庫的簡介Apr 12, 2025 am 12:18 AM

MySQL是一種開源的關係型數據庫管理系統,主要用於快速、可靠地存儲和檢索數據。其工作原理包括客戶端請求、查詢解析、執行查詢和返回結果。使用示例包括創建表、插入和查詢數據,以及高級功能如JOIN操作。常見錯誤涉及SQL語法、數據類型和權限問題,優化建議包括使用索引、優化查詢和分錶分區。

MySQL的重要性:數據存儲和管理MySQL的重要性:數據存儲和管理Apr 12, 2025 am 12:18 AM

MySQL是一個開源的關係型數據庫管理系統,適用於數據存儲、管理、查詢和安全。 1.它支持多種操作系統,廣泛應用於Web應用等領域。 2.通過客戶端-服務器架構和不同存儲引擎,MySQL高效處理數據。 3.基本用法包括創建數據庫和表,插入、查詢和更新數據。 4.高級用法涉及復雜查詢和存儲過程。 5.常見錯誤可通過EXPLAIN語句調試。 6.性能優化包括合理使用索引和優化查詢語句。

為什麼要使用mysql?利益和優勢為什麼要使用mysql?利益和優勢Apr 12, 2025 am 12:17 AM

選擇MySQL的原因是其性能、可靠性、易用性和社區支持。 1.MySQL提供高效的數據存儲和檢索功能,支持多種數據類型和高級查詢操作。 2.採用客戶端-服務器架構和多種存儲引擎,支持事務和查詢優化。 3.易於使用,支持多種操作系統和編程語言。 4.擁有強大的社區支持,提供豐富的資源和解決方案。

描述InnoDB鎖定機制(共享鎖,獨家鎖,意向鎖,記錄鎖,間隙鎖,下一鍵鎖)。描述InnoDB鎖定機制(共享鎖,獨家鎖,意向鎖,記錄鎖,間隙鎖,下一鍵鎖)。Apr 12, 2025 am 12:16 AM

InnoDB的鎖機制包括共享鎖、排他鎖、意向鎖、記錄鎖、間隙鎖和下一個鍵鎖。 1.共享鎖允許事務讀取數據而不阻止其他事務讀取。 2.排他鎖阻止其他事務讀取和修改數據。 3.意向鎖優化鎖效率。 4.記錄鎖鎖定索引記錄。 5.間隙鎖鎖定索引記錄間隙。 6.下一個鍵鎖是記錄鎖和間隙鎖的組合,確保數據一致性。

MySQL查詢性能差的常見原因是什麼?MySQL查詢性能差的常見原因是什麼?Apr 12, 2025 am 12:11 AM

MySQL查询性能不佳的原因主要包括没有使用索引、查询优化器选择错误的执行计划、表设计不合理、数据量过大和锁竞争。1.没有索引导致查询缓慢,添加索引后可显著提升性能。2.使用EXPLAIN命令可以分析查询计划,找出优化器错误。3.重构表结构和优化JOIN条件可改善表设计问题。4.数据量大时,采用分区和分表策略。5.高并发环境下,优化事务和锁策略可减少锁竞争。

您什麼時候應該使用複合索引與多個單列索引?您什麼時候應該使用複合索引與多個單列索引?Apr 11, 2025 am 12:06 AM

在數據庫優化中,應根據查詢需求選擇索引策略:1.當查詢涉及多個列且條件順序固定時,使用複合索引;2.當查詢涉及多個列但條件順序不固定時,使用多個單列索引。複合索引適用於優化多列查詢,單列索引則適合單列查詢。

如何識別和優化MySQL中的慢速查詢? (慢查詢日誌,performance_schema)如何識別和優化MySQL中的慢速查詢? (慢查詢日誌,performance_schema)Apr 10, 2025 am 09:36 AM

要優化MySQL慢查詢,需使用slowquerylog和performance_schema:1.啟用slowquerylog並設置閾值,記錄慢查詢;2.利用performance_schema分析查詢執行細節,找出性能瓶頸並優化。

MySQL和SQL:開發人員的基本技能MySQL和SQL:開發人員的基本技能Apr 10, 2025 am 09:30 AM

MySQL和SQL是開發者必備技能。 1.MySQL是開源的關係型數據庫管理系統,SQL是用於管理和操作數據庫的標準語言。 2.MySQL通過高效的數據存儲和檢索功能支持多種存儲引擎,SQL通過簡單語句完成複雜數據操作。 3.使用示例包括基本查詢和高級查詢,如按條件過濾和排序。 4.常見錯誤包括語法錯誤和性能問題,可通過檢查SQL語句和使用EXPLAIN命令優化。 5.性能優化技巧包括使用索引、避免全表掃描、優化JOIN操作和提升代碼可讀性。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。