300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 【已解决】java.sql.SQLIntegrityConstraintViolationException: Duplicate entry ‘xxx‘ for key ‘xxx‘

【已解决】java.sql.SQLIntegrityConstraintViolationException: Duplicate entry ‘xxx‘ for key ‘xxx‘

时间:2022-05-03 15:57:50

相关推荐

【已解决】java.sql.SQLIntegrityConstraintViolationException: Duplicate entry ‘xxx‘ for key ‘xxx‘

问题重现

开发我的个人博客项目时遇到了这个问题,Mybatis输出SQL报错信息如下:

### Error updating database. Cause: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry 'qwe1111' for key 'blog_post_title_adf2f203_uniq'### The error may exist in com/eastnotes/mapper/ArticleMapper.java (best guess)### The error may involve com.eastnotes.mapper.ArticleMapper.addBlog-Inline### The error occurred while setting parameters### SQL: INSERT INTO blog_post(title, body, created_time, excerpt, views, category_id, status, stick) VALUES(?, ?, ?, ?, ?, ?, ?, ?)### Cause: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry 'qwe1111' for key 'blog_post_title_adf2f203_uniq'; Duplicate entry 'qwe1111' for key 'blog_post_title_adf2f203_uniq'; nested exception is java.sql.SQLIntegrityConstraintViolationException: Duplicate entry 'qwe1111' for key 'blog_post_title_adf2f203_uniq']

分析原因

从报错可以看出来,是title这个字段的值重复了,也就是说当前插入的数据,跟数据库中某个字段的数据冲突了。所以报错了。

进一步分析,一定是MySQL的某个字段添加了唯一键约束,才会出现这样的问题,通过查看表结构,发现确实如此,title字段添加了唯一键约束:

+---------------+------------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra|+---------------+------------------+------+-----+---------+----------------+| id | int(11)| NO | PRI | NULL | auto_increment || title | varchar(100)| NO | UNI | NULL ||| body| longtext | NO || NULL ||| created_time | datetime(6)| NO || NULL ||| modified_time | datetime(6)| YES || NULL ||| excerpt | varchar(200)| NO || NULL ||| views | int(10) unsigned | NO || NULL ||| words | int(10) unsigned | YES || NULL ||| author_id| int(11)| YES | MUL | NULL ||| category_id | int(11)| NO | MUL | NULL ||| status | varchar(1) | NO || NULL ||| stick | varchar(1) | NO || NULL ||+---------------+------------------+------+-----+---------+----------------+

解决办法

1.如果某个字段设定的就是不能插入重复值,那么不用改表结构,在插入时避免数据相同即可。

2.如果允许重复字段,那么解决起来更简单了,只需要在MySQL中把该字段的唯一键索引去掉即可,下面是删除唯一键属性的SQL语句

ALTER TABLE <表名> DROP INDEX <唯一约束名>;

唯一键约束名需要使用下面的命令查看:

show create table <表名>;

结果如下:

blog_post | CREATE TABLE `blog_post` (`id` int(11) NOT NULL AUTO_INCREMENT,`title` varchar(100) NOT NULL,`body` longtext NOT NULL,`created_time` datetime(6) NOT NULL,`modified_time` datetime(6) DEFAULT NULL,`excerpt` varchar(200) NOT NULL,`views` int(10) unsigned NOT NULL,`words` int(10) unsigned DEFAULT NULL,`author_id` int(11) DEFAULT NULL,`category_id` int(11) NOT NULL,`status` varchar(1) NOT NULL,`stick` varchar(1) NOT NULL,PRIMARY KEY (`id`),UNIQUE KEY `blog_post_title_adf2f203_uniq` (`title`),KEY `blog_post_author_id_dd7a8485_fk_auth_user_id` (`author_id`),KEY `blog_post_category_id_c326dbf8_fk_blog_category_id` (`category_id`)) ENGINE=InnoDB AUTO_INCREMENT=184 DEFAULT CHARSET=utf8

其中blog_post_title_adf2f203_uniq就是唯一键约束名。最后通过下面的命令删除唯一键约束即可:

ALTER TABLE blog_post DROP INDEX blog_post_title_adf2f203_uniq;

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。