12
votes

I was trying to load a data file into mysql table using "LOAD DATA LOCAL INFILE 'filename' INTO TABLE 'tablename'".

The problem is the source data file contains data of every fields but the primary key is missing ('id' column). I have to add a unique id to each line of source data file otherwise the import will not go through.

Is there an option to ignore primary key in source file or auto increase it during the import?

It's already been set to a auto-increment primary key.

mysql> desc tablename;
+--------------------+--------------+------+-----+---------+----------------+
| Field              | Type         | Null | Key | Default | Extra          |
+--------------------+--------------+------+-----+---------+----------------+
| id                 | int(11)      | NO   | PRI | NULL    | auto_increment | 
...
1

1 Answers

14
votes

Why not load into a table that has an auto-increment primary key?

create table MyTable (
    id integer auto_increment primary key,
    ...
)

You can specify the columns that will be imported after the table name:

LOAD DATA INFILE 'filename' INTO TABLE tablename (col1,col2,...);

If you don't specify the id column, MySQL won't read it from the file.