### 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:我的表中数据主键有两个相同的主键,在使用把一张表数据导入到另一张表的数据时使用主键字段结果报错。)
三、解决方法:
第一种,如果主键不是必需要的字段,就不用主键
第二种,先把表中重复去除,在进行操作