Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
In real-world data engineering, backend development, and business analytics, you will constantly exchange data with external business teams who work in Microsoft Excel or Google Sheets. Mastering how to rapidly bulk-import hundreds of thousands of spreadsheet rows using LOAD DATA INFILE and export query results to CSV files is an essential practical skill.
LOAD DATA INFILEWhile inserting 100,000 rows via standard SQL INSERT statements can take minutes, MySQL's LOAD DATA INFILE engine can parse and ingest the same data in under two seconds!
LOCAL: Instructs the MySQL client to read the file from your local machine and transmit it to the server.FIELDS TERMINATED BY ',': Specifies the column delimiter (comma for CSV, \t for TSV).ENCLOSED BY '"': Handles strings that contain commas inside quotes (e.g., "Flat 4, Nariman Point").LINES TERMINATED BY '\r\n': Standard Windows line endings (\n on Linux/macOS).IGNORE 1 ROWS: Skips the first header row containing column titles.(column_list): Maps CSV fields to specific table columns.local_infile Security in MySQLBecause reading local client files has security implications, MySQL disables LOCAL INFILE by default in modern installations. To enable it:
And when connecting from the terminal, append the --local-infile=1 flag:
INTO OUTFILE)You can export any query directly to a comma-separated text file on the server using INTO OUTFILE:
secure_file_priv Restriction: For security, MySQL will refuse to write files to arbitrary directories like C:\ or /etc/. It restricts file exports to the directory configured in SHOW VARIABLES LIKE 'secure_file_priv';.If you prefer a point-and-click GUI workflow without security permissions tweaking:
SELECT query -> Click the "Export" floppy disk icon in the Result Grid toolbar -> Choose CSV.IGNORE 1 ROWS when importing CSVs with header columns. Otherwise, the string "fee_paid" will attempt to insert into a DECIMAL column, throwing an error!\r\n), while files generated on Linux/macOS use Line Feed (\n). Always check your file's line ending format.LOAD DATA INFILE skips the first line of a CSV file containing column headers?INTO OUTFILE?ENCLOSED BY '"' clause accomplish during CSV import?LOCAL keyword used in LOAD DATA LOCAL INFILE?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Restoring Databases & Running SQL Dump Scripts | Project 1: Student Information System Database |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.