INSERT IGNORE 與INSERT INTO的區別就是INSERT IGNORE會忽略數據庫中已經存在 的數據,若是數據庫沒有數據,就插入新的數據,若是有數據的話就跳過這條數據。這樣就能夠保留數據庫中已經存在數據,達到在間隙中插入數據的目的。
eg: insert ignore into table(name) select name from table2 mysql
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",12sql