MySQL中INSERT IGNORE INTO和REPLACE INTO的使用
生活随笔
收集整理的這篇文章主要介紹了
MySQL中INSERT IGNORE INTO和REPLACE INTO的使用
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
mysql中insert into和replace into以及insert ignore用法區(qū)別:?
mysql中常用的三種插入數(shù)據(jù)的語(yǔ)句:?
insert into表示插入數(shù)據(jù),數(shù)據(jù)庫(kù)會(huì)檢查主鍵,如果出現(xiàn)重復(fù)會(huì)報(bào)錯(cuò);?
replace into表示插入替換數(shù)據(jù),需求表中有PrimaryKey,或者unique索引,如果數(shù)據(jù)庫(kù)已經(jīng)存在數(shù)據(jù),則用新數(shù)據(jù)替換,如果沒(méi)有數(shù)據(jù)效果則和insert into一樣;?
insert ignore表示,如果中已經(jīng)存在相同的記錄,則忽略當(dāng)前新數(shù)據(jù);?
下面通過(guò)代碼說(shuō)明之間的區(qū)別,如下:?
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,因?yàn)閕d是主鍵,出現(xiàn)主鍵重復(fù)但使用了ignore則錯(cuò)誤被忽略?
replace into testtb(id,name,age)values(1,"aa",12);?
select * from testtb; //數(shù)據(jù)變?yōu)?,"aa",12
mysql中常用的三種插入數(shù)據(jù)的語(yǔ)句:?
insert into表示插入數(shù)據(jù),數(shù)據(jù)庫(kù)會(huì)檢查主鍵,如果出現(xiàn)重復(fù)會(huì)報(bào)錯(cuò);?
replace into表示插入替換數(shù)據(jù),需求表中有PrimaryKey,或者unique索引,如果數(shù)據(jù)庫(kù)已經(jīng)存在數(shù)據(jù),則用新數(shù)據(jù)替換,如果沒(méi)有數(shù)據(jù)效果則和insert into一樣;?
insert ignore表示,如果中已經(jīng)存在相同的記錄,則忽略當(dāng)前新數(shù)據(jù);?
下面通過(guò)代碼說(shuō)明之間的區(qū)別,如下:?
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,因?yàn)閕d是主鍵,出現(xiàn)主鍵重復(fù)但使用了ignore則錯(cuò)誤被忽略?
replace into testtb(id,name,age)values(1,"aa",12);?
select * from testtb; //數(shù)據(jù)變?yōu)?,"aa",12
轉(zhuǎn)載于:https://blog.51cto.com/shenliyang/1364830
總結(jié)
以上是生活随笔為你收集整理的MySQL中INSERT IGNORE INTO和REPLACE INTO的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux C Socket编程原理及简
- 下一篇: Hadoop文件系统常用命令