Import CSV to MySQL Table
Here are the general steps involved:
Additional notes:
- If your CSV file has a different delimiter or enclosure character, adjust the
FIELDS TERMINATED BY
andENCLOSED BY
options accordingly. - For larger CSV files, consider using the
IGNORE
option to skip rows with errors instead of stopping the import. - If you need to handle specific error handling scenarios, refer to the MySQL documentation for more details.
Importing CSV into MySQL: Example Codes
Using LOAD DATA INFILE
Command
This is the most efficient method for importing large CSV files directly into MySQL.
Example:
CREATE TABLE my_table (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
age INT
);
LOAD DATA INFILE 'path/to/your/file.csv'
INTO TABLE my_table
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
- Explanation:
- Creates a table named
my_table
with columnsid
,name
, andage
. - Loads data from the specified CSV file into the table.
FIELDS TERMINATED BY ','
: Indicates commas separate fields.LINES TERMINATED BY '\n'
: Defines newline characters as line terminators.
- Creates a table named
Using MySQL Workbench
MySQL Workbench provides a graphical interface for importing data.
Steps:
- Connect to your database.
- Right-click on the desired table.
- Select "Table Data Import Wizard".
- Follow the wizard's instructions:
- Choose your CSV file.
- Configure column mappings.
- Start the import.
Using a Programming Language (e.g., Python with mysql-connector-python
)
import mysql.connector
mydb = mysql.connector.connect(
host="your_host",
user="your_user",
password="your_password",
database="your_database"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE my_table (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100), age INT)")
with open('path/to/your/file.csv', 'r') as file:
reader = csv.reader(file)
next(reader) # Skip header row if present
for row in reader:
sql = "INSERT INTO my_table (name, age) VALUES (%s, %s)"
val = (row[0], row[1])
mycursor.execute(sql, val)
mydb.commit()
- Explanation:
- Creates the table.
- Reads the CSV file line by line.
- Inserts data into the table using SQL.
Alternative Methods for Importing CSV into MySQL
While the LOAD DATA INFILE
command is generally the most efficient method, there are other approaches that can be considered depending on specific requirements or preferences:
Using a Programming Language and MySQL Connector:
This method involves connecting to the MySQL database using a programming language (e.g., Python, Java, PHP) and utilizing a database connector library. You can read the CSV file line by line and execute SQL INSERT
statements to populate the table.
Advantages:
- Flexibility to customize data processing and error handling.
- Suitable for smaller datasets or complex import scenarios.
- Can be slower for large datasets compared to
LOAD DATA INFILE
. - Requires additional programming knowledge.
Using a GUI Tool or Database Management System (DBMS):
Many GUI tools and DBMSs (like MySQL Workbench, phpMyAdmin) provide built-in features for importing data from CSV files. These tools often offer a user-friendly interface for configuring import options and mapping columns.
- Easy to use for users without extensive SQL knowledge.
- Can be convenient for quick imports or small datasets.
- May have limitations for large datasets or complex import scenarios.
- Dependency on the specific tool or DBMS.
Using a Shell Script or Command-Line Tools:
For automated imports or scripting purposes, you can create shell scripts (e.g., Bash) to combine the LOAD DATA INFILE
command with other shell utilities (like sed
, awk
) to preprocess or filter the CSV data before import.
- Automation and scripting capabilities.
- Suitable for integration into larger automation workflows.
- Requires shell scripting knowledge.
- May be less user-friendly for manual imports.
Using a Third-Party ETL Tool:
Extract, Transform, and Load (ETL) tools are specialized software designed for data integration and transformation. They offer advanced features for handling complex data sources, transformations, and loading into databases.
- Powerful features for data manipulation and quality control.
- Suitable for large-scale data migrations or complex ETL processes.
- Can be expensive and require a learning curve.
- May be overkill for simple CSV imports.
Choosing the Best Method: The optimal method depends on factors such as:
- Dataset size
- Data complexity
- Required automation level
- Existing tools and skills
- Performance requirements
mysql csv load-data-infile