### 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:我的表中數據主鍵有兩個相同的主鍵,在使用把一張表數據導入到另一張表的數據時使用主鍵字段結果報錯。)
三、解決方法:
第一種,如果主鍵不是必需要的字段,就不用主鍵
第二種,先把表中重復去除,在進行操作