LearnContact
Lesson 6818 min read

Importing & Exporting Databases

Learn how to move data in and out of MySQL using SELECT INTO OUTFILE, LOAD DATA INFILE, and Workbench's import/export wizards.

Introduction

Data rarely lives in just one place. You might need to hand a dataset to a colleague, load a spreadsheet of new products into your store's database, or move data between two separate MySQL servers. All of this comes down to importing and exporting.

This lesson covers the two core SQL commands for moving data through CSV files — SELECT ... INTO OUTFILE and LOAD DATA INFILE — as well as MySQL Workbench's friendlier, GUI-based wizard for the same job.

What You Will Learn
  • How to export query results to a CSV file with SELECT INTO OUTFILE.
  • How to import data from a CSV file with LOAD DATA INFILE.
  • How to use MySQL Workbench's Table Data Export/Import Wizard.
  • Common real-world reasons for importing and exporting data.

Why Import and Export Data?

CSV (Comma-Separated Values) is one of the most universal data formats — it can be opened by Excel, Google Sheets, most programming languages, and virtually every database system. Exporting to CSV, or importing from it, is how MySQL exchanges data with the rest of the world.

Exporting

Save the results of a query out of MySQL into a plain CSV file on disk.

Importing

Load rows from an existing CSV file into a MySQL table.

Universal Format

CSV files can be opened by spreadsheets, scripts, and other databases.

GUI Alternative

MySQL Workbench provides a point-and-click wizard for the same tasks.

Exporting Data with SELECT INTO OUTFILE

The SELECT ... INTO OUTFILE statement runs a normal SELECT query, but instead of returning the results to your screen, it writes them straight into a CSV file on the MySQL server's disk.

SELECT id, name, email
FROM customers
INTO OUTFILE '/var/lib/mysql-files/customers.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
customers.csv (file contents)
"1","Alex Turner","alex@example.com"
"2","Priya Shah","priya@example.com"
"3","Sam Lee","sam@example.com"

FIELDS TERMINATED BY sets the delimiter between columns (a comma, for a standard CSV). ENCLOSED BY wraps each value in quotes, which helps if any value itself contains a comma. LINES TERMINATED BY marks the end of each row.

File Location Restrictions

For security reasons, MySQL only allows INTO OUTFILE to write to specific directories (typically configured via the secure_file_priv setting), not just anywhere on disk. You will usually need to check this setting, or ask a database administrator, before your first export.

Importing Data with LOAD DATA INFILE

LOAD DATA INFILE is the reverse operation: it reads a CSV file from disk and inserts its rows directly into a table, in bulk. It is dramatically faster than inserting rows one at a time with individual INSERT statements.

Suppose a supplier sends you a CSV of new products to add to your catalog.

new_products.csv (file contents)
"101","Wireless Mouse","19.99"
"102","USB-C Cable","9.50"
"103","Laptop Stand","34.00"
LOAD DATA INFILE '/var/lib/mysql-files/new_products.csv'
INTO TABLE products
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(id, name, price);
SELECT * FROM products WHERE id >= 101;
Result
+-----+-----------------+-------+
| id  | name            | price |
+-----+-----------------+-------+
| 101 | Wireless Mouse  | 19.99 |
| 102 | USB-C Cable     | 9.50  |
| 103 | Laptop Stand    | 34.00 |
+-----+-----------------+-------+

The FIELDS and LINES clauses must describe the file's actual format so MySQL can correctly split it back into columns — they should match whatever was used to create the file in the first place.

Using the Workbench Wizard

Writing INTO OUTFILE and LOAD DATA INFILE by hand works well for automation and scripts, but MySQL Workbench also offers a friendlier, entirely mouse-driven alternative: the Table Data Export/Import Wizard.

To use it in Workbench: right-click any table in the schema list on the left, and choose "Table Data Export Wizard" to save its contents to a CSV or JSON file, or "Table Data Import Wizard" to load a CSV or JSON file into that table. The wizard walks you through selecting the file, matching columns, and confirming the import.

When to Use the Wizard

The Workbench wizard is a great choice when you are doing a one-off import or export interactively and want to visually confirm columns line up correctly, without worrying about file-path permissions or exact SQL syntax. For repeatable, automated tasks, the SQL commands are usually a better fit.

Common Use Cases

Migrating Data

Move data from an old system, a spreadsheet, or another database into MySQL.

Sharing a Dataset

Hand a colleague or client a clean CSV snapshot of specific data.

Seeding a Test Database

Quickly populate a development or test database with realistic sample data.

Reporting

Export query results so they can be analyzed further in a spreadsheet.

Common Mistakes

Avoid These Mistakes
  • Assuming INTO OUTFILE can write anywhere on disk — it is restricted for security reasons.
  • Mismatching FIELDS/LINES options between the file's actual format and the LOAD DATA INFILE statement.
  • Forgetting that INTO OUTFILE will fail if the target file already exists.
  • Importing untrusted CSV data without checking it first, which can introduce bad or malicious data.

Best Practices

  • Always inspect a CSV file's first few rows before importing it, to confirm the column order and delimiter.
  • Prefer LOAD DATA INFILE over many individual INSERT statements when loading large amounts of data — it is far faster.
  • Use the Workbench wizard for quick, one-off, interactive imports and exports.
  • Back up a table before running a large import into it, in case something goes wrong.

Frequently Asked Questions

Can I export to formats other than CSV?

SELECT INTO OUTFILE is CSV/plain-text focused, but MySQL Workbench's wizard also supports JSON and other formats for convenience.

Why did my SELECT INTO OUTFILE command fail with a permission error?

It is likely writing to a directory not allowed by the secure_file_priv setting, or your MySQL user lacks the FILE privilege. Check with your database administrator.

Does LOAD DATA INFILE check for duplicate keys?

Yes, it still respects primary key and unique constraints just like a normal INSERT, and can be told to IGNORE or REPLACE conflicting rows.

Is LOAD DATA INFILE faster than many INSERT statements?

Yes, significantly — it is one of the fastest ways to bulk-load data into MySQL.

Can I import a CSV without server-level file access?

Yes — MySQL Workbench's Table Data Import Wizard reads the file from your own local machine, not the server, so it works even without server file-system access.

Key Takeaways

  • SELECT ... INTO OUTFILE exports query results to a CSV file on the server.
  • LOAD DATA INFILE bulk-imports rows from a CSV file into a table.
  • MySQL Workbench's Table Data Export/Import Wizard offers a GUI-based alternative.
  • Common use cases include migrating data, sharing datasets, and seeding test databases.
  • File-path permissions (secure_file_priv) restrict where server-side import/export commands can read and write.

Summary

Moving data in and out of MySQL is a routine part of real-world database work, whether through SQL commands or Workbench's visual wizard.

In this lesson, you learned how to export data to CSV with SELECT INTO OUTFILE, import it back with LOAD DATA INFILE, and use MySQL Workbench's wizard as a friendlier alternative. Next, you will learn how to protect your entire database with proper backups.

Lesson 68 Completed
  • You can export query results to a CSV file.
  • You can bulk-import data from a CSV file into a table.
  • You know how to use the MySQL Workbench import/export wizard.
  • You are ready to learn about backing up and restoring databases.
Next Lesson →

Backup & Restore