MySQL中INSERT IGNORE INTO和REPLACE INTO的使用
生活随笔
收集整理的这篇文章主要介绍了
MySQL中INSERT IGNORE INTO和REPLACE INTO的使用
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
mysql中insert into和replace into以及insert ignore用法区别:
mysql中常用的三种插入数据的语句:
insert into表示插入数据,数据库会检查主键,如果出现重复会报错;
replace into表示插入替换数据,需求表中有PrimaryKey,或者unique索引,如果数据库已经存在数据,则用新数据替换,如果没有数据效果则和insert into一样;
insert ignore表示,如果中已经存在相同的记录,则忽略当前新数据;
下面通过代码说明之间的区别,如下:
create table testtb(
id int not null primary key,
name varchar(50),
age int
);
insert into testtb(id,name,age)values(1,"bb",13);
select * from testtb;
insert ignore into testtb(id,name,age)values(1,"aa",13);
select * from testtb;//仍是1,“bb”,13,因为id是主键,出现主键重复但使用了ignore则错误被忽略
replace into testtb(id,name,age)values(1,"aa",12);
select * from testtb; //数据变为1,"aa",12
mysql中常用的三种插入数据的语句:
insert into表示插入数据,数据库会检查主键,如果出现重复会报错;
replace into表示插入替换数据,需求表中有PrimaryKey,或者unique索引,如果数据库已经存在数据,则用新数据替换,如果没有数据效果则和insert into一样;
insert ignore表示,如果中已经存在相同的记录,则忽略当前新数据;
下面通过代码说明之间的区别,如下:
create table testtb(
id int not null primary key,
name varchar(50),
age int
);
insert into testtb(id,name,age)values(1,"bb",13);
select * from testtb;
insert ignore into testtb(id,name,age)values(1,"aa",13);
select * from testtb;//仍是1,“bb”,13,因为id是主键,出现主键重复但使用了ignore则错误被忽略
replace into testtb(id,name,age)values(1,"aa",12);
select * from testtb; //数据变为1,"aa",12
转载于:https://blog.51cto.com/shenliyang/1364830
总结
以上是生活随笔为你收集整理的MySQL中INSERT IGNORE INTO和REPLACE INTO的使用的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: Linux C Socket编程原理及简
- 下一篇: Redis 数据库结构设计