Load a large amount of data with LOAD DATA INFILE
suggest changeThis example loads a semicolon-delimited file. VARCHAR needs a length. Use the line ending the file actually has: \n on Unix, \r\n on Windows.
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(50) NOT NULL,
`sex` VARCHAR(10) NOT NULL,
`designation` VARCHAR(50) NOT NULL,
`dob` VARCHAR(20) 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 ';'
LINES TERMINATED BY '\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 ';'
LINES TERMINATED BY '\n'
(id, name, sex, designation, @dob)
SET dob = STR_TO_DATE(@dob, '%d-%b-%Y');
This example of LOAD DATA INFILE does not specify all the available features.
You can see more references on LOAD DATA INFILE here.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents