Currently, We have a table similar to this:
--------------------- ID | AField | BField| ---------------------
The ID is Auto Increment
How do I create a CSV that can let the database auto populate the ID field with auto increment number?
We've tried the following CSV but it doesn't work:
afieldvalue, bfieldvalue (With Column definition but still doesn't work) 0,afieldvalue,bfieldvalue NULL,afieldvalue,bfieldvalue
In MySQL, the syntax to change the starting value for an AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = start_value; table_name.
Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.
The Auto Increment feature allows you to set the MySQL Auto Increment Primary Key field. This automatically generates a sequence of unique numbers whenever a new row of data is inserted into the table.
MySQL server already provides two auto increment variables: auto_increment_increment and auto_increment_offset, which can be used to generate different auto increment values on each member.
The best thing to do is just include the 2 non-auto-increment columns in the CSV, and then explicitly set the ID column to NULL in the load data infile statement.
Something like this:
LOAD DATA INFILE '/tmp/data.csv' INTO TABLE your_table FIELDS TERMINATED BY ',' (AField, BField) SET ID = NULL;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With