Complete Guide to Importing Excel Data into MySQL Using LOAD DATA INFILE

Nov 13, 2025 · Programming · 13 views · 7.8

Keywords: MySQL Import | Excel to CSV | LOAD DATA INFILE | Data Migration | Database Management

Abstract: This article provides a comprehensive guide on using MySQL's LOAD DATA INFILE command to import Excel files into databases. The process involves converting Excel files to CSV format, creating corresponding MySQL table structures, and executing LOAD DATA INFILE statements for data import. The guide includes detailed SQL syntax examples, common issue resolutions, and best practice recommendations to help users efficiently complete data migration tasks without relying on additional software.

Data Import Method Overview

There are multiple approaches to import Excel data into MySQL databases, with the LOAD DATA INFILE command being an efficient method that doesn't require additional software. This approach is particularly suitable for batch data processing, leveraging MySQL's built-in capabilities for data migration tasks.

Excel to CSV Format Conversion

Since MySQL's LOAD DATA INFILE command primarily supports text format files, the first step involves converting Excel files to CSV format. In Excel, this can be done through the "File → Save As" function, selecting CSV format. During conversion, pay attention to the choice of data delimiter, typically using commas or tabs as field separators.

MySQL Table Structure Design

Before importing data, create the corresponding database table structure. Design MySQL table fields based on Excel data column definitions. For example, for a data table containing country, amount, and quantity information, create the following structure:

CREATE TABLE sales_data (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  country VARCHAR(255),
  amount INT,
  quantity DECIMAL(10,2)
);

LOAD DATA INFILE Command Detailed Explanation

LOAD DATA INFILE is a specialized command provided by MySQL for importing data from files. The basic syntax structure is as follows:

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

Field and Line Delimiter Configuration

In the LOAD DATA INFILE command, correctly configure field and line delimiters:

Special Handling for Tab-Delimited Data

For data files using tab delimiters, adjust the field separator configuration:

LOAD DATA INFILE '/path/to/tab_delimited_data.csv'
INTO TABLE sales_data
FIELDS TERMINATED BY '\t'
ENCLOSED BY ''
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

Data Validation and Error Handling

During data import, various issues may arise requiring appropriate validation and handling:

Post-Import Data Validation

After data import completion, perform integrity validation:

SELECT COUNT(*) as total_records FROM sales_data;
SELECT * FROM sales_data LIMIT 10;

Verify all data has been correctly imported by querying total record count and sampling data.

Performance Optimization Recommendations

For large-scale data imports, implement the following optimization measures:

Alternative Solution Comparison

Besides the LOAD DATA INFILE method, other import options are available:

Best Practices Summary

Based on practical project experience, summarize the following best practices:

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.