当前位置:网站首页>Duplicate entry ‘XXX‘ for key ‘XXX.PRIMARY‘解决方案。
Duplicate entry ‘XXX‘ for key ‘XXX.PRIMARY‘解决方案。
2022-07-31 04:47:00 【geejkse_seff】
mysql insert时报错:Duplicate entry ‘XXX’ for key ‘XXX.PRIMARY’
今天在插入数据时,出现一个错误
com.jfinal.plugin.activerecord.ActiveRecordException:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Duplicate entry 'xxxxxx' for key 'xxxx.PRIMARY'
网上查看说是主键冲突了,但是查看数据库并没有,把所有数据清空,或者把表删了之后,在重新建也还是报同样的错。
…
…
…
最后的结果方案:
- 方案一:设置主键自增,适合主键为int类型(我的主键是varchar)
- 方案二:把insert into 表名() values();修改为:insert ignore into 表名() values(); (问题解决)
MySQL 提供了Ignore 用来避免数据的重复插入.
IGNORE :
若有导致unique key 冲突的记录,则该条记录不会被插入到数据库中.
MySQL INSERT IGNORE 详解请移步:MySQL INSERT IGNORE
原因:这是由于表中对应字段设置了唯一索引,在我们进行逻辑删除时,并没有真正的删除,只是标记为删除状态而已,
当我们再进行新增的时候,由于有唯一索引的限制,导致我们不能进行新增成功,所以报错.
解决方法1:
如果允许的话,把数据库中该字段的唯一索引限制去掉,这样后端可以进行逻辑删除,新增时即使与标记为删除的记录的字段值相等,由于没有唯一索引的限制,可以正常新增
解决方法2:
不需要去掉数据库中的唯一索引限制,但是当后端开发进行删除操作的时候,执行的是物理删除的sql语句(真正的删除),而不是逻辑删除,同时后端用逻辑判断,去查询数据库中是否有相同的编号存在,如果存在,则不允许新增,如果不存在,则可以新增
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
- 【云原生】DevOps(五):集成Harbor
- Unity URP渲染管线摄像机核心机制剖析
- Unity shader forge和自带的shader graph,有哪些优缺点?
- 递归实现汉诺塔问题
- Industry-university-research application to build an open source talent ecosystem | 2022 Open Atom Global Open Source Summit Education Sub-Forum was successfully held
- 微信小程序使用云函数更新和添加云数据库嵌套数组元素
- Learning DAVID Database (1)
- MySQL数据库增删改查(基础操作命令详解)
- 强化学习:从入门到入坑再到拉屎
猜你喜欢

ERROR 1819 (HY000) Your password does not satisfy the current policy requirements

Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined

【C语言】操作符详解

pom文件成橘红色未加载的解决方案

【云原生】DevOps(五):集成Harbor

Open Source Database Innovation in the Digital Economy Era | 2022 Open Atom Global Open Source Summit Database Sub-Forum Successfully Held

ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your

打造基于ILRuntime热更新的组件化开发

ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)Solution

From scratch, a mirror to the end, a pure system builds a grasscutter (Grasscutter)
随机推荐
Smartcom Programming Level 4 - Magic Academy Lesson 6
volatile内存语义以及实现 -volatile写和读对普通变量的影响
简易网络文件拷贝的C实现
The MySQL database installed configuration nanny level tutorial for 8.0.29 (for example) have hands
MySQL database installation (detailed)
Heavyweight | The Open Atomic School Source Line activity was officially launched
强化学习:从入门到入坑再到拉屎
Reinforcement learning: from entry to pit to shit
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
论治理与创新 | 2022开放原子全球开源峰会OpenAnolis分论坛圆满召开
Puzzle Game Level Design: Reverse Method--Explaining Puzzle Game Level Design
从零开始,一镜到底,纯净系统搭建除草机(Grasscutter)
The Vue project connects to the MySQL database through node and implements addition, deletion, modification and query operations
MySQL模糊查询可以使用INSTR替代LIKE
ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)
【C语言进阶】文件操作(一)
WPF WPF 】 【 the depth resolution of the template
Understanding of the presence of a large number of close_wait states
【wpf】wpf中的那些模板之深度解析
两个地址池r2负责管地址池r1负责管dhcp中继