How to import CSV into MySQL without broken rows or wrong column types

How to import CSV into MySQL without broken rows or wrong column types

A spreadsheet export needs to become a MySQL table. Clean the file before you load it, then let the column types come from the data.

What goes wrong when a raw CSV meets a table

Spreadsheet exports carry problems you don't see in Excel: spaces around values, empty rows at the end, duplicate rows, semicolons where MySQL expects commas, and rows with fewer cells than the header. A value with an apostrophe, such as O'Neil, breaks a hand-written INSERT unless the quote is escaped.

Column types cause the rest. Load a price column as text and every sum needs a cast. Declare an ID column as INTEGER and a value over 2,147,483,647 needs BIGINT instead. Fixing these after the import means altering a table that may already hold bad data.

Clean the file, then generate the SQL

Paste the CSV or upload the file in the CSV Cleaner. It parses the data as CSV, so quoted fields with commas or line breaks inside stay intact. Turn on Trim cells, Remove empty rows, and Remove duplicate rows, and set the output delimiter to comma. Fill ragged rows pads short rows so every row has the header's width. Duplicate detection compares whole rows, not one chosen column.

Then open the CSV to SQL Converter, pick MySQL, and type the table name. It infers each column's type from all of its values. Whole numbers become INTEGER, or BIGINT above 2,147,483,647. Decimals become DECIMAL, true and false become BOOLEAN, YYYY-MM-DD dates become DATE, and anything else becomes VARCHAR sized to the longest value. It writes a CREATE TABLE and INSERT statements with backticks around names, and it doubles single quotes inside values, so O'Neil goes in as 'O''Neil'. It takes files up to 5 MB and runs in your browser.

For hundreds of thousands of rows, MySQL's bulk loader, LOAD DATA INFILE, is much faster and reads the CSV directly. Clean the file first either way.

Import a CSV file into MySQL

  1. Upload the CSV to the CSV Cleaner and turn on Trim cells, Remove empty rows, and Remove duplicate rows.
  2. Set the output delimiter to comma and download the cleaned file.
  3. Load it into the CSV to SQL Converter, choose MySQL, and enter the table name.
  4. Review the inferred column types and correct any that are wrong in the CREATE TABLE.
  5. Download the .sql file and run it against your database, for example with mysql -u user -p dbname < import.sql.

What the tools leave for you to check

  • Column types are guessed from the values in the file. Review them before you create a production table.
  • The converter takes files up to 5 MB. Larger files belong in LOAD DATA INFILE.
  • The cleaner fixes CSV structure. It doesn't sort rows or validate business rules in the data.
  • If a file isn't valid CSV, parsing can fail before cleanup starts.

Frequently asked questions

How do I convert a CSV file to SQL for MySQL?

Upload the CSV to the CSV to SQL Converter, choose MySQL and a table name, and copy the CREATE TABLE and INSERT statements.

How are apostrophes in CSV data handled?

A single quote inside a value is doubled, so O'Neil becomes 'O''Neil', the standard SQL escape.

What is the fastest way to import a large CSV into MySQL?

Use LOAD DATA INFILE. It reads the CSV directly and is much faster than INSERT statements for hundreds of thousands of rows.

Is my CSV uploaded when I convert it to SQL?

No. The CSV to SQL Converter runs in your browser, and the SQL is text you copy or download.

Tools used in this guide

  • CSV CleanerText Tools

    Cleans CSV data by trimming cells, removing empty rows, deduping rows, and fixing delimiter issues.

More from the blog

All guides