MySql三种避免重复插入数据的方法
前言
MySql在存在主键冲突或唯一键冲突的情况下,根据插入方式,一般有以下三种插入方式避免错误。
- insertignore。
- replaceinto
- insertonduplicatekeyupdate
insertignore
insertignore会忽视数据库中已经存在的数据,根据主键或者唯一索引判断,如果数据库没有数据,就会插入新的数据,如果有数据的话就跳过这条数据
小case
表结构
root:test>showcreatetablet3G ***************************1.row*************************** Table:t3 CreateTable:CREATETABLE`t3`( `id`int(11)NOTNULLAUTO_INCREMENT, `c1`int(11)DEFAULTNULL, `c2`varchar(20)DEFAULTNULL, `c3`int(11)DEFAULTNULL, PRIMARYKEY(`id`), UNIQUEKEY`uidx_c1`(`c1`) )ENGINE=InnoDBAUTO_INCREMENT=18DEFAULTCHARSET=utf8 1rowinset(0.00sec) root:test>select*fromt3; +----+------+------+------+ |id|c1|c2|c3| +----+------+------+------+ |1|1|a|1| |2|2|a|1| |8|NULL|NULL|1| |14|4|bb|NULL| |17|5|cc|4| +----+------+------+------+ 5rowsinset(0.00sec)
插入冲突数据
root:test>insertignoreintot3(c1,c2,c3)values(5,'cc',4),(6,'dd',5);QueryOK,1rowaffected,1warning(0.01sec) Records:2Duplicates:1Warnings:1
查看结果
root:test>showwarnings; +---------+------+---------------------------------------+ |Level|Code|Message| +---------+------+---------------------------------------+ |Warning|1062|Duplicateentry'5'forkey'uidx_c1'| +---------+------+---------------------------------------+ 1rowinset(0.00sec) root:test>select*fromt3; +----+------+------+------+ |id|c1|c2|c3| +----+------+------+------+ |1|1|a|1| |2|2|a|1| |8|NULL|NULL|1| |14|4|bb|NULL| |17|5|cc|4| |18|6|dd|5| +----+------+------+------+ 6rowsinset(0.00sec)
replaceinto
replaceinto会尝试先插入数据,如果发现冲突进行删除。否则不做任何操作。
小case
root:test>showcreatetablet3G ***************************1.row*************************** Table:t3 CreateTable:CREATETABLE`t3`( `id`int(11)NOTNULLAUTO_INCREMENT, `c1`int(11)DEFAULTNULL, `c2`varchar(20)DEFAULTNULL, `c3`int(11)DEFAULTNULL, PRIMARYKEY(`id`), UNIQUEKEY`uidx_c1`(`c1`) )ENGINE=InnoDBAUTO_INCREMENT=4DEFAULTCHARSET=utf8 1rowinset(0.00sec) root:test>select*fromt3; +----+------+--------+------+ |id|c1|c2|c3| +----+------+--------+------+ |1|1|cc|4| |2|2|dd|5| |3|3|qwewqe|3| +----+------+--------+------+ 3rowsinset(0.00sec)
插入冲突数据
root:test>replaceintot3(c1,c2,c3)values(3,'new',8); QueryOK,2rowsaffected(0.02sec) root:test>select*fromt3; +----+------+------+------+ |id|c1|c2|c3| +----+------+------+------+ |1|1|cc|4| |2|2|dd|5| |4|3|new|8| +----+------+------+------+ 3rowsinset(0.00sec)
可以看到原有的记录已经没有了,新的记录又有了。
insertonduplicatekeyupdate
如果在insertinto语句末尾指定了insertonduplicatekeyupdate如果出现了重复值,则会在出现重复值以后进行update。
case
root:test>showcreatetablet3G ***************************1.row*************************** Table:t3 CreateTable:CREATETABLE`t3`( `id`int(11)NOTNULLAUTO_INCREMENT, `c1`int(11)DEFAULTNULL, `c2`varchar(20)DEFAULTNULL, `c3`int(11)DEFAULTNULL, PRIMARYKEY(`id`), UNIQUEKEY`uidx_c1`(`c1`) )ENGINE=InnoDBAUTO_INCREMENT=4DEFAULTCHARSET=utf8 1rowinset(0.00sec) root:test>select*fromt3; +----+------+------+------+ |id|c1|c2|c3| +----+------+------+------+ |1|1|fds|4| |2|2|ytu|3| |3|3|czx|5| +----+------+------+------+ 3rowsinset(0.00sec)
插入一条与记录id=3存在唯一键(列c1)冲突的数据
root:test>insertintot3(c1,c2,c3)values(3,'new',5)onduplicatekeyupdatec1=c1+3; QueryOK,2rowsaffected(0.01sec) root:test>select*fromt3; +----+------+------+------+ |id|c1|c2|c3| +----+------+------+------+ |1|1|fds|4| |2|2|ytu|3| |3|6|czx|5| +----+------+------+------+ 3rowsinset(0.00sec)
可以看到,id=3的记录发生了改变,c1=原有的c1+3,其他列没有改变。
以上就是MySql四种避免重复插入数据的方法的详细内容,更多关于MySQL避免插入重复数据的资料请关注毛票票其它相关文章!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。