Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to LOAD DATA INFILE in mysql with first col being Auto Increment?

Tags:

sql

mysql

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 
like image 414
DucDigital Avatar asked May 16 '11 12:05

DucDigital


People also ask

How do I make a column auto increment in MySQL?

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.

Can auto increment be a primary key?

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.

Does MySQL auto increment primary key?

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.

Can we have 2 auto increment in MySQL?

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.


1 Answers

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; 
like image 162
Ike Walker Avatar answered Oct 11 '22 23:10

Ike Walker