When working with an existing table where the ID column is an auto-increment field, importing data through LOAD DATA INFILE can pose a challenge. This article provides an efficient solution to handle such scenarios.
For clarity, consider the following table structure:
--------------------- ID | AField | BField| ---------------------
where ID is an auto-increment field.
To create a CSV file that enables the database to auto-populate the ID field with auto-increment numbers, the ideal approach is to include only the non-auto-increment columns in the CSV. Subsequently, explicitly set the ID column to NULL in the LOAD DATA INFILE statement.
This approach ensures that the database generates the auto-increment numbers for the ID field during the import process. For example, consider the following CSV:
afieldvalue, bfieldvalue
The LOAD DATA INFILE statement would look like this:
LOAD DATA INFILE '/tmp/data.csv' INTO TABLE your_table FIELDS TERMINATED BY ',' (AField, BField) SET ID = NULL;
By following this approach, you can seamlessly populate the auto-increment ID field when importing data through LOAD DATA INFILE in MySQL.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3