How to Create a MySQL Table from a CSV File?
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
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.
Ensure your CSV file is correctly formatted for the import:
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.
After setting up your table, proceed to import the CSV file. Here’s how to execute the import using the MySQL command line:
Log into your MySQL server with the command line tool:
mysql -u username -p
Choose the database for the CSV import:
USE your_database;
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.
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.
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
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.
Basedash lets you build charts, dashboards, and reports in seconds using all your data.