避免MySQL替換邏輯SQL的坑爹操作

2021-10-01 01:22:27 字數 4231 閱讀 1526

replace into和insert into on duplicate key 區別

replace的用法

當不衝突時相當於insert,其餘列預設值

當key衝突時,自增列更新,replace衝突列,其餘列預設值

com_replace會加1

innodb_rows_updated會加1

insert into …on duplicate key的用法

不衝突時相當於insert,其餘列預設值

當與key衝突時,只update相應字段值。

com_insert會加1

innodb_rows_inserted會增加1

實驗展示

表結構

create table helei1(

id int(10) unsigned not null auto_increment,

name varchar(20) not null default '',

age tinyint(3) unsigned not null default 0,

primary key(id),

unique key uk_name (name)

)engine=innodb auto_increment=1

default charset=utf8;

表資料

[email protected] (helei)> select * from helei1;

±—±----------±----+

| id | name | age |

±—±----------±----+

| 1 | 賀磊 | 26 |

| 2 | 小明 | 28 |

| 3 | 小紅 | 26 |

±—±----------±----+

3 rows in set (0.00 sec)

replace into用法

[email protected] (helei)> replace into helei1 (name) values(『賀磊』);

query ok, 2 rows affected (0.00 sec)

[email protected] (helei)> select * from helei1;

±—±----------±----+

| id | name | age |

±—±----------±----+

| 2 | 小明 | 28 |

| 3 | 小紅 | 26 |

| 4 | 賀磊 | 0 |

±—±----------±----+

3 rows in set (0.00 sec)

[email protected] (helei)> replace into helei1 (name) values(『愛璇』);

query ok, 1 row affected (0.00 sec)

[email protected] (helei)> select * from helei1;

±—±----------±----+

| id | name | age |

±—±----------±----+

| 2 | 小明 | 28 |

| 3 | 小紅 | 26 |

| 4 | 賀磊 | 0 |

| 5 | 愛璇 | 0 |

±—±----------±----+

4 rows in set (0.00 sec)

replace的用法

當沒有key衝突時,replace into 相當於insert,其餘列預設值

當key衝突時,自增列更新,replace衝突列,其餘列預設值

insert into …on duplicate key:

[email protected] (helei)> select * from helei1;

±—±----------±----+

| id | name | age |

±—±----------±----+

| 2 | 小明 | 28 |

| 3 | 小紅 | 26 |

| 4 | 賀磊 | 0 |

| 5 | 愛璇 | 0 |

±—±----------±----+

4 rows in set (0.00 sec)

[email protected] (helei)> insert into helei1 (name,age) values(『賀磊』,0) on duplicate key update age=100;

query ok, 2 rows affected (0.00 sec)

[email protected] (helei)> select * from helei1;

±—±----------±----+

| id | name | age |

±—±----------±----+

| 2 | 小明 | 28 |

| 3 | 小紅 | 26 |

| 4 | 賀磊 | 100 |

| 5 | 愛璇 | 0 |

±—±----------±----+

4 rows in set (0.00 sec)

[email protected] (helei)> select * from helei1;

±—±----------±----+

| id | name | age |

±—±----------±----+

| 2 | 小明 | 28 |

| 3 | 小紅 | 26 |

| 4 | 賀磊 | 100 |

| 5 | 愛璇 | 0 |

±—±----------±----+

4 rows in set (0.00 sec)

[email protected] (helei)> insert into helei1 (name) values(『愛璇』) on duplicate key update age=120;

query ok, 2 rows affected (0.01 sec)

[email protected] (helei)> select * from helei1;

±—±----------±----+

| id | name | age |

±—±----------±----+

| 2 | 小明 | 28 |

| 3 | 小紅 | 26 |

| 4 | 賀磊 | 100 |

| 5 | 愛璇 | 120 |

±—±----------±----+

4 rows in set (0.00 sec)

[email protected] (helei)> insert into helei1 (name) values(『不存在』) on duplicate key update age=80;

query ok, 1 row affected (0.00 sec)

[email protected] (helei)> select * from helei1;

±—±----------±----+

| id | name | age |

±—±----------±----+

| 2 | 小明 | 28 |

| 3 | 小紅 | 26 |

| 4 | 賀磊 | 100 |

| 5 | 愛璇 | 120 |

| 8 | 不存在 | 0 |

±—±----------±----+

5 rows in set (0.00 sec)

總結

replace into這種用法,相當於如果發現衝突鍵,先做乙個delete操作,再做乙個insert 操作,未指定的列使用預設值,這種情況會導致自增主鍵產生變化,如果表中存在外來鍵或者業務邏輯上依賴主鍵,那麼會出現異常。因此建議使用insert into …on duplicate key。由於編寫時間也很倉促,文中難免會出現一些錯誤或者不準確的地方,不妥之處懇請讀者批評指正。

關於避免MySQL替換邏輯SQL的坑爹操作詳解

replace into和insert into on duplicate key 區別 replace的用法 當不衝突時相當於insert,其餘列預設值 當key衝突時,自增列更新,replace衝突列,其餘列預設值 com replace會加1 innodb rows updated會加1 in...

MySql避免重複插入sql語句

惡補下mysql中的幾個特殊sql語法,避免面試過程中被人問到,答不上來 1 ignore primary key 用例 insert ignore into user table id,name,age values 1,wpf 20 若記錄已經存在,則忽略本次操作,返回影響行為0,否則執行插入操...

mysql批量替換的SQL語句怎麼寫

先看一下分頁的基本原理 我拿的是csdn那個百萬級資料庫來測試!select from csdn order by id desc limit 100000,2000 耗時 0.813ms 分析 對上面的mysql語句說明 limit 100000,2000的意思掃瞄滿足條件的102000行,扔掉前...