๐Ÿ“ฅ MySQL Import / ๐Ÿ“ค Export CSV โ€“ Complete Guide for Developers & DBAs


๐Ÿงฒ Introduction โ€“ Why Learn CSV Import and Export in MySQL?

Whether you’re migrating data, building dashboards, or integrating with external systems, CSV import/export is one of the most common tasks in MySQL. MySQL makes it easy to load CSV files into tables and export query results as CSV files, supporting a wide range of real-world scenarios like:

  • ๐Ÿ“Š Data migration from Excel
  • ๐Ÿ” ETL pipelines and automation
  • ๐Ÿ“ค Backups and offline reports
  • ๐Ÿ”„ Data synchronization between systems

๐ŸŽฏ In this tutorial, youโ€™ll learn:

  • How to import CSV to MySQL using LOAD DATA INFILE
  • How to export MySQL query results to CSV
  • GUI tools vs CLI methods
  • Real-world use cases and best practices

๐Ÿ“ฅ How to Import CSV File into MySQL

โš™๏ธ Using LOAD DATA INFILE

LOAD DATA INFILE '/path/to/file.csv'
INTO TABLE employees
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

๐Ÿ” Explanation:

ClausePurpose
FIELDS TERMINATED BY ','CSV separator (comma)
ENCLOSED BY '"'Wrap values in double quotes
LINES TERMINATED BY '\n'New row on new line
IGNORE 1 ROWSSkip header row

๐Ÿ’ก Tip:

  • Ensure file is accessible by the MySQL server.
  • Use LOCAL if uploading from client machine:
LOAD DATA LOCAL INFILE 'employees.csv' INTO TABLE employees ...

๐Ÿ“˜ Example Table & CSV

CSV File (employees.csv):

id,name,department,salary
1,Alice,HR,60000
2,Bob,Sales,70000
3,Charlie,IT,75000

Matching Table:

CREATE TABLE employees (
  id INT,
  name VARCHAR(50),
  department VARCHAR(50),
  salary DECIMAL(10,2)
);

๐Ÿ“ค How to Export Data from MySQL to CSV

๐Ÿงช Using INTO OUTFILE

SELECT id, name, department, salary
FROM employees
INTO OUTFILE '/var/lib/mysql-files/employees_export.csv'
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\n';

๐Ÿ” Notes:

  • File is written on the MySQL server, not on your client.
  • The path must be writable by MySQL and in the secure-file-priv directory (check it via: SHOW VARIABLES LIKE 'secure_file_priv';).

๐Ÿงฐ MySQL Workbench Import / Export CSV

๐Ÿ“ฅ Import CSV via MySQL Workbench

  1. Open MySQL Workbench
  2. Go to Table Data Import Wizard
  3. Choose CSV file and target table
  4. Map columns
  5. Click Next โ†’ Import

๐Ÿ“ค Export CSV via MySQL Workbench

  1. Right-click the table โ†’ Table Data Export Wizard
  2. Choose columns or use a custom SQL query
  3. Select file path and format
  4. Click Next โ†’ Export

๐Ÿ’ก Best Practices

๐Ÿ“˜ Use Matching Column Order: Ensure your CSV column order aligns with the MySQL table.

๐Ÿ“˜ Data Sanitization: Clean the CSV file to avoid errors due to missing quotes or malformed lines.

๐Ÿ“˜ Use Transactions (Optional):

START TRANSACTION;
-- LOAD DATA INFILE ...
COMMIT;

๐Ÿ“˜ Secure Your File Paths: Avoid using unrestricted paths on shared servers.

โš ๏ธ Pitfall: If secure_file_priv is set, you must place your file inside the permitted directory.


๐Ÿš€ Real-World Use Cases

Use CaseHow CSV Helps
๐Ÿ“ˆ Excel IntegrationEasily export to Excel-readable CSV files
๐Ÿ› ๏ธ Bulk Data UploadInsert thousands of rows at once from files
๐Ÿ”„ External SyncExport MySQL data to sync with APIs or partners
๐Ÿ“š Database MigrationMove data between MySQL instances or other RDBMS

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

Using CSV for import and export in MySQL is fast, efficient, and essential for any backend developer or DBA.

๐Ÿ” Key Takeaways:

  • Use LOAD DATA INFILE for importing CSV to MySQL.
  • Use SELECT ... INTO OUTFILE to export MySQL data to CSV.
  • Tools like MySQL Workbench simplify the process.

โš™๏ธ Real-World Relevance:
Essential for reporting, migrations, integration, and automation in any data-driven application.


โ“ FAQ โ€“ MySQL CSV Import / Export

โ“ How do I fix “The MySQL server is running with the –secure-file-priv option”?

โœ… Run:

SHOW VARIABLES LIKE 'secure_file_priv';

Then place your file in that directory.


โ“ Can I import CSV without a header row?

โœ… Yes. Simply omit IGNORE 1 ROWS in your LOAD DATA INFILE query.


โ“ Can I automate CSV import/export with cron jobs?

โœ… Yes. Use CLI + .sql files + mysql or mysqldump commands in shell scripts.


โ“ What are alternatives to CSV in MySQL?

โœ… JSON, XML, and database dumps (mysqldump for .sql files).


โ“ How to export with a WHERE clause?

โœ… Example:

SELECT * 
FROM orders 
WHERE status = 'completed' 
INTO OUTFILE 'completed_orders.csv'
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';

Share Now :

Leave a Reply

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

Share

๐Ÿ“ฅ MySQL Import / ๐Ÿ“ค Export CSV

Or Copy Link

CONTENTS
Scroll to Top