Load data infile will read the content from the text file and insert into table. This process is very fast in MySQL.
Below is the Syntax suggested by MySQL in their documentation:
Procedure 1:
Text file contains header:
Similarly, if your text file doesn't contains any header then you can remove the IGNORE 1 LINES part from the actual query and run.
Procedure 2:
Text file contains different sequence of columns when compared with table.
In this case compare the columns with the file and provide the field names in the query what will match to that in the text file respectively.
Let's say in your text file column_1 is in fifth position, then provide the columns which actually are there in the text file. Here you need to follow the text file sequence instead of table.
Below is the Syntax suggested by MySQL in their documentation:
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'Will look into some of the procedures to load data into table using an external text file:file_name
' [REPLACE | IGNORE] INTO TABLEtbl_name
[PARTITION (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
,...)] [SETcol_name
=expr
,...]
Procedure 1:
Text file contains header:
LOAD DATA INFILE ‘path/to/example.csv’ INTO TABLE example FIELDS TERMINATED BY ‘,’ LINES TERMINATED BY ‘\n’ IGNORE 1 LINES;Above, IGNORE 1 LINES considers that you have a header in your actual text file.
Similarly, if your text file doesn't contains any header then you can remove the IGNORE 1 LINES part from the actual query and run.
Procedure 2:
Text file contains different sequence of columns when compared with table.
In this case compare the columns with the file and provide the field names in the query what will match to that in the text file respectively.
LOAD DATA INFILE ‘path/to/example.csv’ INTO TABLE example FIELDS TERMINATED BY ‘,’ LINES TERMINATED BY ‘\n’ IGNORE 1 LINES (column_1,column_3,column_5,column_2);What if the sequential order is different in table and text file, we don't need to about this case
Let's say in your text file column_1 is in fifth position, then provide the columns which actually are there in the text file. Here you need to follow the text file sequence instead of table.
ReplyDeleteI have a doubt, I am trying to do everything as the tutorial tells me but the problem is that only mySQL assumes the first line of the file, I cannot insert the missing 8562 lines of the document.