加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

LOAD DATA INFILE Syntax--官方

发布时间:2020-12-11 23:55:38 所属栏目:MySql教程 来源:网络整理
导读:pre class="programlisting"LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'em class="replaceable" file_name ' [REPLACE | IGNORE] INTO TABLE em class="replaceable" tbl_name [CHARACTER SET em class="replaceable" charset_name ] [{FIELDS
For more information about?”-escape syntax,see?.

If the??character is empty,escape-sequence interpretation does not occur.

  • For output,it is used to prefix the following characters on output:

    The??character

  • The??character

  • The first character of the??and??values

  • ASCII??(what is actually written following the escape character is ASCII?”,not a zero-valued byte)

  • If the??character is empty,no characters are escaped and??is output as?,not?. 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??is an empty string and??is nonempty,lines are also terminated with?.

  • If the??and??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). Instead,column values are read and written using a field width wide enough to hold all values in the field. For?,?,BIGINT" href="https://dev.mysql.com/doc/refman/5.5/en/integer-types.html">,BIGINT" href="https://dev.mysql.com/doc/refman/5.5/en/integer-types.html">,and?,the field widths are 4,6,8,11,and 20,respectively,no matter what the declared display width is.

    ?is still used to separate lines. 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??values,as described later. Note that fixed-size format does not work if you are using a multibyte character set.

  • Handling of??values varies according to the??and??options in use:

    For the default??and??values,??is written as a field value of??for output,and a field value of?is read as??for input (assuming that the??character is?”).

  • If??is not empty,a field containing the literal word??as its value is read as a??value. This differs from the word??enclosed within??characters,which is read as the string.

  • If??is empty,??is written as the word?.

  • With fixed-row format (which is used when??and??are both empty),?is written as an empty string. Note that this causes both??values and empty strings in the table to be indistinguishable when written to the file because both are written as empty strings. 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??into a??column causes assignment of the implicit default value for the column's data type and a warning,or an error in strict SQL mode. Implicit default values are discussed in?.

    Some cases are not supported by?:

    Fixed-size rows (?and??both empty) and??or??columns.

  • If you specify one separator that is the same as or a prefix of another,??cannot interpret the input properly. For example,the following??clause would cause problems:

  • If??is empty,a field value that contains an occurrence of??or??followed by the??value causes??to stop reading a field or line too early. This happens because??cannot properly determine where the field or line value ends.

  • The following example loads all columns of the??table:

    By default,when no column list is provided at the end of the??statement,input lines are expected to contain a field for each table column. If you want to load only some of a table's columns,specify a column list:

    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.

    The column list can contain either column names or user variables. With user variables,the??clause enables you to perform transformations on their values before assigning the result to columns.

    User variables in the??clause can be used in several ways. The following example uses the first input column directly for the value of?,and assigns the second input column to a user variable that is subjected to a division operation before being used for the value of?:

    The??clause can be used to supply values not derived from the input file. The following statement sets?to the current date and time:

    You can also discard an input value by assigning it to a user variable and not assigning the variable to a table column:

    Use of the column/variable list and??clause is subject to the following restrictions:

    Assignments in the??clause should have only column names on the left hand side of assignment operators.

  • You can use subqueries in the right hand side of??assignments. 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??clause are not processed for the column/variable list or??clause.

  • User variables cannot be used when loading data with fixed-row format because user variables do not have a display width.

  • When processing an input line,??splits it into fields and uses the values according to the column/variable list and the??clause,if they are present. Then the resulting row is inserted into the table. If there are??or??triggers for the table,they are activated before or after inserting the row,respectively.

    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 table columns for which input fields are missing are set to their default values. Default value assignment is described in?.

    An empty field value is interpreted different from a missing field:

    For string types,the column is set to the empty string.

  • For numeric types,the column is set to?.

  • For date and time types,the column is set to the appropriate?

  • 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??or??statement.

    Treatment of empty or incorrect field values differs from that just described if the SQL mode is set to a restrictive value. For example,if?,conversion of an empty value or a value such as??for a numeric column results in an error,not conversion to 0. (With?,warnings occur rather than errors,even with a restrictive??value,because the server has no way to stop transmission of the file in the middle of the operation.)

    ?columns are set to the current date and time only if there is a??value for the column (that is,?) and the column is not declared to permit??values,or if the??column's default value is the current timestamp and it is omitted from the field list when a field list is specified.

    ?regards all input as strings,so you cannot use numeric values for??or??columns the way you can with??statements. All??and??values must be specified as strings.

    ?values cannot be loaded using binary notation (for example,?). To work around this,specify the values as regular integers and use the??clause to convert them so that MySQL performs a numeric type conversion and loads them into the??column properly:

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

    mysql> <strong class="userinput">SELECT BIN(b+0) FROM bit_test;
    +----------+
    | bin(b+0) |
    +----------+
    | 10 |
    | 2222211 |
    +----------+
    2 rows in set (0.00 sec)

    On Unix,if you need??to read from a pipe,you can use the following technique (the example loads a listing of the??directory into the table?):

     /mysql/data/db1/ls.dat &
    mysql -e "LOAD DATA INFILE 'ls.dat' INTO TABLE t1" db1
    

    Note that 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). If you do not do this,the pipe will block until data is read by the?mysql?process.

    When the??statement finishes,it returns an information string in the following format:

    Warnings occur under the same circumstances as when values are inserted using the??statement (see?Syntax”),except that??also generates warnings when there are too few or too many fields in the input row.

    You can use??to get a list of the first??warnings as information about what went wrong. See??Syntax”.

    If you are using the C API,you can get information about the statement by calling the??function. See.

    For partitioned tables using storage engines that employ table locks,such as?,any locks caused by??perform locks on all partitions of the table. This does not apply to tables using storage engines which employ row-level locking,such as?. For more information,see?.

    原文:https://dev.mysql.com/doc/refman/5.5/en/load-data.html

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    <pre class="programlisting">LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE '<em class="replaceable">file_name'
    [REPLACE | IGNORE]
    INTO TABLE <em class="replaceable">tbl_name
    [CHARACTER SET <em class="replaceable">charset_name]
    [{FIELDS | COLUMNS}
    [TERMINATED BY '<em class="replaceable">string']
    [[OPTIONALLY] ENCLOSED BY '<em class="replaceable">char']
    [ESCAPED BY '<em class="replaceable">char']
    ]
    [LINES
    [STARTING BY '<em class="replaceable">string']
    [TERMINATED BY '<em class="replaceable">string']
    ]
    [IGNORE <em class="replaceable">number {LINES | ROWS}]
    [(<em class="replaceable">col_name_or_user_var,...)]
    [SET <em class="replaceable">col_name = <em class="replaceable">expr,...]

    The??statement reads rows from a text file into a table at a very high speed.??is the complement of?. (See??Syntax”.) To write data from a table to a file,use?. To read the file back into a table,use?. The syntax of the??and??clauses is the same for both statements. Both clauses are optional,but??must precede??if both are specified.

    You can also load data files by using the?mysqlimport?utility; it operates by sending a?statement to the server. The??option causes?mysqlimport?to read data files from the client host. You can specify the??option to get better performance over slow networks if the client and server support the compressed protocol. See?mysqlimport?— A Data Import Program”.

    For more information about the efficiency of??versus??and speeding up?,see??Statements”.

    The file name must be given as a literal string. On Windows,specify backslashes in path names as forward slashes or doubled backslashes. The??system variable controls the interpretation of the file name.

    The character set indicated by the??system variable is used to interpret the information in the file.??and the setting of??do not affect interpretation of input. If the contents of the input file use a character set that differs from the default,it is usually preferable to specify the character set of the file by using the??clause. A character set of??specifies?

    ?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. For proper interpretation of file contents,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?statement in?mysql,be sure to use a??option so that output is written in the character set to be used when the file is loaded with?.

    It is not possible to load data files that use the?,?,or??character set.

    If you use?,execution of the??statement is delayed until no other clients are reading from the table. This affects only storage engines that use only table-level locking (such as?,?,and).

    If you specify??with a??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??is executing. This option affects the performance of??a bit,even if no other thread is using the table at the same time.

    With row-based replication,??is replicated regardless of MySQL version. With statement-based replication??is not replicated prior to MySQL 5.5.1 (see Bug #34628). For more information,see”.

    The??keyword affects expected location of the file and error handling,as described later.??works only if your server and your client both have been configured to permit it. For example,if?mysqld?was started with?,??does not work. See?”.

    The??keyword affects where the file is expected to be found:

    If??is specified,the file is read by the client program on the client host and sent to the server. The file can be given as a full path name to specify its exact location. If given as a relative path name,the name is interpreted relative to the directory in which the client program was started.

    When using??with?,a copy of the file is created in the server's temporary directory. This is?not?the directory determined by the value of??or?,but rather the operating system's temporary directory,and is not configurable in the MySQL Server. (Typically the system temporary directory is??on Linux systems and??on Windows.) Lack of sufficient space for the copy in this directory can cause the?statement to fail.

  • If??is not specified,the file must be located on the server host and is read directly by the server. The server uses the following rules to locate the file:

    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 one or more leading components,the server searches for the file relative to the server's data directory.

  • If a file name with no leading components is given,the server looks for the file in the database directory of the default database.

  • In the non-?case,these rules mean that a file named as??is read from the server's data directory,whereas the file named as??is read from the database directory of the default database. For example,if??is the default database,the following??statement reads the file??from the database directory for?,even though the statement explicitly loads the file into a table in the??database:

    For security reasons,when reading text files located on the server,the files must either reside in the database directory or be readable by all. Also,to use??on server files,you must have the??privilege. See?. For non-?load operations,if the?system variable is set to a nonempty directory name,the file to be loaded must be located in that directory.

    Using??is a bit slower than letting the server access the files directly,because the contents of the file must be sent over the connection by the client to the server. On the other hand,you do not need the??privilege to load local files.

    ?also affects error handling:

    With?,data-interpretation and duplicate-key errors terminate the operation.

  • With?,data-interpretation and duplicate-key errors become warnings and the operation continues because the server has no way to stop transmission of the file in the middle of the operation. For duplicate-key errors,this is the same as if??is specified.??is explained further later in this section.

  • The??and??keywords control handling of input rows that duplicate existing rows on unique key values:

    If you specify?,input rows replace existing rows. In other words,rows that have the same value for a primary key or unique index as an existing row. See??Syntax”.

  • If you specify?,input rows that duplicate an existing row on a unique key value are skipped.

  • If you do not specify either option,the behavior depends on whether the??keyword is specified. Without,an error occurs when a duplicate key value is found,and the rest of the text file is ignored. With?,the default behavior is the same as if??is specified; this is because the server has no way to stop transmission of the file in the middle of the operation.

  • To ignore foreign key constraints during the load operation,issue a??statement before executing?.

    If you use??on an empty??table,all nonunique indexes are created in a separate batch (as for?). Normally,this makes??much faster when you have many indexes. In some extreme cases,you can create the indexes even faster by turning them off with??before loading the file into the table and using??to re-create the indexes after loading the file. See??Statements”.

    For both the??and??statements,the syntax of the??and?clauses is the same. Both clauses are optional,but??must precede??if both are specified.

    If you specify a??clause,each of its subclauses (,?,and) is also optional,except that you must specify at least one of them.

    If you specify no??or??clause,the defaults are the same as if you had written this:

    (Backslash is the MySQL escape character within strings in SQL statements,so to specify a literal backslash,you must specify two backslashes for the value to be interpreted as a single backslash. The escape sequences?and??specify tab and newline characters,respectively.)

    In other words,the defaults cause??to act as follows when reading input:

    Look for line boundaries at newlines.

  • Do not skip over 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,?”,?”,and”?signify tab,newline,and backslash,respectively. See the discussion of??later for the full list of escape sequences.

  • Conversely,the defaults cause??to act as follows when writing output:

    Write tabs between fields.

  • Do not enclose fields within any quoting characters.

  • Use?”?to escape instances of tab,or?”?that occur within field values.

  • Write newlines at the ends of lines.

  • If you have generated the text file on a Windows system,you might have to use?to read the file properly,because Windows programs typically use two characters as a line terminator. Some programs,such as?WordPad,might use??as a line terminator when writing files. To read such files,use?.

    If all the lines you want to read in have a common prefix that you want to ignore,you can use?prefix_string'?to skip over the prefix,?and anything before it. If a line does not include the prefix,the entire line is skipped. Suppose that you issue the following statement:

    If the data file looks like this:

    The resulting rows will be??and?. The third row in the file is skipped because it does not contain the prefix.

    The?number?LINES?option can be used to ignore lines at the start of the file. For example,you can use?to skip over an initial header line containing column names:

    When you use??in tandem with??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. Otherwise,??will not interpret the contents of the file properly. Suppose that you use??to write a file with fields delimited by commas:

    To read the comma-delimited file back in,the correct statement would be:

    If instead you tried to read in the file with the statement shown following,it wouldn't work because it instructs??to look for tabs between fields:

    The likely result is that each input line would be interpreted as a single field.

    ?can be used to read files obtained from external sources. 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. 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:

    tbl_name
      FIELDS TERMINATED BY ',' ENCLOSED BY '"'
      LINES TERMINATED BY 'rn'
      IGNORE 1 LINES;
    

    If the input values are not necessarily enclosed within quotation marks,use??before the?keywords.

    Any of the field- or line-handling options can specify an empty string (). If not empty,the??and??values must be a single character. The?,?,and??values can be more than one character. For example,to write lines that are terminated by carriage return/linefeed pairs,or to read a file containing such lines,specify a??clause.

    To read a file containing jokes that are separated by lines consisting of?,you can do this

    ?controls quoting of fields. For output (),if you omit the word?,all fields are enclosed by the??character. An example of such output (using a comma as the field delimiter) is shown here:

    If you specify?,the??character is used only to enclose values from columns that have a string data type (such as?,?,?,or?):

    Note that occurrences of the??character within a field value are escaped by prefixing them with the?character. Also note that if you specify an empty??value,it is possible to inadvertently generate output that cannot be read properly by?. 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:

    For input,the??character,if present,is stripped from the ends of field values. (This is true regardless of whether??is specified;??has no effect on input interpretation.) Occurrences of the?character preceded by the??character are interpreted as part of the current field value.

    If the field begins with the??character,instances of that character are recognized as terminating a field value only if followed by the field or line??sequence. To avoid ambiguity,occurrences of the?character within a field value can be doubled and are interpreted as a single instance of the character. For example,if??is specified,quotation marks are handled as shown here:

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

    ?controls how to read or write special characters:

    For input,if the??character is not empty,occurrences of that character are stripped and the following character is taken literally as part of a field value. 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??handling are described later in this section.