LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name
' [REPLACE | IGNORE] INTO TABLEtbl_name
[PARTITION (partition_name
[,partition_name
] ...)] [CHARACTER SETcharset_name
] [{FIELDS | COLUMNS} [TERMINATED BY 'string
'] [[OPTIONALLY] ENCLOSED BY 'char
'] [ESCAPED BY 'char
'] ] [LINES [STARTING BY 'string
'] [TERMINATED BY 'string
'] ] [IGNOREnumber
{LINES | ROWS}] [(col_name_or_user_var
[,col_name_or_user_var
] ...)] [SETcol_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 DATA
是SELECT ... 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 OUTFILE
。To read the file back into a table, use 要将文件读回表中,请使用LOAD DATA
. LOAD DATA
。The syntax of the 对于这两个语句,FIELDS
and LINES
clauses is the same for both statements.FIELDS
和LINES
子句的语法是相同的。
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”.INSERT
与LOAD DATA
的效率以及加速LOAD DATA
的信息,请参阅第8.2.5.1节,“优化INSERT语句”。
The 与非LOCAL
modifier affects these aspects of LOAD DATA
, compared to non-LOCAL
operation:LOCAL
操作相比,LOCAL
修饰符影响LOAD DATA
的这些方面:
It changes the expected location of the input file; see Input File Location.它改变了输入文件的预期位置;请参见输入文件位置。
It changes the statement security requirements; see Security Requirements.改变报表安全要求;请参阅安全要求。
It has the same effect as the 它对输入文件内容的解释和错误处理具有与IGNORE
modifier on the interpretation of input file contents and error handling; see Duplicate-Key and Error Handling, and Column Value Assignment.IGNORE
修饰符相同的效果;请参阅重复键和错误处理以及列值分配。
只有在服务器和客户端都配置为允许的情况下,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 例如,如果mysqld启动时禁用了local_infile
system variable disabled, LOCAL
produces an error. local_infle
系统变量,则LOCAL
会产生一个错误。See Section 6.1.6, “Security Considerations for LOAD DATA LOCAL”.参见第6.1.6节,“本地负载数据的安全注意事项”。
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 NAMES
和character_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 例如,如果您使用mysqldump -T编写数据文件,或者通过在mysql中发出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
.SELECT ... INTO OUTFILE
语句,确保使用--default-character-set
选项将输出写为在用LOAD DATA
加载文件时所使用的字符集。
It is not possible to load data files that use the 无法加载使用ucs2
, utf16
, utf16le
, or utf32
character set.ucs2
、utf16
、utf16le
或utf32
字符集的数据文件。
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;
The server also uses the non-服务器还使用非LOCAL
rules to locate .sdi
files for the IMPORT TABLE
statement.LOCAL
规则来定位IMPORT TABLE
语句的.sdi
文件。
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
权限,并且文件可以位于客户机程序可以访问的任何目录中。
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):REPLACE
和IGNORE
修饰符控制对在唯一键值(主键或唯一索引值)上复制现有表行的新(输入)行的处理:
With 使用REPLACE
, new rows that have the same value as a unique key value in an existing row replace the existing row. REPLACE
时,与现有行中唯一键值具有相同值的新行将替换现有行。See Section 13.2.9, “REPLACE Statement”.见第13.2.9节,“REPLACE语句”。
With 使用IGNORE
, new rows that duplicate an existing row on a unique key value are discarded. IGNORE
,将丢弃与唯一键值上的现有行重复的新行。For more information, see The Effect of IGNORE on Statement Execution.有关详细信息,请参阅IGNORE对语句执行的影响。
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.REPLACE
、IGNORE
或LOCAL
,则在发现重复的键值时会发生错误,并忽略文本文件的其余部分。
In addition to affecting duplicate-key handling as just described, 除了如上所述影响重复密钥处理外,IGNORE
and LOCAL
also affect error handling:IGNORE
和LOCAL
还影响错误处理:
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. IGNORE
或LOCAL
时,数据解释错误将变成警告,并且加载操作将继续,即使SQL模式是限制性的。For examples, see Column Value Assignment.有关示例,请参见列值赋值。
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节,“优化插入语句”。
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
语句,FIELDS
和LINES
子句的语法是相同的。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 BY
和ESCAPED 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.在行尾写新行。
For a text file generated on a Windows system, proper file reading might require 对于在Windows系统上生成的文本文件,正确的文件读取可能需要LINES TERMINATED BY '\r\n'
because Windows programs typically use two characters as a line terminator. 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 '
to skip the prefix and anything before it. prefix_string
'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
clause can be used to ignore lines at the start of the file. number
LINESIGNORE
子句可用于忽略文件开头的行。number
LINESFor 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 BY
、LINES STARTING BY
和LINES 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
字符仅用于括起具有字符串数据类型(如CHAR
、BINARY
、TEXT
或ENUM
)的列中的值:
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.) OPTIONALLY
;OPTIONALLY
对输入解释都没有影响。)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
值处理的规则将在本节后面介绍。
\0 | X'00' ) characterX'00' )字符 |
\b | |
\n | |
\r | |
\t | |
\Z | ASCII 26 (Control+Z) |
\N | NULL |
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 BY
值LINES TERMINATED BY
值的第一个字符(如果ENCLOSED BY
字符是空的或未指定)。
ASCII ASCII 0
(what is actually written following the escape character is ASCII 0
, not a zero-valued byte).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
,而不是\N
。It 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 BY
值FIELDS 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.TINYINT
、SMALLINT
、MEDIUMINT
、INT
和BIGINT
,无论声明的显示宽度是多少,字段宽度都分别为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
值的处理,如后面所述。
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
值的处理因使用的FIELD
和LINES
选项而异:
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 BY
和FIELDS 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 BY
和FIELDS ENCLOSED BY
都为空)和BLOB
或TEXT
列。
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 BY
或LINES TERMINATED BY
,后跟FIELDS TERMINATED BY
值,会导致LOAD DATA
过早停止读取字段或行。This happens because 这是因为LOAD DATA
cannot properly determine where the field or line value ends.LOAD DATA
无法正确确定字段或行值的结束位置。
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无法告诉如何将输入字段与表列匹配。
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
clause are not processed for the column/variable list or number
LINESSET
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.加载固定行格式的数据时不能使用用户变量,因为用户变量没有显示宽度。
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 INSERT
或AFTER INSERT
触发器,则它们分别在插入行之前或之后被激活。
Interpretation of field values and assignment to table columns depends on these factors:字段值的解释和表列的赋值取决于以下因素:
The SQL mode (the value of the SQL模式(sql_mode
system variable). 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 例如,可以启用严格的SQL模式,或者该模式可以包含诸如NO_ZERO_DATE
or NO_ZERO_IN_DATE
.NO_ZERO_DATE
或NO_ZERO_IN_DATE
之类的值。
Presence or absence of the 是否存在IGNORE
and LOCAL
modifiers.IGNORE
和LOCAL
修饰语。
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 如果SQL模式是限制性的,并且既没有指定IGNORE
nor the LOCAL
modifier is specified. IGNORE
也没有指定LOCAL
修饰符,则数据解释是限制性的。Errors terminate the load operation.错误终止加载操作。
Data interpretation is nonrestrictive if the SQL mode is nonrestrictive or the 如果SQL模式是非限制的,或者指定了IGNORE
or LOCAL
modifier is specified. IGNORE
或LOCAL
修饰符,则数据解释是非限制的。(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 无论SQL mode DATETIME
, DATE
, or TIME
column is inserted as the implicit default value, regardless of the SQL mode NO_ZERO_DATE
setting. NO_ZERO_DATE
设置如何,DATETIME
、DATE
或TIME
列的无效值都将作为隐式默认值插入。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)
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节,“分区选择”。
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
).MyISAM
、MEMORY
和MERGE
)。
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
的性能。
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 如果您使用的是C API,那么可以通过调用mysql_info()
function. mysql_info()
函数来获取有关该语句的信息。See mysql_info().请参阅mysql_info()。
对于基于语句的复制,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 DATA
与binlog_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节,“复制和加载数据”。
On Unix, if you need 在Unix上,如果需要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
):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进程读取数据。