MySQL’s INTO OUTFILE for loading file

MySQL: SELECT / LOAD in Batch

Here’s are the MySQL commands to extract data from  table and to load data into tables.
Unlike other DBMS, MySQL offers SELECT..INTO.. and LOAD DATA.. commands are quite faster and convenient. Data would be fetched and loaded in bacthes.

Sample query to extract data from a table:

SELECT *
FROM tmp.my_table
INTO OUTFILE ‘/file_path/filename.csv’
FIELDS TERMINATED BY ‘|$|’
LINES TERMINATED BY ‘\n’;

You can select particular columns, instead of  ‘*’ .

OUTFILE should be given with file path and name. By default, file will be created inside data directory.
Important thing to be noted here is, the file will be generated inside the local host only.
Means, if you’re executing above query on server S2, fetching data from S1, the file will be generated inside S1 itself.

Also, to execute above command, it requires FILE Privilege for MySQL user and the destination directory should also be having server level MySQL user privilege.

Below is the query that an be used to load data in batches:

LOAD DATA LOCAL
INFILE ‘/file_path/filename.csv’
INTO TABLE tmp.my_table
CHARACTER SET utf8
FIELDS TERMINATED ‘|$|’
LINES TERMINATED BY ‘\n’;

You can use, ‘IGNORE INTO’ instead of ‘INTO’, to ignore the duplicate records.
CHARACTER SET is mentioned explicitly as the data being loaded is having local languages(like kannada, hindi, telugu,..). Without this, data won’t be visible – scrambled.

  • Ask Question