遇到的错误:Mysql 报错Duplicate entry '值' for key '字段名'的解决
Posted 慢漫长路
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了遇到的错误:Mysql 报错Duplicate entry '值' for key '字段名'的解决相关的知识,希望对你有一定的参考价值。
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry ‘admin‘ for key ‘user_name_unique‘ ### The error may involve com.mmall.dao.UserMapper.insert-Inline ### The error occurred while setting parameters ### SQL: insert into mmall_user (id, username, password, email, phone, question, answer, role, create_time, update_time) values (?, ?, ?, ?, ?, ?, ?, ?, now(), now()) ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry ‘admin‘ for key ‘user_name_unique‘ ; SQL []; Duplicate entry ‘admin‘ for key ‘user_name_unique‘; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry ‘admin‘ for key ‘user_name_unique‘] with root cause com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry ‘admin‘ for key ‘user_name_unique‘ at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) at com.mysql.jdbc.Util.getInstance(Util.java:408) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3978) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3914) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
二、问题解释:
Duplicate entry...for key...的错误原因是主键的唯一值重复,在对数据库进行修改、插入操作时,一但主键的唯一值重复就会报此错误,有时在表中存在多个相同主键时,对表操作报此错误,
(ps:我的表中数据主键有两个相同的主键,在使用把一张表数据导入到另一张表的数据时使用主键字段结果报错。)
三、解决方法:
第一种,如果主键不是必需要的字段,就不用主键
第二种,先把表中重复去除,在进行操作
以上是关于遇到的错误:Mysql 报错Duplicate entry '值' for key '字段名'的解决的主要内容,如果未能解决你的问题,请参考以下文章
mysql插入数据报错IntegrityError: (1062, "Duplicate entry 'xx' for key 'xxxxx'")
用java连接mysql数据库时总是报错:Duplicate entry '1' for key 1,是啥原因?
mysql在更改root远程连接时候报错:Duplicate entry ‘%-root‘ for key ‘PRIMARY‘
ios-ld: 1 duplicate symbol for architecture arm64报错处理