File data into Mysql

mysql导入数据load data infile用法
基本语法:
  load data [low_priority] [local] infile ‘file_name txt’ [replace | ignore]
  into table tbl_name
  fields
  [terminated by’t’]
  [OPTIONALLY] enclosed by ‘’]
  [escaped by’’ ]]
  [lines terminated by’n’]
  [ignore number lines]
  [(col_name, )]

load data infile statement from a text file read at high speed into a table. Before using this command, mysqld process (service) must have been in operation. For security reasons, when reading a file on the server is located, the file must be in the database directory or be readable by all. In addition, in order to use load data infile files on the server, you must have permission to file on the server host.

1, if you specify the keyword low_priority, then MySQL will wait until there are no other people when reading this table, only then insert the data. You can use the following commands:
  Load Data LOW_PRIORITY INFILE "/ Home / Mark / SQL Data" INTO the Orders Table;

2. If you specify a local keyword indicates that the file is read from the client host. If the local is not specified, the file must be located on the server.

3, replace the control keyword and ignore repeated processes existing unique key record. If you specify replace, new rows replace existing rows with the same unique key value. If you specify ignore, skip the input duplicates existing line of unique keys. Either option if you do not specify when to find duplicate keys, an error, and the rest of the text file is ignored. For example:
  Load Data LOW_PRIORITY INFILE "/ Home / Mark / SQL Data" Replace the Orders Table INTO;

4, separator
(1) fields keyword specifies the format division field, if the keyword is used, MySQL parser would like to see at least one of the following options:
    terminated by delimiter: What is the meaning of the character as a separator character
    enclosed by the field enclosed characters
    escaped by an escape character
    terminated by field separators is described, the default is the tab character (\ T)
    enclosed by enclosed character field is described.
    escaped by an escape character described. The default is a backslash (backslash: \)
   , for example: load data infile "/ home / mark / Orders txt" replace into table Orders fields terminated by ',' enclosed by ' "';

(2) keyword specifies lines each record delimiter defaults '\ n' is the line breaks
  if the two fields are specified, before the lines that have fields. If you do not specify the fields keyword, the default value and write the same: fields terminated by '\ t' enclosed by '' '' escaped by '\'
  If you do not specify a lines clause, the default value is the same as written : lines terminated by '\ n'
  , for example: load data infile "/jiaoben/load.txt" replace into table test fields terminated by ',' lines terminated by '/ n';

5, load data infile specified columns can be imported to the database file. When part of the imported data we want ,, need to add some columns (column / field / field) to the MySQL database to accommodate some additional needs. For example, we upgraded from Access database to MySQL database, the
following example shows how to import the data to the specified column (field) in:
  the Load the Data infile "/ Home / TXT the Order" INTO the Table the Orders (Order_Number, Order_Date, Customer_ID );

6. When looking for files on the server host, the server uses the following rules:
(1) If given an absolute pathname, the server uses the path name.
(2) if a given one or more of the front member relative path name, directory server searches the relative data file server.
(3) If a file name is given without leading components, the server looks for the file in the current database directory database.
For example: / myfile txt "file is read from the data given in the directory server, as" a document myfile txt "given is read from the current database directory database.

Note: a null field \ represents N

Released eight original articles · won praise 6 · views 2530

Guess you like

Origin blog.csdn.net/weixin_42374329/article/details/90448534