Import CSV File Into MySQL Table
Importing data from a CSV (Comma-Separated Values) file into a MySQL table is a common operation, often used for bulk data insertion. Here’s how you can do it efficiently.
1. Import CSV Using LOAD DATA INFILE
The fastest way to import a CSV file into MySQL is by using the LOAD DATA INFILE statement.
Syntax:
Example:
Explanation:
✔ FIELDS TERMINATED BY ',' → Specifies the delimiter (comma).
✔ ENCLOSED BY '"' → Ensures that values inside quotes are handled correctly.
✔ LINES TERMINATED BY '\n' → Defines row separation.
✔ IGNORE 1 ROWS → Skips the first row (column headers).
⚠ Common Error:
- If you get a "The MySQL server is running with the --secure-file-priv option" error, check the directory using:
- Move your CSV file to this directory before running
LOAD DATA INFILE.
2. Import CSV Using MySQL Workbench
Steps:
- Open MySQL Workbench.
- Select your database.
- Click Table > Import Wizard.
- Select CSV file and configure settings.
- Click Next to finish the import.
3. Import CSV Using INSERT INTO ... SELECT
If LOAD DATA INFILE is restricted, use INSERT INTO.
Example:
For bulk insertion, use a script to read a CSV file and generate multiple INSERT statements.
4. Import CSV Using the MySQL Command Line
If you're using the terminal, use:
5. Handling Common Issues
| Issue | Solution |
|---|---|
secure_file_priv error | Move the file to the specified directory |
| Encoding issues | Ensure CSV uses UTF-8 |
| Incorrect column mapping | Specify columns explicitly in LOAD DATA INFILE |
Summary
| Method | Pros | Cons |
|---|---|---|
LOAD DATA INFILE | Fastest import method | Requires file permission setup |
| MySQL Workbench | User-friendly GUI | Slower for large files |
INSERT INTO ... SELECT | Works without file access | Not ideal for large datasets |
| MySQL CLI | Simple for quick imports | Requires correct file path |
Would you like help automating CSV imports in MySQL? š

