Error 1062. Duplicate entry in mysql

白昼怎懂夜的黑 提交于 2019-12-12 16:24:24

问题


I have a MySQL table whose schema in which column 1 is primary key. I have a tsv file which I need to insert in this table. Now, the tsv has repetition of primary key hence when I try to insert it in MySQL table it gives an error

ERROR 1062 (23000): Duplicate entry '107664521128181760' for key
'PRIMARY'

Is there any way by which if the primary key value already exists, then it should ignore and move further for next insertion.


回答1:


You are probably looking for INSERT IGNORE INTO command.

You can try like this:

INSERT IGNORE INTO yourtablename(col1,col2...)
values(val1,val2,...)



回答2:


It depends on how you are importing the data.

If you are using LOAD DATA INFILE command then use IGNORE in command as:

LOAD DATA INFILE 'member.tsv'
IGNORE INTO TABLE tbl_member
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
(name, age);

And if you are using sql having INSERT command then use INSERT IGNORE in INSERT command as:

INSERT IGNORE INTO yourtablename(col1,col2...)
values(val1,val2,...)



回答3:


You are probably looking for REPLACE query.

REPLACE INTO Users (Phone, Name, Email) VALUES ( Phone, Name, Email);


来源:https://stackoverflow.com/questions/29545733/error-1062-duplicate-entry-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!