语法: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
轉載于:https://www.cnblogs.com/RoadGY/archive/2011/09/28/2193733.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的语法:MySQL中INSERT IGNORE INTO和REPLACE INTO的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何与客户相处
- 下一篇: 基于NHibernate的三层结构应用程