MOCKSTACKS
EN
Questions And Answers

More Tutorials









MYSQL LOAD DATA INFILE

LOAD DATA INFILE to load large amount of data to database


Consider the following example assuming that you have a ';'-delimited CSV to load into your database.

1;max;male;manager;12-7-1985
2;jack;male;executive;21-8-1990
.
.
.
1000000;marta;female;accountant;15-6-1992

Create the table for insertion.

CREATE TABLE `employee` ( `id` INT NOT NULL ,
 `name` VARCHAR NOT NULL,
 `sex` VARCHAR NOT NULL ,
 `designation` VARCHAR NOT NULL ,
 `dob` VARCHAR NOT NULL );

Use the following query to insert the values in that table.

LOAD DATA INFILE 'path of the file/file_name.txt'
INTO TABLE employee
FIELDS TERMINATED BY ';' //specify the delimiter separating the values
LINES TERMINATED BY '\r\n'
(id,name,sex,designation,dob)

Consider the case where the date format is non standard.

1;max;male;manager;17-Jan-1985
2;jack;male;executive;01-Feb-1992
.
.
.
1000000;marta;female;accountant;25-Apr-1993

In this case you can change the format of the dob column before inserting like this.

LOAD DATA INFILE 'path of the file/file_name.txt'
INTO TABLE employee
FIELDS TERMINATED BY ';' //specify the delimiter separating the values
LINES TERMINATED BY '\r\n'
(id,name,sex,designation,@dob)
SET date = STR_TO_DATE(@date, '%d-%b-%Y');

This example of LOAD DATA INFILE does not specify all the available features.

Conclusion

In this page (written and validated by ) you learned about MYSQL LOAD DATA INFILE . What's Next? If you are interested in completing MYSQL tutorial, your next topic will be learning about: MYSQL Unions.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.