MySQL中unique列上插入重复值解决办法
2015-02-10 15:32
288 查看
当unique列在一个UNIQUE键上插入包含重复值的记录时,我们可以控制MySQL如何处理这种情况:使用IGNORE关键字或者ONDUPLICATE KEY UPDATE子句跳过INSERT、中断操作或者更新旧记录为新值。
1.新建一张测试表tbtest,包含三个字段:
create table tbtest(
id tinyint(4) not nullauto_increment,
title varchar(10) null,
content varchar(30)null,
uniquekey(id));
2.为新增的表tbtest中增加数据并确认插入的数据:
insert intotbtest(title,content)
values('Monday','Today is Monday');
insert intotbtest(title,content)
values('Tuesday','Today is Tuesday');
insert intotbtest(title,content)
values('Wednesday','Today is Wednesday!');
insert intotbtest(title,content)
values('Thursday','Today is Thursday');
select * from tbtest;
3.在unique列上插入一条违背该约束的数据,MySQL会中断操作并提示出错信息:
insert intotbtest(id,title,content)
values(3,'Sunday','Aha,Todayis Sunday!');
4.在insert语句中增加关键字ignore,当解析发现该语句违背了unique约束,MySQL不会尝试去执行这条语句,自然也不会返回出错信息。这样当有很多的insert语句需要顺序执行的时候,关键字ignore的加入就会使操作变得十分的简单,他可以保证无论哪一个insert语句中碰到这种重复键值时都会跳过它,执行下一个的insert,这样就不会出现放弃或中断insert操作的情况。
insert ignore intotbtest(id,title,content)
values(3,'Sunday','Aha,Todayis Sunday!');
5.利用了ignore可以忽略这些重复键值的操作,如果当很多insert语句顺序执行的目的是当发现存在重复键值就进行相应的更新旧记录操作的时候,我可以利用ONDUPLICATE KEY UPDATE从句来实现,从而制定新值:
insert intotbtest(id,title,content)
values(3,'Sunday','Aha,Todayis Sunday!')
on duplicate key updatetitle='Sunday',content='Aha,Today is Sunday!';
6.查看结果。原来的Wednesday已经被update为Sunday了!
1.新建一张测试表tbtest,包含三个字段:
create table tbtest(
id tinyint(4) not nullauto_increment,
title varchar(10) null,
content varchar(30)null,
uniquekey(id));
2.为新增的表tbtest中增加数据并确认插入的数据:
insert intotbtest(title,content)
values('Monday','Today is Monday');
insert intotbtest(title,content)
values('Tuesday','Today is Tuesday');
insert intotbtest(title,content)
values('Wednesday','Today is Wednesday!');
insert intotbtest(title,content)
values('Thursday','Today is Thursday');
select * from tbtest;
3.在unique列上插入一条违背该约束的数据,MySQL会中断操作并提示出错信息:
insert intotbtest(id,title,content)
values(3,'Sunday','Aha,Todayis Sunday!');
4.在insert语句中增加关键字ignore,当解析发现该语句违背了unique约束,MySQL不会尝试去执行这条语句,自然也不会返回出错信息。这样当有很多的insert语句需要顺序执行的时候,关键字ignore的加入就会使操作变得十分的简单,他可以保证无论哪一个insert语句中碰到这种重复键值时都会跳过它,执行下一个的insert,这样就不会出现放弃或中断insert操作的情况。
insert ignore intotbtest(id,title,content)
values(3,'Sunday','Aha,Todayis Sunday!');
5.利用了ignore可以忽略这些重复键值的操作,如果当很多insert语句顺序执行的目的是当发现存在重复键值就进行相应的更新旧记录操作的时候,我可以利用ONDUPLICATE KEY UPDATE从句来实现,从而制定新值:
insert intotbtest(id,title,content)
values(3,'Sunday','Aha,Todayis Sunday!')
on duplicate key updatetitle='Sunday',content='Aha,Today is Sunday!';
6.查看结果。原来的Wednesday已经被update为Sunday了!
相关文章推荐
- 【转】MySQL中unique列上插入重复值的解决方法
- Mysql中 unique列插入重复值该怎么解决呢
- mysql中的唯一键索引,插入重复数据直接报错的解决办法
- 使用distinct在mysql中查询多条不重复记录值的解决办法
- 使用distinct在mysql中查询多条不重复记录值的解决办法
- 使用distinct在mysql中查询多条不重复记录值的解决办法
- 浅谈unique列上插入重复值的MySQL解决方案
- PHPCMS V9用array_unique函数解决采集网址重复的办法
- mysql中文乱码,无法插入中文的解决办法总结
- 使用distinct在mysql中查询多条不重复记录值的解决办法
- mysql中查询多条不重复记录值的解决办法
- 使用distinct在mysql中查询多条不重复记录值的解决办法
- 使用distinct在mysql中查询多条不重复记录值的解决办法
- 【MySql】MySql数据库中插入中文字符,显示乱码解决办法
- 自增列无法插入重复主键解决办法
- 向MySQL插入数据乱码解决办法
- 使用distinct在mysql中查询多条不重复记录值的解决办法
- 使用distinct在mysql中查询多条不重复记录值的解决办法
- php mysql不能插入中文的解决办法