MySQL data import export methods and tools introduction (2-import from SQL files)

Source: Internet
Author: User
Tags command line insert log mysql mysql in access database mysql command line mysql database
mysql| data
MySQL data import export methods and tools introduction (2-import from SQL files)
Batch import file, import data from SQL file into database

Translation statement:
This article is part of a book from Sam's Teach yourself MySQL in and by Mark Maslakowski
English original version ownership of the original author, the Chinese part of the translation has a slight additions and deletions, the original book is too clear where there is a deletion, not clear where there is an increase, if there is improper translation or incorrect place, please correct me.

Translator: David Euler,scu. de_euler-david@www.yahoo.com.cn
Time: 2004/04/24 in Sichuan



Batch processing is a way to run a MySQL program that is not interactive, and you will still use these commands, just as you would for the commands you use in MySQL.

In order to implement batch processing, you redirect a file to a MySQL program, first we need a text file that contains the same text as the command we entered in MySQL.
For example, we want to insert some data, use the file containing the following text (file name New_data.sql, of course, we can also be named New_data.txt and any other legitimate names, not necessarily after the end of the suffix SQL):
Use Meet_a_geek;
INSERT into Customers (customer_id, last_name) VALUES (NULL, "block");
INSERT into Customers (customer_id, last_name) VALUES (NULL, "Newton");
INSERT into Customers (customer_id, last_name) VALUES (NULL, "Simmons");
Note that the syntax of the above sentences must be correct, and each sentence ends with a semicolon.
The use command above selects the database and inserts the data into the Insert command.

Below we will import the above file into the database, before importing to confirm that the database is already running, that is, the mysqld process (or service, Windows NT hereinafter referred to as "services", UNIX is "process") already running.
Then run the following command:
Bin/mysql–p Then press the prompt to enter the password, if the statement in the above file does not have errors, then the data is imported into the database.

The command line uses the load data INFILE to import data from a file into the database:
Now you might ask yourself, "Why on earth do I have to enter all these SQL statements into a file and then run them through the program?" ”
It looks like it's going to take a lot of work. Well, you're probably right about that. But what if you have log records from all of these commands? Now that's great, well, most databases will automatically generate log of event records in the database. Most log contains the original SQL commands that are useful. So, if you can't export data from your current database to a new MySQL database, you can use the log and MySQL batch features to quickly and easily import your data. Of course, this eliminates the trouble of typing.

LOAD DATA INFILE
This is the last way we're going to introduce the data into the MySQL database. This command is very similar to Mysqlimport, but this method can be used on the MySQL command line. That means you can use this command in all programs that use APIs. With this approach, you can import the data you want to import into your application.

Before using this command, the MYSQLD process (service) must already be running.
To start the MySQL command line:
Bin/mysql–p
Press the prompt to enter the password, after successfully entered the MySQL command line, enter the following command:
Use Meet_a_geek;
LOAD DATA INFILE "/home/mark/data.sql" into TABLE Orders;
Simply put, this will import the contents of the file Data.sql into the table orders, like the Mysqlimport tool, and there are some parameters to choose from. For example, you need to import data from your computer to a remote database server, and you can use the following command:
LOAD DATA local INFILE "C:\MyDocs QL.txt" into TABLE Orders;

The local parameter above indicates that the file is a native file and the server is the server you are logging on to.
This eliminates the use of FTP to upload files to the server, MySQL for you to complete.
You can also set the priority of the INSERT statement, if you want to mark it as low priority (low_priority), then MySQL will wait until no one else reads the table to insert the data. You can use the following command:
LOAD DATA low_priority INFILE "/home/mark/data.sql" into TABLE Orders;

You can also specify whether to replace or ignore duplicate key values in the file and datasheet when inserting data. Syntax to override duplicate key values:
LOAD DATA low_priority INFILE "/home/mark/data.sql" REPLACE into TABLE Orders;
The above sentence looks a bit clumsy, but puts the keyword in the place where your profiler can understand it.

The following pair of options describes the file's record format, which is also available in the Mysqlimport tool. They look a little different here. First, to use the fields keyword, if you use this keyword, the MySQL profiler would like to see at least one of the following options:
Terminated by character
Enclosed by character
Escaped by character
These keywords are the same as their parameters and their usage in Mysqlimport. The
Terminated by describes the separator for the field, which is the tab character by default (\ t)
Enclosed by describes the enclosing character of the field. For example, enclose each field in quotation marks.
Escaped by description of the escape character. The default is the Counter bar (backslash:\).
The following example of the previous Mysqlimport command is still used to import the same file into the database using the load DATA infile statement:
The LOAD DATA INFILE "/home/mark/orders.txt" REPLACE into the TABLE Orders FIELDS terminated by ', ' enclosed by ' ";


There is no feature in a Mysqlimport tool in the LOAD DATA infile statement:
The LOAD DATA INFILE can import files into the database according to the specified columns.
This feature is important when we want to import part of the data into the content. For example, when upgrading from an Access database to a MySQL database, we need to add some columns (column/field/field) to the MySQL database to accommodate some additional requirements.
At this point, the data in our Access database is still available, but because the column (field) of the data is no longer matched in MySQL, the Mysqlimport tool cannot be used again. Nevertheless, we can still use the load data INFILE, and the following example shows how to import data into a specified column (field):
LOAD DATA INFILE "/home/order.txt" into TABLE Orders (Order_number, order_date, customer_id);

As you can see, we could specify the required columns (fields). These specified fields are still enclosed in parentheses, separated by commas, and if you omit any of them, MySQL will remind you ^_^

Importing data from Microsoft access (imports from access, slightly)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.