13.2.7 LOAD DATA Statement语句

LOAD DATA
    [LOW_PRIORITY | CONCURRENT] [LOCAL]
    INFILE 'file_name'
    [REPLACE | IGNORE]
    INTO TABLE tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    [CHARACTER SET charset_name]
    [{FIELDS | COLUMNS}
        [TERMINATED BY 'string']
        [[OPTIONALLY] ENCLOSED BY 'char']
        [ESCAPED BY 'char']
    ]
    [LINES
        [STARTING BY 'string']
        [TERMINATED BY 'string']
    ]
    [IGNORE number {LINES | ROWS}]
    [(col_name_or_user_var
        [, col_name_or_user_var] ...)]
    [SET col_name={expr | DEFAULT}
        [, col_name={expr | DEFAULT}] ...]

The LOAD DATA statement reads rows from a text file into a table at a very high speed. LOAD DATA语句以非常高的速度将文本文件中的行读取到表中。The file can be read from the server host or the client host, depending on whether the LOCAL modifier is given. 文件可以从服务器主机或客户机主机读取,这取决于是否给定了LOCAL修饰符。LOCAL also affects data interpretation and error handling.LOCAL也会影响数据解释和错误处理。

LOAD DATA is the complement of SELECT ... INTO OUTFILE. LOAD DATASELECT ... INTO OUTFILE的补充。(See Section 13.2.10.1, “SELECT ... INTO Statement”.) (请参阅第13.2.10.1节,“SELECT ... INTO进入语句”。)To write data from a table to a file, use SELECT ... INTO OUTFILE. 要将数据从表写入文件,请使用SELECT ... INTO OUTFILETo read the file back into a table, use LOAD DATA. 要将文件读回表中,请使用LOAD DATAThe syntax of the FIELDS and LINES clauses is the same for both statements.对于这两个语句,FIELDSLINES子句的语法是相同的。

The mysqlimport utility provides another way to load data files; it operates by sending a LOAD DATA statement to the server. mysqlimport实用程序提供了另一种加载数据文件的方法;它通过向服务器发送LOAD DATA语句进行操作。See Section 4.5.5, “mysqlimport — A Data Import Program”.请参阅第4.5.5节,“mysqlimport-数据导入程序”

For information about the efficiency of INSERT versus LOAD DATA and speeding up LOAD DATA, see Section 8.2.5.1, “Optimizing INSERT Statements”.有关INSERTLOAD DATA的效率以及加速LOAD DATA的信息,请参阅第8.2.5.1节,“优化INSERT语句”

Non-LOCAL Versus LOCAL Operation非本地操作与本地操作

The LOCAL modifier affects these aspects of LOAD DATA, compared to non-LOCAL operation:与非LOCAL操作相比,LOCAL修饰符影响LOAD DATA的这些方面:

LOCAL works only if the server and your client both have been configured to permit it. 只有在服务器和客户端都配置为允许的情况下,LOCAL才起作用。For example, if mysqld was started with the local_infile system variable disabled, LOCAL produces an error. 例如,如果mysqld启动时禁用了local_infle系统变量,则LOCAL会产生一个错误。See Section 6.1.6, “Security Considerations for LOAD DATA LOCAL”.参见第6.1.6节,“本地负载数据的安全注意事项”

Input File Character Set输入文件字符集

The file name must be given as a literal string. 文件名必须以文本字符串形式给出。On Windows, specify backslashes in path names as forward slashes or doubled backslashes. 在Windows上,将路径名中的反斜杠指定为正斜杠或双反斜杠。The server interprets the file name using the character set indicated by the character_set_filesystem system variable.服务器使用character_set_filesystem系统变量指示的字符集解释文件名。

By default, the server interprets the file contents using the character set indicated by the character_set_database system variable. 默认情况下,服务器使用character_set_database系统变量指示的字符集解释文件内容。If the file contents use a character set different from this default, it is a good idea to specify that character set by using the CHARACTER SET clause. 如果文件内容使用与此默认值不同的字符集,则最好使用CHARACTER SET子句指定该字符集。A character set of binary specifies no conversion.binary字符集指定“无转换”。

SET NAMES and the setting of character_set_client do not affect interpretation of file contents.SET NAMEScharacter_set_client的设置不影响文件内容的解释。

LOAD DATA interprets all fields in the file as having the same character set, regardless of the data types of the columns into which field values are loaded. LOAD DATA将文件中的所有字段解释为具有相同的字符集,而不考虑将字段值加载到的列的数据类型。For proper interpretation of the file, you must ensure that it was written with the correct character set. 要正确解释文件,必须确保使用正确的字符集编写。For example, if you write a data file with mysqldump -T or by issuing a SELECT ... INTO OUTFILE statement in mysql, be sure to use a --default-character-set option to write output in the character set to be used when the file is loaded with LOAD DATA.例如,如果您使用mysqldump -T编写数据文件,或者通过在mysql中发出SELECT ... INTO OUTFILE语句,确保使用--default-character-set选项将输出写为在用LOAD DATA加载文件时所使用的字符集。

Note注意

It is not possible to load data files that use the ucs2, utf16, utf16le, or utf32 character set.无法加载使用ucs2utf16utf16leutf32字符集的数据文件。

Input File Location输入文件位置

These rules determine the LOAD DATA input file location:这些规则确定LOAD DATA输入文件的位置:

  • If LOCAL is not specified, the file must be located on the server host. 如果未指定LOCAL,则文件必须位于服务器主机上。The server reads the file directly, locating it as follows:服务器直接读取文件,按如下方式定位:

    • If the file name is an absolute path name, the server uses it as given.如果文件名是绝对路径名,则服务器将按给定方式使用它。

    • If the file name is a relative path name with leading components, the server looks for the file relative to its data directory.如果文件名是具有前导组件的相对路径名,则服务器将查找与其数据目录相关的文件。

    • If the file name has no leading components, the server looks for the file in the database directory of the default database.如果文件名没有前导组件,服务器将在默认数据库的数据库目录中查找该文件。

  • If LOCAL is specified, the file must be located on the client host. 如果指定了LOCAL,则文件必须位于客户端主机上。The client program reads the file, locating it as follows:客户端程序读取文件,并按如下方式定位:

    • If the file name is an absolute path name, the client program uses it as given.如果文件名是绝对路径名,则客户机程序将按给定方式使用它。

    • If the file name is a relative path name, the client program looks for the file relative to its invocation directory.如果文件名是相对路径名,则客户机程序将查找与其调用目录相关的文件。

    When LOCAL is used, the client program reads the file and sends its contents to the server. 当使用LOCAL时,客户机程序读取文件并将其内容发送到服务器。The server creates a copy of the file in the directory where it stores temporary files. 服务器在存储临时文件的目录中创建文件的副本。See Section B.3.3.5, “Where MySQL Stores Temporary Files”. 请参阅第B.3.3.5节,“MySQL存储临时文件的位置”Lack of sufficient space for the copy in this directory can cause the LOAD DATA LOCAL statement to fail.此目录中的副本空间不足可能导致LOAD DATA LOCAL语句失败。

The non-LOCAL rules mean that the server reads a file named as ./myfile.txt relative to its data directory, whereas it reads a file named as myfile.txt from the database directory of the default database. LOCAL规则意味着服务器相对于其数据目录读取名为./myfile.txt的文件,而从默认数据库的数据库目录读取名为myfile.txt的文件。For example, if the following LOAD DATA statement is executed while db1 is the default database, the server reads the file data.txt from the database directory for db1, even though the statement explicitly loads the file into a table in the db2 database:例如,如果在db1是默认数据库时执行以下LOAD DATA语句,则服务器会从db1的数据库目录中读取DATA.txt文件,即使该语句会显式地将该文件加载到db2数据库的表中:

LOAD DATA INFILE 'data.txt' INTO TABLE db2.my_table;
Note注意

The server also uses the non-LOCAL rules to locate .sdi files for the IMPORT TABLE statement.服务器还使用非LOCAL规则来定位IMPORT TABLE语句的.sdi文件。

Security Requirements安全要求

For a non-LOCAL load operation, the server reads a text file located on the server host, so these security requirements must be satisified:对于非LOCAL加载操作,服务器读取位于服务器主机上的文本文件,因此必须满足以下安全要求:

  • You must have the FILE privilege. 您必须具有FILE权限。See Section 6.2.2, “Privileges Provided by MySQL”.请参阅第6.2.2节,“MySQL提供的权限”

  • The operation is subject to the secure_file_priv system variable setting:此操作受secure_file_priv系统变量设置的约束:

    • If the variable value is a nonempty directory name, the file must be located in that directory.如果变量值是非空目录名,则文件必须位于该目录中。

    • If the variable value is empty (which is insecure), the file need only be readable by the server.如果变量值为空(这是不安全的),则该文件只需要服务器可读。

For a LOCAL load operation, the client program reads a text file located on the client host. 对于LOCAL加载操作,客户机程序读取位于客户机主机上的文本文件。Because the file contents are sent over the connection by the client to the server, using LOCAL is a bit slower than when the server accesses the file directly. 因为文件内容是由客户机通过连接发送到服务器的,所以使用LOCAL要比服务器直接访问文件慢一些。On the other hand, you do not need the FILE privilege, and the file can be located in any directory the client program can access.另一方面,您不需要FILE权限,并且文件可以位于客户机程序可以访问的任何目录中。

Duplicate-Key and Error Handling重复密钥和错误处理

The REPLACE and IGNORE modifiers control handling of new (input) rows that duplicate existing table rows on unique key values (PRIMARY KEY or UNIQUE index values):REPLACEIGNORE修饰符控制对在唯一键值(主键或唯一索引值)上复制现有表行的新(输入)行的处理:

The LOCAL modifier has the same effect as IGNORE. LOCAL修改器的效果与IGNORE相同。This occurs because the server has no way to stop transmission of the file in the middle of the operation.这是因为服务器无法停止文件在操作中间的传输。

If none of REPLACE, IGNORE, or LOCAL is specified, an error occurs when a duplicate key value is found, and the rest of the text file is ignored.如果未指定REPLACEIGNORELOCAL,则在发现重复的键值时会发生错误,并忽略文本文件的其余部分。

In addition to affecting duplicate-key handling as just described, IGNORE and LOCAL also affect error handling:除了如上所述影响重复密钥处理外,IGNORELOCAL还影响错误处理:

  • With neither IGNORE nor LOCAL, data-interpretation errors terminate the operation.无论是IGNORE还是LOCAL,数据解释错误都会终止操作。

  • With IGNORE or LOCAL, data-interpretation errors become warnings and the load operation continues, even if the SQL mode is restrictive. 使用IGNORELOCAL时,数据解释错误将变成警告,并且加载操作将继续,即使SQL模式是限制性的。For examples, see Column Value Assignment.有关示例,请参见列值赋值

Index Handling索引处理

To ignore foreign key constraints during the load operation, execute a SET foreign_key_checks = 0 statement before executing LOAD DATA.要在加载操作期间忽略外键约束,请在执行LOAD DATA之前执行SET foreign_key_checks = 0语句。

If you use LOAD DATA on an empty MyISAM table, all nonunique indexes are created in a separate batch (as for REPAIR TABLE). 如果在空的MyISAM表上使用LOAD DATA,那么所有非唯一索引都将在单独的批处理中创建(对于REPAIR table)。Normally, this makes LOAD DATA much faster when you have many indexes. 通常,当您有许多索引时,这会使LOAD DATA的速度更快。In some extreme cases, you can create the indexes even faster by turning them off with ALTER TABLE ... DISABLE KEYS before loading the file into the table and re-creating the indexes with ALTER TABLE ... ENABLE KEYS after loading the file. 在某些极端情况下,在将文件加载到表中之前通过使用ALTER TABLE ... DISABLE KEYS关闭索引,加载文件之后使用ALTER TABLE ... ENABLE KEYS重新创建索引,可以更快地创建索引。See Section 8.2.5.1, “Optimizing INSERT Statements”.请参阅第8.2.5.1节,“优化插入语句”

Field and Line Handling字段处理和行处理

For both the LOAD DATA and SELECT ... INTO OUTFILE statements, the syntax of the FIELDS and LINES clauses is the same. 对于LOAD DATA语句和SELECT ... INTO OUTFILE语句,FIELDSLINES子句的语法是相同的。Both clauses are optional, but FIELDS must precede LINES if both are specified.这两个子句都是可选的,但如果同时指定了这两个子句,则FIELDS必须在LINES之前。

If you specify a FIELDS clause, each of its subclauses (TERMINATED BY, [OPTIONALLY] ENCLOSED BY, and ESCAPED BY) is also optional, except that you must specify at least one of them. 如果指定FIELDS子句,则其每个子子句(TERMINATED BY[OPTIONALLY] ENCLOSED BYESCAPED BY)也是可选的,但必须至少指定其中一个子类。Arguments to these clauses are permitted to contain only ASCII characters.这些子句的参数只允许包含ASCII字符。

If you specify no FIELDS or LINES clause, the defaults are the same as if you had written this:如果不指定FIELDS子句或LINES子句,则默认值与编写此子句时相同:

FIELDS TERMINATED BY '\t' ENCLOSED BY '' ESCAPED BY '\\'
LINES TERMINATED BY '\n' STARTING BY ''

Backslash is the MySQL escape character within strings in SQL statements. 反斜杠是SQL语句中字符串中的MySQL转义字符。Thus, to specify a literal backslash, you must specify two backslashes for the value to be interpreted as a single backslash. 因此,要指定文字反斜杠,必须为要解释为单个反斜杠的值指定两个反斜杠。The escape sequences '\t' and '\n' specify tab and newline characters, respectively.转义序列'\t''\n'分别指定制表符和换行符。

In other words, the defaults cause LOAD DATA to act as follows when reading input:换句话说,在读取输入时,默认值会导致LOAD DATA的行为如下:

  • Look for line boundaries at newlines.在换行处查找行边界。

  • Do not skip any line prefix.不要跳过任何行前缀。

  • Break lines into fields at tabs.在制表符处将行拆分为字段。

  • Do not expect fields to be enclosed within any quoting characters.不要期望字段包含在任何引号字符中。

  • Interpret characters preceded by the escape character \ as escape sequences. 将转义符\前面的字符解释为转义序列。For example, \t, \n, and \\ signify tab, newline, and backslash, respectively. 例如,\t\n\\分别表示制表符、换行符和反斜杠。See the discussion of FIELDS ESCAPED BY later for the full list of escape sequences.有关转义序列的完整列表,请参见后面对FIELDS ESCAPED BY的讨论。

Conversely, the defaults cause SELECT ... INTO OUTFILE to act as follows when writing output:相反,默认值会导致SELECT ... INTO OUTFILE时,按如下操作:

  • Write tabs between fields.在字段之间写入制表符。

  • Do not enclose fields within any quoting characters.不要将字段括在任何引号字符内。

  • Use \ to escape instances of tab, newline, or \ that occur within field values.使用\转义字段值中出现的制表符、换行符或\的实例。

  • Write newlines at the ends of lines.在行尾写新行。

Note注意

For a text file generated on a Windows system, proper file reading might require LINES TERMINATED BY '\r\n' because Windows programs typically use two characters as a line terminator. 对于在Windows系统上生成的文本文件,正确的文件读取可能需要LINES TERMINATED BY '\r\n',因为Windows程序通常使用两个字符作为行终止符。Some programs, such as WordPad, might use \r as a line terminator when writing files. 某些程序(如写字板)在写入文件时可能使用\r作为行终止符。To read such files, use LINES TERMINATED BY '\r'.要读取此类文件,请使用LINES TERMINATED BY '\r'

If all the input lines have a common prefix that you want to ignore, you can use LINES STARTING BY 'prefix_string' to skip the prefix and anything before it. 如果所有输入行都有一个要忽略的公共前缀,则可以使用LINES STARTING BY 'prefix_string'跳过前缀及其前面的任何内容。If a line does not include the prefix, the entire line is skipped. 如果一行不包含前缀,则跳过整行。Suppose that you issue the following statement:假设您发布以下语句:

LOAD DATA INFILE '/tmp/test.txt' INTO TABLE test
  FIELDS TERMINATED BY ','  LINES STARTING BY 'xxx';

If the data file looks like this:如果数据文件如下所示:

xxx"abc",1
something xxx"def",2
"ghi",3

The resulting rows are ("abc",1) and ("def",2). 结果行是("abc",1)("def",2)The third row in the file is skipped because it does not contain the prefix.跳过文件中的第三行,因为它不包含前缀。

The IGNORE number LINES clause can be used to ignore lines at the start of the file. IGNORE number LINES子句可用于忽略文件开头的行。For example, you can use IGNORE 1 LINES to skip an initial header line containing column names:例如,可以使用IGNORE 1 LINES跳过包含列名的初始标题行:

LOAD DATA INFILE '/tmp/test.txt' INTO TABLE test IGNORE 1 LINES;

When you use SELECT ... INTO OUTFILE in tandem with LOAD DATA to write data from a database into a file and then read the file back into the database later, the field- and line-handling options for both statements must match. 当你使用SELECT ... INTO OUTFILE配合LOAD DATA以将数据从数据库写入文件,然后再将文件读回数据库时,两个语句的字段和行处理选项必须匹配。Otherwise, LOAD DATA does not interpret the contents of the file properly. 否则,LOAD DATA无法正确解释文件的内容。Suppose that you use SELECT ... INTO OUTFILE to write a file with fields delimited by commas:假设您使用SELECT ... INTO OUTFILE以写入字段由逗号分隔的文件:

SELECT * INTO OUTFILE 'data.txt'
  FIELDS TERMINATED BY ','
  FROM table2;

To read the comma-delimited file, the correct statement is:要读取逗号分隔的文件,正确的语句是:

LOAD DATA INFILE 'data.txt' INTO TABLE table2
  FIELDS TERMINATED BY ',';

If instead you tried to read the file with the statement shown following, it would not work because it instructs LOAD DATA to look for tabs between fields:相反,如果您尝试使用下面所示的语句读取文件,它将不起作用,因为它指示LOAD DATA在字段之间查找制表符:

LOAD DATA INFILE 'data.txt' INTO TABLE table2
  FIELDS TERMINATED BY '\t';

The likely result is that each input line would be interpreted as a single field.可能的结果是,每个输入行将被解释为一个字段。

LOAD DATA can be used to read files obtained from external sources. LOAD DATA可用于读取从外部源获取的文件。For example, many programs can export data in comma-separated values (CSV) format, such that lines have fields separated by commas and enclosed within double quotation marks, with an initial line of column names. 例如,许多程序可以以逗号分隔值(CSV)格式导出数据,例如,行中的字段用逗号分隔,并用双引号括起来,其中包含一行初始列名。If the lines in such a file are terminated by carriage return/newline pairs, the statement shown here illustrates the field- and line-handling options you would use to load the file:如果此类文件中的行由回车/换行符对终止,则此处显示的语句说明了用于加载文件的字段和行处理选项:

LOAD DATA INFILE 'data.txt' INTO TABLE tbl_name
  FIELDS TERMINATED BY ',' ENCLOSED BY '"'
  LINES TERMINATED BY '\r\n'
  IGNORE 1 LINES;

If the input values are not necessarily enclosed within quotation marks, use OPTIONALLY before the ENCLOSED BY option.如果输入值不一定要用引号括起来,可以在ENCLOSED BY选项之前使用OPTIONALLY

Any of the field- or line-handling options can specify an empty string (''). 任何字段或行处理选项都可以指定空字符串('')。If not empty, the FIELDS [OPTIONALLY] ENCLOSED BY and FIELDS ESCAPED BY values must be a single character. 如果不为空,则FIELDS [OPTIONALLY] ENCLOSED BY值和FIELDS ESCAPED BY值必须是单个字符。The FIELDS TERMINATED BY, LINES STARTING BY, and LINES TERMINATED BY values can be more than one character. FIELDS TERMINATED BYLINES STARTING BYLINES TERMINATED BY可以是多个字符。For example, to write lines that are terminated by carriage return/linefeed pairs, or to read a file containing such lines, specify a LINES TERMINATED BY '\r\n' clause.例如,要写入由回车/换行符对终止的行,或要读取包含此类行的文件,请指定LINES TERMINATED BY '\r\n'子句。

To read a file containing jokes that are separated by lines consisting of %%, you can do this要读取包含笑话的文件,这些笑话由%%组成的行分隔,可以执行以下操作

CREATE TABLE jokes
  (a INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  joke TEXT NOT NULL);
LOAD DATA INFILE '/tmp/jokes.txt' INTO TABLE jokes
  FIELDS TERMINATED BY ''
  LINES TERMINATED BY '\n%%\n' (joke);

FIELDS [OPTIONALLY] ENCLOSED BY controls quoting of fields. FIELDS [OPTIONALLY] ENCLOSED BY控制了字段的引号。For output (SELECT ... INTO OUTFILE), if you omit the word OPTIONALLY, all fields are enclosed by the ENCLOSED BY character. 对于输出(SELECT ... INTO OUTFILE),如果您省略了单词OPTIONALLY,则所有字段都将由ENCLOSED BY字符括起来。An example of such output (using a comma as the field delimiter) is shown here:这种输出的示例(使用逗号作为字段分隔符)如下所示:

"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 used only to enclose values from columns that have a string data type (such as CHAR, BINARY, TEXT, or ENUM):如果指定了OPTIONALLY,则ENCLOSED BY字符仅用于括起具有字符串数据类型(如CHARBINARYTEXTENUM)的列中的值:

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

Occurrences of the ENCLOSED BY character within a field value are escaped by prefixing them with the ESCAPED BY character. 字段值中出现ENCLOSED BY字符,将被转义,方法是在它们前面加一个ESCAPED BY字符。Also, if you specify an empty ESCAPED BY value, it is possible to inadvertently generate output that cannot be read properly by LOAD DATA. 另外,如果指定空的ESCAPED BY值,则可能会无意中生成无法由LOAD DATA正确读取的输出。For example, the preceding output just shown would appear as follows if the escape character is empty. 例如,如果转义字符为空,则前面显示的输出如下所示。Observe that the second field in the fourth line contains a comma following the quote, which (erroneously) appears to terminate 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

For input, the ENCLOSED BY character, if present, is stripped from the ends of field values. 对于输入,如果存在ENCLOSED BY字符,则会从字段值的末尾去掉。(This is true regardless of whether OPTIONALLY is specified; OPTIONALLY has no effect on input interpretation.) (这是真实的,无论是否是否指定了OPTIONALLYOPTIONALLY对输入解释都没有影响。)Occurrences of the ENCLOSED BY character preceded by the ESCAPED BY character are interpreted as part of the current field value.ESCAPED BY字符之前出现的ENCLOSED BY字符将被解释为当前字段值的一部分。

If the field begins with the ENCLOSED BY character, instances of that character are recognized as terminating a field value only if followed by the field or line TERMINATED BY sequence. 如果字段以ENCLOSED BY字符开头,则该字符的实例仅在后跟字段或行TERMINATED BY序列时才被识别为终止字段值。To avoid ambiguity, occurrences of the ENCLOSED BY character within a field value can be doubled and are interpreted as a single instance of the character. 为避免歧义,字段值中ENCLOSED BY字符出现次数可以加倍,并解释为字符的单个实例。For example, if ENCLOSED BY '"' is specified, quotation marks are handled as shown here:例如,如果指定了ENCLOSED BY '"',则按如下所示处理引号:

"The ""BIG"" boss"  -> The "BIG" boss
The "BIG" boss      -> The "BIG" boss
The ""BIG"" boss    -> The ""BIG"" boss

FIELDS ESCAPED BY controls how to read or write special characters:FIELDS ESCAPED BY控制了如何读取或写入特殊字符:

  • For input, if the FIELDS ESCAPED BY character is not empty, occurrences of that character are stripped and the following character is taken literally as part of a field value. 对于输入,如果FIELDS ESCAPED BY字符不为空,则将剥离该字符的出现,并将以下字符作为字段值的一部分。Some two-character sequences that are exceptions, where the first character is the escape character. 一些双字符序列是例外,其中第一个字符是转义字符。These sequences are shown in the following table (using \ for the escape character). 下表显示了这些序列(转义字符使用\表示)。The rules for NULL handling are described later in this section.NULL值处理的规则将在本节后面介绍。

    Character字符Escape Sequence转义序列
    \0An ASCII NUL (X'00') characterASCII NUL(X'00')字符
    \bA backspace character退格字符
    \nA newline (linefeed) character换行符
    \rA carriage return character回车符
    \tA tab character.制表符。
    \ZASCII 26 (Control+Z)
    \NNULL

    For more information about \-escape syntax, see Section 9.1.1, “String Literals”.有关\转义语法的详细信息,请参阅第9.1.1节,“字符串文字”

    If the FIELDS ESCAPED BY character is empty, escape-sequence interpretation does not occur.如果FIELDS ESCAPED BY字符为空,则不会进行转义序列解释。

  • For output, if the FIELDS ESCAPED BY character is not empty, it is used to prefix the following characters on output:对于输出,如果按FIELDS ESCAPED BY字符不为空,则用于在输出时为以下字符加前缀:

    • The FIELDS ESCAPED BY character.FIELDS ESCAPED BY字符。

    • The FIELDS [OPTIONALLY] ENCLOSED BY character.FIELDS [OPTIONALLY] ENCLOSED BY字符

    • The first character of the FIELDS TERMINATED BY and LINES TERMINATED BY values, if the ENCLOSED BY character is empty or unspecified.FIELDS TERMINATED BYLINES TERMINATED BY值的第一个字符(如果ENCLOSED BY字符是空的或未指定)。

    • ASCII 0 (what is actually written following the escape character is ASCII 0, not a zero-valued byte).ASCII 0(在转义字符后面实际写入的是ASCII 0,而不是零值字节)。

    If the FIELDS ESCAPED BY character is empty, no characters are escaped and NULL is output as NULL, not \N. 如果FIELDS ESCAPED BY字符为空,则不转义任何字符,并且NULL输出为NULL,而不是\NIt is probably not a good idea to specify an empty escape character, particularly if field values in your data contain any of the characters in the list just given.指定一个空转义字符可能不是一个好主意,特别是如果数据中的字段值包含刚刚给出的列表中的任何字符。

In certain cases, field- and line-handling options interact:在某些情况下,字段和行处理选项相互作用:

  • If LINES TERMINATED BY is an empty string and FIELDS TERMINATED BY is nonempty, lines are also terminated with FIELDS TERMINATED BY.如果LINES TERMINATED BY是空字符串,而FIELDS TERMINATED BY是非空的,则行也会以FIELDS TERMINATED BY结尾。

  • If the FIELDS TERMINATED BY and FIELDS ENCLOSED BY values are both empty (''), a fixed-row (nondelimited) format is used. With fixed-row format, no delimiters are used between fields (but you can still have a line terminator). 如果FIELDS TERMINATED BYFIELDS ENCLOSED BY值都为空(''),则使用固定行(非delimited)格式。对于固定行格式,字段之间不使用分隔符(但仍可以使用行终止符)。Instead, column values are read and written using a field width wide enough to hold all values in the field. 相反,使用足够宽的字段来读取和写入列值,以容纳字段中的所有值。For TINYINT, SMALLINT, MEDIUMINT, INT, and BIGINT, the field widths are 4, 6, 8, 11, and 20, respectively, no matter what the declared display width is.对于TINYINTSMALLINTMEDIUMINTINTBIGINT,无论声明的显示宽度是多少,字段宽度都分别为4、6、8、11和20。

    LINES TERMINATED BY is still used to separate lines. LINES TERMINATED BY仍用于分隔行。If a line does not contain all fields, the rest of the columns are set to their default values. 如果一行不包含所有字段,则其余列将设置为其默认值。If you do not have a line terminator, you should set this to ''. 如果没有行终止符,则应将其设置为''In this case, the text file must contain all fields for each row.在这种情况下,文本文件必须包含每行的所有字段。

    Fixed-row format also affects handling of NULL values, as described later.固定行格式也会影响NULL值的处理,如后面所述。

    Note注意

    Fixed-size format does not work if you are using a multibyte character set.如果使用多字节字符集,则固定大小格式不起作用。

Handling of NULL values varies according to the FIELDS and LINES options in use:NULL值的处理因使用的FIELDLINES选项而异:

  • For the default FIELDS and LINES values, NULL is written as a field value of \N for output, and a field value of \N is read as NULL for input (assuming that the ESCAPED BY character is \).对于默认FIELDS值和LINES值,NULL作为字段值\N写入输出,字段值\N作为NULL读取输入(假设ESCAPED BY字符为\)。

  • If FIELDS ENCLOSED BY is not empty, a field containing the literal word NULL as its value is read as a NULL value. 如果FIELDS ENCLOSED BY不为空,则将包含文本字NULL作为其值的字段读取为NULL值。This differs from the word NULL enclosed within FIELDS ENCLOSED BY characters, which is read as the string 'NULL'.这与用字符括起来的字段中包含的单词NULL不同,后者被读取为字符串'NULL'

  • If FIELDS ESCAPED BY is empty, NULL is written as the word NULL.如果FIELDS ESCAPED BY为空,则将NULL写为单词NULL

  • With fixed-row format (which is used when FIELDS TERMINATED BY and FIELDS ENCLOSED BY are both empty), NULL is written as an empty string. 对于固定行格式(当FIELDS TERMINATED BYFIELDS ENCLOSED BY都为空时使用),NULL将作为空字符串写入。This causes both NULL values and empty strings in the table to be indistinguishable when written to the file because both are written as empty strings. 这会导致表中的NULL值和空字符串在写入文件时无法区分,因为它们都是作为空字符串写入的。If you need to be able to tell the two apart when reading the file back in, you should not use fixed-row format.如果在读回文件时需要区分两者,则不应使用固定行格式。

An attempt to load NULL into a NOT NULL column produces either a warning or an error according to the rules described in Column Value Assignment.根据列值赋值中描述的规则,尝试将NULL加载到NOT NULL列会产生警告或错误。

Some cases are not supported by LOAD DATA:LOAD DATA不支持某些情况:

  • Fixed-size rows (FIELDS TERMINATED BY and FIELDS ENCLOSED BY both empty) and BLOB or TEXT columns.固定大小的行(FIELDS TERMINATED BYFIELDS ENCLOSED BY都为空)和BLOBTEXT列。

  • If you specify one separator that is the same as or a prefix of another, LOAD DATA cannot interpret the input properly. 如果指定一个与另一个分隔符相同或前缀相同的分隔符,则LOAD DATA无法正确解释输入。For example, the following FIELDS clause would cause problems:例如,以下FIELDS子句会导致问题:

    FIELDS TERMINATED BY '"' ENCLOSED BY '"'
  • If FIELDS ESCAPED BY is empty, a field value that contains an occurrence of FIELDS ENCLOSED BY or LINES TERMINATED BY followed by the FIELDS TERMINATED BY value causes LOAD DATA to stop reading a field or line too early. 如果FIELDS ESCAPED BY为空,则出现以下内容的字段值:FIELDS ENCLOSED BYLINES TERMINATED BY,后跟FIELDS TERMINATED BY值,会导致LOAD DATA过早停止读取字段或行。This happens because LOAD DATA cannot properly determine where the field or line value ends.这是因为LOAD DATA无法正确确定字段或行值的结束位置。

Column List Specification列列表规范

The following example loads all columns of the persondata table:以下示例加载persondata表的所有列:

LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata;

By default, when no column list is provided at the end of the LOAD DATA statement, input lines are expected to contain a field for each table column. 默认情况下,如果在LOAD DATA语句的末尾没有提供列列表,则输入行应该为每个表列包含一个字段。If you want to load only some of a table's columns, specify a column list:如果只想加载表的某些列,请指定列列表:

LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata
(col_name_or_user_var [, col_name_or_user_var] ...);

You must also specify a column list if the order of the fields in the input file differs from the order of the columns in the table. 如果输入文件中字段的顺序与表中列的顺序不同,则还必须指定列列表。Otherwise, MySQL cannot tell how to match input fields with table columns.否则,MySQL无法告诉如何将输入字段与表列匹配。

Input Preprocessing输入预处理

Each instance of col_name_or_user_var in LOAD DATA syntax is either a column name or a user variable. LOAD DATA语法中col_name_or_user_var的每个实例都是列名或用户变量。With user variables, the SET clause enables you to perform preprocessing transformations on their values before assigning the result to columns.对于用户变量,SET子句允许您在将结果赋给列之前对其值执行预处理转换。

User variables in the SET clause can be used in several ways. SET子句中的用户变量可以以多种方式使用。The following example uses the first input column directly for the value of t1.column1, and assigns the second input column to a user variable that is subjected to a division operation before being used for the value of t1.column2:下面的示例将第一个输入列直接用于t1.column1的值,并将第二个输入列指定给用户变量,该用户变量在用于t1.column2的值之前进行除法运算:

LOAD DATA INFILE 'file.txt'
  INTO TABLE t1
  (column1, @var1)
  SET column2 = @var1/100;

The SET clause can be used to supply values not derived from the input file. SET子句可用于提供不是从输入文件派生的值。The following statement sets column3 to the current date and time:以下语句将column3设置为当前日期和时间:

LOAD DATA INFILE 'file.txt'
  INTO TABLE t1
  (column1, column2)
  SET column3 = CURRENT_TIMESTAMP;

You can also discard an input value by assigning it to a user variable and not assigning the variable to any table column:也可以通过将输入值赋给用户变量而不将该变量赋给任何表列来丢弃输入值:

LOAD DATA INFILE 'file.txt'
  INTO TABLE t1
  (column1, @dummy, column2, @dummy, column3);

Use of the column/variable list and SET clause is subject to the following restrictions:列/变量列表和SET子句的使用受以下限制:

  • Assignments in the SET clause should have only column names on the left hand side of assignment operators.SET子句中的赋值只能在赋值运算符的左侧有列名。

  • You can use subqueries in the right hand side of SET assignments. 可以在SET分配的右侧使用子查询。A subquery that returns a value to be assigned to a column may be a scalar subquery only. 返回要分配给列的值的子查询只能是标量子查询。Also, you cannot use a subquery to select from the table that is being loaded.此外,不能使用子查询从正在加载的表中进行选择。

  • Lines ignored by an IGNORE number LINES clause are not processed for the column/variable list or SET clause.对于列/变量列表或SET子句,不处理被IGNORE number LINES子句忽略的行。

  • User variables cannot be used when loading data with fixed-row format because user variables do not have a display width.加载固定行格式的数据时不能使用用户变量,因为用户变量没有显示宽度。

Column Value Assignment列值赋值

To process an input line, LOAD DATA splits it into fields and uses the values according to the column/variable list and the SET clause, if they are present. 要处理输入行,LOAD DATA将其拆分为字段,并根据列/变量列表和SET子句(如果存在)使用值。Then the resulting row is inserted into the table. 然后将结果行插入表中。If there are BEFORE INSERT or AFTER INSERT triggers for the table, they are activated before or after inserting the row, respectively.如果表有BEFORE INSERTAFTER INSERT触发器,则它们分别在插入行之前或之后被激活。

Interpretation of field values and assignment to table columns depends on these factors:字段值的解释和表列的赋值取决于以下因素:

  • The SQL mode (the value of the sql_mode system variable). SQL模式(sql_mode系统变量的值)。The mode can be nonstrictive, or restrictive in various ways. 模式可以是非限制性的,也可以是各种限制性的。For example, strict SQL mode can be enabled, or the mode can include values such as NO_ZERO_DATE or NO_ZERO_IN_DATE.例如,可以启用严格的SQL模式,或者该模式可以包含诸如NO_ZERO_DATENO_ZERO_IN_DATE之类的值。

  • Presence or absence of the IGNORE and LOCAL modifiers.是否存在IGNORELOCAL修饰语。

Those factors combine to produce restrictive or nonrestrictive data interpretation by LOAD DATA:这些因素结合起来,通过LOAD DATA产生限制性或非限制性的数据解释:

  • Data interpretation is restrictive if the SQL mode is restrictive and neither the IGNORE nor the LOCAL modifier is specified. 如果SQL模式是限制性的,并且既没有指定IGNORE也没有指定LOCAL修饰符,则数据解释是限制性的。Errors terminate the load operation.错误终止加载操作。

  • Data interpretation is nonrestrictive if the SQL mode is nonrestrictive or the IGNORE or LOCAL modifier is specified. 如果SQL模式是非限制的,或者指定了IGNORELOCAL修饰符,则数据解释是非限制的。(In particular, either modifier if specified overrides a restrictive SQL mode.) (特别是,如果指定了任何一个修饰符,都会覆盖限制性SQL模式。)Errors become warnings and the load operation continues.错误变成警告,加载操作继续。

Restrictive data interpretation uses these rules:限制性数据解释使用以下规则:

  • Too many or too few fields results an error.字段过多或过少会导致错误。

  • Assigning NULL (that is, \N) to a non-NULL column results in an error.NULL(即,\N)赋给非NULL列会导致错误。

  • A value that is out of range for the column data type results in an error.值超出列数据类型的范围将导致错误。

  • Invalid values produce errors. 无效值会产生错误。For example, a value such as 'x' for a numeric column results in an error, not conversion to 0.例如,数值列的'x'等值会导致错误,而不是转换为0。

By contrast, nonrestrictive data interpretation uses these rules:相比之下,非限制性数据解释使用以下规则:

  • If an input line has too many fields, the extra fields are ignored and the number of warnings is incremented.如果输入行中的字段太多,则会忽略多余的字段,并增加警告的数量。

  • If an input line has too few fields, the columns for which input fields are missing are assigned their default values. 如果输入行的字段太少,则会为缺少输入字段的列指定默认值。Default value assignment is described in Section 11.6, “Data Type Default Values”.第11.6节,“数据类型默认值”中描述了默认值分配。

  • Assigning NULL (that is, \N) to a non-NULL column results in assignment of the implicit default value for the column data type. NULL(即,\N)赋给非NULL列将导致为列数据类型指定隐式默认值。Implicit default values are described in Section 11.6, “Data Type Default Values”.第11.6节,“数据类型默认值”中描述了隐式默认值。

  • Invalid values produce warnings rather than errors, and are converted to the closest valid value for the column data type. 无效值将生成警告而不是错误,并转换为列数据类型的“最接近”有效值。Examples:示例:

    • A value such as 'x' for a numeric column results in conversion to 0.数值列的值(如'x')将导致转换为0。

    • An out-of-range numeric or temporal value is clipped to the closest endpoint of the range for the column data type.超出范围的数值或时间值被剪裁到列数据类型范围的最近端点。

    • An invalid value for a DATETIME, DATE, or TIME column is inserted as the implicit default value, regardless of the SQL mode NO_ZERO_DATE setting. 无论SQL mode NO_ZERO_DATE设置如何,DATETIMEDATETIME列的无效值都将作为隐式默认值插入。The implicit default is the appropriate zero value for the type ('0000-00-00 00:00:00', '0000-00-00', or '00:00:00'). 隐式默认值是类型的适当“零”值('0000-00-00 00:00:00''0000-00-00''00:00:00')。See Section 11.2, “Date and Time Data Types”.请参阅第11.2节,“日期和时间数据类型”

  • LOAD DATA interprets an empty field value differently from a missing field:LOAD DATA对空字段值的解释与缺少字段的解释不同:

    • For string types, the column is set to the empty string.对于字符串类型,列设置为空字符串。

    • For numeric types, the column is set to 0.对于数值类型,列设置为0

    • For date and time types, the column is set to the appropriate zero value for the type. 对于日期和时间类型,该列被设置为该类型的相应“零”值。See Section 11.2, “Date and Time Data Types”.第11.2节,“日期和时间数据类型”

    These are the same values that result if you assign an empty string explicitly to a string, numeric, or date or time type explicitly in an INSERT or UPDATE statement.如果在INSERT语句或UPDATE语句中显式地将空字符串指定给字符串、数字、日期或时间类型,则会产生相同的值。

TIMESTAMP columns are set to the current date and time only if there is a NULL value for the column (that is, \N) and the column is not declared to permit NULL values, or if the TIMESTAMP column default value is the current timestamp and it is omitted from the field list when a field list is specified.只有当列有NULL值(即,\N)且该列未声明为允许NULL值时,或者如果TIMESTAMP列的默认值是当前时间戳且在指定字段列表时从字段列表中省略,则TIMESTAMP列才会设置为当前日期和时间。

LOAD DATA regards all input as strings, so you cannot use numeric values for ENUM or SET columns the way you can with INSERT statements. LOAD DATA将所有输入视为字符串,因此不能像INSERT语句那样对ENUM列或SET列使用数值。All ENUM and SET values must be specified as strings.必须将所有ENUM值和SET值指定为字符串。

BIT values cannot be loaded directly using binary notation (for example, b'011010'). 不能使用二进制表示法(例如,b'011010')直接加载BIT值。To work around this, use the SET clause to strip off the leading b' and trailing ' and perform a base-2 to base-10 conversion so that MySQL loads the values into the BIT column properly:要解决这个问题,请使用SET子句去掉前导的b'和尾部',并执行从base-2到base-10的转换,以便MySQL正确地将值加载到BIT列中:

shell> cat /tmp/bit_test.txt
b'10'
b'1111111'
shell> mysql test
mysql> LOAD DATA INFILE '/tmp/bit_test.txt'
INTO TABLE bit_test (@var1)
SET b = CAST(CONV(MID(@var1, 3, LENGTH(@var1)-3), 2, 10) AS UNSIGNED);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Deleted: 0  Skipped: 0  Warnings: 0

mysql> SELECT BIN(b+0) FROM bit_test;
+----------+
| BIN(b+0) |
+----------+
| 10       |
| 1111111  |
+----------+
2 rows in set (0.00 sec)

For BIT values in 0b binary notation (for example, 0b011010), use this SET clause instead to strip off the leading 0b:对于0b二进制表示法中的BIT值(例如,0b011010),请改用此SET子句去除前导0b

SET b = CAST(CONV(MID(@var1, 3, LENGTH(@var1)-2), 2, 10) AS UNSIGNED)

Partitioned Table Support分区表支持

LOAD DATA supports explicit partition selection using the PARTITION clause with a list of one or more comma-separated names of partitions, subpartitions, or both. LOAD DATA支持显式分区选择,使用PARTITION子句列出一个或多个以逗号分隔的分区、子分区或两者的名称。When this clause is used, if any rows from the file cannot be inserted into any of the partitions or subpartitions named in the list, the statement fails with the error Found a row not matching the given partition set. 使用此子句时,如果无法将文件中的任何行插入到列表中指定的任何分区或子分区中,则该语句将失败,并出现“发现与给定分区集不匹配的行”错误。For more information and examples, see Section 24.5, “Partition Selection”.有关更多信息和示例,请参阅第24.5节,“分区选择”

Concurrency Considerations并发注意事项

With the LOW_PRIORITY modifier, execution of the LOAD DATA statement is delayed until no other clients are reading from the table. 使用LOW_PRIORITY修饰符,LOAD DATA语句的执行会延迟,直到没有其他客户机从表中读取数据为止。This affects only storage engines that use only table-level locking (such as MyISAM, MEMORY, and MERGE).这只影响仅使用表级锁定的存储引擎(如MyISAMMEMORYMERGE)。

With the CONCURRENT modifier and a MyISAM table that satisfies the condition for concurrent inserts (that is, it contains no free blocks in the middle), other threads can retrieve data from the table while LOAD DATA is executing. 使用CONCURRENT修饰符和满足并发插入条件的MyISAM表(即,它中间不包含空闲块),其他线程可以在执行LOAD DATA时从表中检索数据。This modifier affects the performance of LOAD DATA a bit, even if no other thread is using the table at the same time.即使没有其他线程同时使用该表,该修饰符也会稍微影响LOAD DATA的性能。

Statement Result Information报表结果信息

When the LOAD DATA statement finishes, it returns an information string in the following format:LOAD DATA语句完成后,将返回以下格式的信息字符串:

Records: 1  Deleted: 0  Skipped: 0  Warnings: 0

Warnings occur under the same circumstances as when values are inserted using the INSERT statement (see Section 13.2.6, “INSERT Statement”), except that LOAD DATA also generates warnings when there are too few or too many fields in the input row.警告出现的情况与使用INSERT语句插入值的情况相同(请参阅第13.2.6节,“INSERT语句”),只是当输入行中的字段太少或太多时,LOAD DATA也会生成警告。

You can use SHOW WARNINGS to get a list of the first max_error_count warnings as information about what went wrong. 您可以使用SHOW WARNINGS来获取第一个max_error_count警告的列表,作为出错的信息。See Section 13.7.7.42, “SHOW WARNINGS Statement”.请参阅第13.7.7.42节,“显示警告声明”

If you are using the C API, you can get information about the statement by calling the mysql_info() function. 如果您使用的是C API,那么可以通过调用mysql_info()函数来获取有关该语句的信息。See mysql_info().请参阅mysql_info()

Replication Considerations复制注意事项

LOAD DATA is considered unsafe for statement-based replication. 对于基于语句的复制,LOAD DATA被认为是不安全的。If you use LOAD DATA with binlog_format=STATEMENT, each replica on which the changes are to be applied creates a temporary file containing the data. 如果将LOAD DATAbinlog_format=STATEMENT一起使用,则要应用更改的每个副本都会创建一个包含数据的临时文件。This temporary file is not encrypted, even if binary log encryption is active on the source, If encryption is required, use row-based or mixed binary logging format instead, for which replicas do not create the temporary file. 此临时文件未加密,即使源上的二进制日志加密处于活动状态,如果需要加密,请改用基于行或混合二进制日志格式,副本不会为其创建临时文件。For more information on the interaction between LOAD DATA and replication, see Section 17.5.1.19, “Replication and LOAD DATA”.有关LOAD DATA和复制之间交互的更多信息,请参阅第17.5.1.19节,“复制和加载数据”

Miscellaneous Topics杂项主题

On Unix, if you need LOAD DATA to read from a pipe, you can use the following technique (the example loads a listing of the / directory into the table db1.t1):在Unix上,如果需要LOAD DATA从管道中读取,可以使用以下技术(示例将/目录的列表加载到表db1.t1中):

mkfifo /mysql/data/db1/ls.dat
chmod 666 /mysql/data/db1/ls.dat
find / -ls > /mysql/data/db1/ls.dat &
mysql -e "LOAD DATA INFILE 'ls.dat' INTO TABLE t1" db1

Here you must run the command that generates the data to be loaded and the mysql commands either on separate terminals, or run the data generation process in the background (as shown in the preceding example). 在这里,您必须在不同的终端上运行生成要加载的数据的命令和mysql命令,或者在后台运行数据生成过程(如前面的示例所示)。If you do not do this, the pipe blocks until data is read by the mysql process.如果不这样做,管道将阻塞,直到mysql进程读取数据。