2

So, basically I have this code below to change the date format from CSV, so it can be recognized by MySQL when I import it.

LOAD DATA INFILE 'file.csv'
INTO TABLE customer FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' 
(
    ID, name, @var1, age
)
SET date = STR_TO_DATE(@var1, '%d/%m/%Y')

Now, consider the ID is the PRIMARY key. I get an update on my file.csv and I want to re-load it to MySQL by running my code again. Instead only update the new record, it give me this error message:

#1062 - Duplicate entry '05' for key 'PRIMARY'

Is there any way to ignore the same ID and only add the new record with different ID?

Reza Satnaz
  • 101
  • 2
  • 15

1 Answers1

2

MySQL does not allow duplication of PKs because every Primary key is a Unique Key

What you can do, I suggest, is to AUTO_INCREMENT the ID. If you really want to insert the records rather than update it.

Also, make sure that when you insert the .csv file, it does not contain any ID. So, you'll want it to auto-populate along the table.

Hibari
  • 131
  • 16