mysql insert 主鍵 重複問題

2022-05-06 19:03:10 字數 759 閱讀 4804

**:

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 主鍵重複

mysql中insert into和replace into以及insert ignore用法區別 mysql中常用的三種插入資料的語句 insert into表示插入資料,資料庫會檢查主鍵,如果出現重複會報錯 replace into表示插入替換資料,需求表中有primarykey,或者uniqu...

Mongo ID主鍵重複 id dup key

今天在開發中遇到mongo主鍵重複,查詢網上關於mongo主鍵未找到相關解釋,經後續仔細排查專案上使用mongo的細節,發現專案上使用了資料版本。由於mongo本身未支援事務操作,故專案中使用了 版本 version private long optlock org.springframework....

Mysql insert語句的優化

1 如果你同時從同一客戶插入很多行,使用多個值表的insert語句。這比使用分開insert語句快 在一些情況中幾倍 insert into test values 1,2 1,3 1,4 2 如果你從不同客戶插入很多行,能通過使用insert delayed語句得到更高的速度。delayed的含義...