Home > Database > Mysql Tutorial > How to Import a CSV File into MySQL Using LOAD DATA, Handling Headers and Empty Data?

How to Import a CSV File into MySQL Using LOAD DATA, Handling Headers and Empty Data?

Linda Hamilton
Release: 2024-12-27 02:35:12
Original
944 people have browsed it

How to Import a CSV File into MySQL Using LOAD DATA, Handling Headers and Empty Data?

CSV File Import into MySQL Table with Column Header Naming

Importing a CSV file into a MySQL table can be achieved with a SQL LOAD DATA command. To utilize this method, ensure the CSV file's first row comprises the column headers.

The syntax for importing the CSV file is as follows:

LOAD DATA LOCAL INFILE 'uniq.csv'
INTO TABLE tblUniq
FIELDS TERMINATED BY ','
    ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(uniqName, uniqCity, uniqComments)
Copy after login

By including the 'IGNORE 1 LINES' option, the SQL command skips the first line of the CSV file, effectively assigning the column headers from the second line onwards.

Additional Considerations

Prior to importing the CSV file, ensure to export it from an Excel file and remove any headers from the generated CSV. Additionally, eliminate any empty data present at the end of the CSV file.

Note:

The SQL LOAD DATA command can also be used to ignore specific lines within the CSV file, providing granular control over the import process.

The above is the detailed content of How to Import a CSV File into MySQL Using LOAD DATA, Handling Headers and Empty Data?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template