当前位置:网站首页>mysql load data infile
mysql load data infile
2022-06-25 19:14:00 【kankan231】
LOAD DATA INFILE Statement reads rows from a text file to a table at a high speed . It is SELECT ... INTO OUTFILE A supplement to ,SELECT ... INTO OUTFILE Is to write the data in the table to a file ,LOAD DATA INFILE Import the contents of the file into the table . Use both FIELDS And LINES sentence , Two statements are optional , At the same time FIELDS To be in LINES front .
We can also pass mysqlimport Tools to import data , It is essentially sending LOAD DATA INFILE Statement to the server .
2 grammar
LOAD DATA The grammar is as follows (mysql5.6 And above ):
3 Presentation data preparation 3.1 establish test database And table
3.3 Export data
View the exported data file

4 Grammar explanation and example analysis file_name: file name file_name A string is required , stay windows Next , The path can be a slash '/' Or double backslash '\\'(windows Verify below ).
PARTITION:MySQL 5.6.2 And subsequent versions ,LOAD DATA INFILE Start supporting PARTITION Partition options . For partitioned tables, a storage engine using table locks should be used , for example myisam, Not applicable to the storage engine with row lock , Such as Innodb, because LOAD DATA You cannot remove any partition locks .
CHARACTER SET: from character_set_database The character set indicated by the system variable is used to interpret the information in the file .SET NAMES and character_set_client The setting of does not affect the interpretation of the input . If the character encoding in the file is inconsistent with the default , It is best to CHARACTER SET Statement specifies the encoding method , This requires us to go through mysqldump or SELECT ... INTO OUTFILE When exporting a file, use the parameter --default-character-set Specify the encoding format of the exported file .
Be careful : Importing files does not support ucs2, utf16, utf16le, or utf32 Coding format .
LOW_PRIORITY: This parameter is applicable to the table lock storage engine , such as MyISAM, MEMORY, and MERGE, If a client program reads a table during writing , Writing will be delayed , Continue writing until no client program reads the table .
CONCURRENT: Use this parameter , Allow other client programs to read table contents during writing .
The impact on replication : Use statement based formatting (statement-based) Copy , stay mysql5.5.1 Before ,CONCURRENT Parameters cannot be copied . Use row based formatting (row-based) Copy , stay mysql5.1 And subsequent versions , Can be copied correctly .mysql5.6 Think of using LOAD DATA INFILE It's not safe , When using statement-based A warning will be issued when the log is formatted , Use mixed Format log will be stored as row Format .
LOCAL: This keyword affects the expected read location and error handle of the file . This option should be enabled on both the client and the server , If mysqld The startup parameters for include local-infile=0, Then specifying this parameter is invalid .
Impact on file lookup :
Specify this option : The file will be searched by the client program on the client host , Then send it to the server . You can specify the absolute path and relative path of the file , If you specify a relative path, you will find it in the client program directory .
When importing, the file will be copied to the temporary folder of the server first (linux Next is /tmp, windows Next is C:\WINDOWS\TEMP), If the temporary folder space is insufficient, it will cause LOAD DATA Failure .
The option is not specified : The file will be searched by the server program on the server host , The search rules are as follows :
If an absolute path name is given , Then the server uses this path name .
If a relative path name with one or more boot components is given , Then the server will search relative to the server ( Data directory ) The file of .
If you give a file name without a boot component , The server will default to ( database ) Of ( Database directory ) Looking for files in .
for example :./myfile.txt Will be in the database data Directory lookup ,myfile.txt The default database that will be used when we connect , Such as test The data directory of .
LOCAL Impact on error handles :
Use LOAD DATA INFILE, Data interpretation and primary key conflict error Will terminate operation .
Use LOAD DATA LOCAL INFILE Data interpretation and primary key conflict error Will become warning, Operation continues , because mysql server There is no way to end the transfer of data during the operation . For primary key conflicts error Speaking of , This method is similar to specifying IGNORE.
REPLACE And IGNORE: Control the repetition of the entered row and the unique primary key .
REPLACE: The input line replaces the existing line . That is, the repetition with the primary key or unique index column .
IGNORE: The input row duplicates the existing row primary key or unique index column , Then discard .
If neither is specified : The behavior will depend on whether or not it is specified LOCAL Parameters .
Ignored during data import ( Foreign key constraints ) Can be in LOAD DATA Pre execution SET foreign_key_checks = 0 .
LINES STARTING BY 'prefix_string': If all the rows you want to read in contain a common prefix that we want to ignore , You can use 'prefix_string' To skip the prefix ( And the prefix
All characters before ). If a row does not include a prefix , The entire line is skipped
for example :
Then the contents we read include ("abc",1) and ("def",2), The third line is skipped directly .
IGNORE number LINES: This option can be used to ignore lines at the beginning of the file . for example , We can use IGNORE 1 LINES To skip a starting header row containing column names .
FIELDS and LINES:
about LOAD DATA INFILE and SELECT ... INTO OUTFILE,FIELDS and LINES The syntax of each statement is the same , Both are optional , But at the same time FIELDS The sentence should be put in LINES In front of the statement .
If we specify FIELDS sentence , Its clause (TERMINATED BY, [OPTIONALLY] ENCLOSED BY, and ESCAPED BY) Also optional , However, at least one must be specified .
If FIELDS and LINES None of us specified , The default syntax is :
all field- or line-handling Options can specify an empty string (''). If the string is not empty , be FIELDS [OPTIONALLY] ENCLOSED BY and FIELDS ESCAPED BY The value must be a single character .FIELDS TERMINATED BY, LINES STARTING BY and LINES TERMINATED BY The value can exceed one character . for example , To write by carriage return / A line that ends with a pair of characters , Or read
Files containing such lines , Then one should be specified LINES TERMINATED BY ‘\r\n’ Clause .
FIELDS [OPTIONALLY] ENCLOSED BY : about SELECT ... INTO OUTFILE Output , If not OPTIONALLY Options , All fields will be ENCLOSED BY Specified character wrap , for example :
If we specify OPTIONALLY, Only string data type ( Such as CHAR, BINARY, TEXT, or ENUM) The field of will be ENCLOSED BY Specified character wrap , for example :
Be careful , If... Appears in the field value ENCLOSED BY character , By using ESCAPED BY Character as prefix , Yes ENCLOSED BY Character escape . in addition , it is to be noted that , If an empty... Is specified ESCAPED BY value , It may be generated that cannot be LOAD DATAINFILE Output value read correctly . for example :
FIELDS ESCAPED BY: Used to control how to read and write special characters , As an example above , Specify... When exporting and importing FIELDS ESCAPED BY Is double quotation mark ["] Can be correctly imported , The export format is as follows , Double quotation marks inside the field ["] It's escaped .
For input :If FIELDS ESCAPED BY The specified character is not empty , Then the character is removed during input , Subsequent content is added to the field . Some two character string sequences where the first character is an escape character '\' It's an exception , Such as \0,\b,\n,\r,\t,\Z\,\N etc. , These character sequences themselves have a special meaning .
If FIELDS ESCAPED BY The specified character is empty , There will be no interpretation of escape sequences .
For output :
If FIELDS ESCAPED BY The specified character is not empty , Characters are prefixed to the following output .
【********** Here to be improved ***********】
In certain circumstances ,field- and line-handling Options interact :
If LINES TERMINATED BY Is an empty string ,FIELDS TERMINATED BY Non empty , Line to FIELDS TERMINATED BY End with the specified string .
If FIELDS TERMINATED BY And FIELDS ENCLOSED BY Values are all empty (''), Fixed rows will be used ( No segmentation ) Format . Use fixed line format , There will be no delimiters between fields ( Line terminators can still be used ), The column field data is read and written according to the width defined by the field , Such as TINYINT, SMALLINT, MEDIUMINT, INT, and BIGINT, The field widths are 4, 6, 8, 11, and 20.
Unsuitable for use LOAD DATA INFILE The situation of
Use fixed line format ( namely FIELDS TERMINATED BY and FIELDS ENCLOSED BY All empty. ) The column field type is BLOB or TEXT.
The specified delimiter is the same as other option prefixes ,LOAD DATA INFILE Unable to interpret input correctly . for example :
If FIELDS ESCAPED BY It's empty , The field value contains FIELDS ENCLOSED BY Specify the characters , perhaps LINES TERMINATED BY The characters in FIELDS TERMINATED BY Before , Will lead to premature stop LOAD DATA INFILE operation . because LOAD DATA INFILE Cannot accurately determine the end of a row or column .
Select the imported Columns :
The following statement will import all the columns of the file
LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata;If we want to import some columns of the table , You need to specify a list of columns
LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata (col1,col2,...);
Be careful : If sql-mode For the strict mode of things STRICT_TRANS_TABLES, Errors will be reported when importing some columns .
If the column order of the input file is different from that of the table , We must specify a list of columns , otherwise mysql You cannot match the fields of the input file with the columns of the table .
The list of columns can contain column names or user variables , Before writing a column, we need to use SET Statement to convert user variables . Yes set Statements and user variables can be used as follows :
Method 1: Before the user variable is used for the first column , First assign the value of the first column to the user variable , After the division operation, input to c1.

Method 2: hold c3 Set the column to the current time (sql-mode Use strict thing patterns STRICT_TRANS_TABLES Will report a mistake )
Method 3: Assign input to user variables , Instead of assigning user variables to columns in the table , To discard this input value .
Import data through pipes :
stay unix In the system , If we want to go from the pipe (pipe) in load data, You need to use the following methods :Be careful :sql-mode Use strict thing patterns STRICT_TRANS_TABLES Will report a mistake
Write data to the pipeline in another window
notes : You can read or write the pipeline first , Whoever comes first can , Before all the data written into the pipeline is read out , In a blocking state .
5 matters needing attention
LOAD DATA INFILE You can read external files , Such as comma separated by other databases or programs CSV File format .
When we use SELECT ... INTO OUTFILE Export data from database to file , And then through LOAD DATA INFILE Read the file to the database , The of these two statements field and lines Options must match , otherwise LOAD DATA INFILE Will not correctly interpret the contents of the file .
6 Common mistakes
6.1 sql_mode by STRICT_TRANS_TABLES, That is, strict transaction mode , It's easy to make mistakes , for example :
see sql_mode
Only when the strict transaction mode is cancelled can
6.2 windows Some mistakes on
mysql5.5.30
边栏推荐
- Apifox simple understanding -- the integrator of web side testing
- shell-跳出循环-shift参数左移-函数的使用
- 广州华锐互动VR全景为各行各业带来发展
- Analysis on the market scale and pattern of contrast agents in China in 2021: Jiangsu Hengrui pharmaceutical, general electric, Yangzijiang Pharmaceutical Group, Bayer and bleco account for more than
- 云上弹性高性能计算,支持生命科学产业高速发展、降本增效
- 【C语言练习——打印上三角及其变形(带空格版)】
- Ali visual AI training camp -day03- construction of electronic photo album (face and expression recognition)
- 一、HikariCP获取连接流程源码分析一
- R language plot visualization: plot visualization of two-dimensional histogram contour (basic 2D histogram contour)
- From the perspective of the core competitiveness of ABT, the Internet giant, who fell first
猜你喜欢

Google cloud SSH enable root password login

One night I worked as an XPath Terminator: XPath Helper Plus

Vulnhub range the planes: mercury

Combing the latest Data Mining Event Scheme!

Gbpnzd firm offer for 14 months, simulation for 19 months, test stable

【C语言练习——打印上三角及其变形(带空格版)】

ECS 7-day practical training camp (Advanced route) -- day04 -- build a portal using ECs and polardb

rmi-registry-bind-deserialization

Analysis on the market scale and pattern of contrast agents in China in 2021: Jiangsu Hengrui pharmaceutical, general electric, Yangzijiang Pharmaceutical Group, Bayer and bleco account for more than

LeetCode-78-子集
随机推荐
Web development solution to cross domain problems
R语言plotly可视化:plotly可视化二维直方图等高线图(Basic 2D Histogram Contour)
Android Development Notes - Quick Start (from sqllite to room licentiousness) 2
Ali visual AI training camp -day03- construction of electronic photo album (face and expression recognition)
Why are life science enterprises on the cloud in succession?
One night I worked as an XPath Terminator: XPath Helper Plus
solidity日期工具
想知道新股民怎样炒股票开户?在线开户安全么?
Validation of TTF font by validator of laravel
Leetcode-101-symmetric binary tree
Huawei released two promotion plans to promote AI talent development and scientific research innovation
6、 Configuration resolution of hikariconfig
Current situation and trend analysis of China's glass packaging containers in 2021: the revenue of glass packaging containers increases year by year [figure]
Oriental Wealth function (the most complete edition of Childe Yong)
二、HikariCP获取连接流程源码分析二
QQ robot official plug-in loading configuration method [beta2 version]
Paddleocr learning (II) paddleocr detection model training
Network security detection and prevention test questions (I)
一、HikariCP获取连接流程源码分析一
shell-跳出循环-shift参数左移-函数的使用