Skip to content

Creating a table in MySQL from a CSV file simplifies data migration and batch processing tasks. The post below will help you efficiently manage and integrate large datasets, significantly enhancing your database management capabilities.

How to prepare your CSV file?

Ensure your CSV file is correctly formatted for the import:

  • Include column headers in the first row.
  • Delimit fields with a comma or another specified delimiter.
  • Enclose text fields in quotes if they contain delimiters or special characters.

How to create a MySQL table?

Construct a corresponding table in your MySQL database based on your CSV’s structure. Here’s how you create a basic table:

CREATE TABLE example_table (
    id INT AUTO_INCREMENT,
    name VARCHAR(255),
    age INT,
    PRIMARY KEY (id)
);

Modify this structure to align with your CSV file’s layout.

How to import the CSV file into MySQL?

After setting up your table, proceed to import the CSV file. Here’s how to execute the import using the MySQL command line:

  1. Log into your MySQL server with the command line tool:

    mysql -u username -p
    
  2. Choose the database for the CSV import:

    USE your_database;
    
  3. Execute the LOAD DATA command to transfer your CSV data into the MySQL table:

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

Adjust the file path and delimiters according to your CSV file specifications. The IGNORE 1 ROWS option skips the headers.

Verifying the data

Check the data import for accuracy:

SELECT * FROM example_table;

This SQL command allows you to view all records in your table, ensuring the data import was successful.

Troubleshooting

If you encounter issues, verify the CSV file path, ensure MySQL has the necessary file permissions, and check that your table’s structure matches the CSV layout.

Following these guidelines enables you to efficiently create a MySQL table from a CSV file, streamlining your data handling processes.

Written by

Robert Cooper avatar

Robert Cooper

Senior Engineer at Basedash

Robert Cooper is a senior engineer at Basedash who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.

View full author profile →

Looking for an AI-native BI tool?

Basedash lets you build charts, dashboards, and reports in seconds using all your data.