Database

Export Data into file using MySQL

With MySQL we can easily export our selected rows to the files. MySQL has several syntax which can export the retrieved rows into the file.

Yes we can import these rows back to database tables also.

Lets see how you can export rows to the file.

Export data to File

[cc lang=”mysql”]
SELECT * FROM table_name INTO OUTFILE “rows.txt”
[/cc]

So after executing above query you can see the rows.txt on the server folder where database data files are stored.

Now import the exported files into database tables.

Import data from File

[cc lang=”mysql”]
LOAD DATA INFILE ‘rows.txt’ INTO TABLE test;

// We can have other options like below

LOAD DATA INFILE ‘rows.txt’ INTO TABLE test
FIELDS TERMINATED BY ‘,’ LINES STARTING BY ‘xx’;

[/cc]

Note: [code]FIELDS TERMINATED BY[/code] and [code]LINES STARTING BY[/code] are used for the formatting purspose.

Shares:
  • Tjorriemorrie
    Tjorriemorrie
    September 23, 2011 at 10:35 am

    Is this still appropriate for extremely large databases (with millions of rows)? At our company we can’t afford the table getting locked for very long. Any suggestions?

    Reply
    • Avinash
      September 24, 2011 at 1:34 pm

      Hi Tjorriemorrie ,

      I have tried with table with 13983816 rows. This table is about 1 GB in size.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *