Mysql LOAD statement Batch Data Input

Source: Internet
Author: User

Fortunately, MySQL provides some methods for batch data entry, making it easy to add data to a table. This section and the next section describe these methods. This section describes the SQL language-level solutions.

1. Basic syntax

Syntax: load data [LOCAL] INFILE 'file_name.txt '[REPLACE | IGNORE] into table tbl_name the load data infile statement reads a TABLE from a text file at a high speed. If you specify the LOCAL keyword, read the file from the client host. If LOCAL is not specified, the file must be on the server. (LOCAL is available in MySQL 3.22.6 or a later version .)

For security reasons, when reading text files on the server, the files must be in the database directory or can be read by everyone. In addition, to use load data infile for files on the server, you must have the file permission on the server host. See Chapter 7 database security.

The REPLACE and IGNORE keywords control repeated processing of existing unique key records. If you specify REPLACE, the new row replaces the existing row with the same unique key value. If you specify IGNORE, skip the input of duplicate rows of existing rows with a unique key. If you do not specify any option, an error occurs when the duplicate key is found and the remaining part of the text file is ignored.

If you use the LOCAL keyword to load data from a LOCAL file, the server cannot stop file transmission during the operation. Therefore, the default behavior is as if IGNORE was specified.

2. File Search principles

When searching for files on the server host, the server uses the following rules:

If an absolute path name is provided, the server uses this path name.

If a relative path name of one or more front parts is given, the server searches for files in the data directory relative to the server.

If a file name without a front-end component is provided, the server searches for a file in the database directory of the current database.

Note that these rules mean that a file such as "./myfile.txt" is read from the data directory of the server and written as "Prepare myfile.txt". A file is read from the database directory of the current database. Note which of the following statements read the db1 file from the database directory instead of db2:

mysql> USE db1;mysql> LOAD DATA INFILE "./data.txt" INTO TABLE db2.my_table;

3. Syntax of FIELDS and LINES clauses

If you specify a FIELDS clause, each of its clauses (terminated by, [OPTIONALLY] enclosed by and escaped by) is optional, except that you must specify at least one of them.

If you do not specify a FIELDS clause, the default value is the same as if you write this statement:

Fields terminated by '\ t' enclosed by ''escaped '\\'

If you do not specify a LINES clause, the default value is the same as if you write this statement:

Lines terminated by '\ N'

In other words, when the default value causes reading input, load data infile performs as follows:

Search for line boundary at line breaks

Split the row into fields at the location Operator

Do not expect fields to be enclosed by any quotation marks

The delimiters, linefeeds, or "\" starting with "\" are part of the literal characters of the field value.

Load data infile can be used to read files obtained from external sources. For example, a file in dBASE format will have fields separated by commas and enclosed by double quotation marks. If the row in the file is terminated by a line break, the command shown below describes the fields and row processing options you will use to load the file:

mysql> LOAD DATA INFILE 'data.txt' INTO TABLE tbl_nameFIELDS TERMINATED BY ',' ENCLOSED BY '"'LINES TERMINATED BY '\n';

Any field or line processing option can specify an empty string (''). If it is not null, the value of FIELDS [OPTIONALLY] enclosed by and fields escaped by must be a single character. Fields terminated by and lines terminated by can be more than one character. For example, write a line terminated by the carriage return or read a file containing such a line and specify a line terminated by '\ r \ n' clause.

FIELDS [OPTIONALLY] enclosed by control field surrounded BY characters. For output (SELECT... into outfile), if OPTIONALLY is omitted, all fields are surrounded by enclosed by characters. An example of such output (using a comma as the field separator) is shown below:

"1", "a string", "100.20"

"2", "a string containing a, comma", "102.20"

"3", "a string containing a \" quote "," 102.20"

"4", "a string containing a \", quote and comma "," 102.20"

If you specify OPTIONALLY, the enclosed by character is only used to enclose the CHAR and VARCHAR fields:

1, "a string", 100.20

2, "a string containing a, comma", 102.20

3, "a string containing a \" quote ", 102.20

4, "a string containing a \", quote and comma ", 102.20

Note that the enclosed by character in a field value is escaped by using the escaped by character as its prefix. Note that if you specify an empty escaped by value, the output may not be correctly read BY the load data infile. For example, if the escape character is empty, the output shown above is as follows. Note that the second field in the fourth row contains a comma following the quotation marks. It (incorrectly) seems to end the field:

1, "a string", 100.20

2, "a string containing a, comma", 102.20

3, "a string containing a" quote ", 102.20

4, "a string containing a", quote and comma ", 102.20

Fields escaped by controls how to write or read special characters. If the fields escaped by character is not empty, it is used to prefix the following characters on the output:

Fields escaped by character

FIELDS [OPTIONALLY] enclosed by character

The first character of fields terminated by and lines terminated by values.

ASCII 0 (in fact, the subsequent escape characters are written as '0' rather than a zero-value byte)

If the fields escaped by character is empty, no character is ESCAPED. It may not be a good idea to specify an empty escape character, especially if the field value in your data contains any character in the table just given.

For input, if the fields escaped by character is not empty, the appearance of this character is stripped and subsequent characters are literally part of the field value. The exception is an escape "0" or "N" (that is, \ 0 or \ N, if the escape character is "\"). These sequences are interpreted as ASCII 0 (a zero-value byte) and NULL. See the following rules for NULL processing.

Summary

Loading data for the database is one of the important responsibilities of the Administrator. Because of the importance, MySQL provides a wide range of methods. The main examples are listed in this section:

1. Use INSERT and REPLACE statements

2. Use INSERT/REPLACE... SELECT statement

3. Use the load data infile statement

4. Use the utility mysqlimport

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.