We have a CSV file with thousands of records in it. I want to import these rows into a MySQL table via phpmyadmin. here is the command used:
load data loca
Why is MySQL LOAD DATA INFILE command only loading one row?
What is happening is you load the first column fine, then when you load the second and it fails because is violates a unique index constraint.
MySQL LOAD DATA LOCAL INFILE will enforce your unique indexes on columns and then skip any offending duplicate rows without doing anything about it, not even logging a warning!
How to reproduce this phenemenon:
Create a table with an int and varchar column:
mysql> create table foo(id INT, mytext VARCHAR(255));
Query OK, 0 rows affected (0.02 sec)
Add a unique constraint on the varchar column:
mysql> alter table foo add constraint my_epic_constraint unique(mytext);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
Create your input text file /tmp/foo.txt, delimited by tabs, which violates the unique constraint:
1 fred
2 fred
Try importing it:
mysql> load data local infile '/tmp/foo.txt' into table foo fields
terminated by '\t' lines terminated by '\n' (@col1,@col2)
set id=@col1, mytext=@col2;
Query OK, 1 row affected (0.01 sec)
Records: 2 Deleted: 0 Skipped: 1 Warnings: 0
BAM! There's your problem: why is it only importing only one row? Because you have a unique key constraint on it which the lines in the data file violated.
Solutions:
Remove the unique key constraint on your table and try again.
Go into the text file and remove the duplicate rows that violate the unique constraint.