How can we transfer information between MySQL and data files?


Transferring the information between MySQL and data files mean importing data from data files into our database or exporting data from our database into files. MySQL is having two statements that can be used to import or export data between MySQL and data files −

LOAD DATA INFILE

This statement is used for importing the data from data files into our database. It reads data records directly from a file and inserts them into a table. Its syntax would be as follows −

Syntax

LOAD DATA LOCAL INFILE '[path/][file_name]' INTO TABLE [table_name ];

Here, the path is the address of the file.
file_name is the name of the .txt file
table_name is the table where the data will be loaded.

SELECT … INTO OUTFILE

This statement is used for exporting the data from our database into data files. This statement writes the result of a SELECT operation to a file. Its syntax would be as follows −

Syntax

SELECT … INTO OUTFILE '[path/][file_name]' FROM TABLE [table_name ];

Here, the path is the address of the file.
file_name is the name of the .txt file
table_name is the table from where the data, after SELECT statement, would be selected.

Both the above-described statements are similar in the sense that they are related to the transfer of data either from the data file to database or from the database to a data file.

Updated on: 20-Jun-2020

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements