共計 3093 個字符,預(yù)計需要花費 8 分鐘才能閱讀完成。
這篇文章給大家分享的是有關(guān)如何避免 MySQL 替換邏輯 SQL 的坑的內(nèi)容。丸趣 TV 小編覺得挺實用的,因此分享給大家做個參考,一起跟隨丸趣 TV 小編過來看看吧。
replace into 和 insert into on duplicate key 區(qū)別
replace 的用法
當(dāng)不沖突時相當(dāng)于 insert,其余列默認(rèn)值
當(dāng) key 沖突時,自增列更新,replace 沖突列,其余列默認(rèn)值
Com_replace 會加 1
Innodb_rows_updated 會加 1
Insert into …on duplicate key 的用法
不沖突時相當(dāng)于 insert,其余列默認(rèn)值
當(dāng)與 key 沖突時,只 update 相應(yīng)字段值。
Com_insert 會加 1
Innodb_rows_inserted 會增加 1
實驗展示
表結(jié)構(gòu)
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;
表數(shù)據(jù)
root@127.0.0.1 (helei) select * from helei1;
+----+-----------+-----+
| id | name | age |
+----+-----------+-----+
| 1 | 賀磊 | 26 |
| 2 | 小明 | 28 |
| 3 | 小紅 | 26 |
+----+-----------+-----+
3 rows in set (0.00 sec)
replace into 用法
root@127.0.0.1 (helei) replace into helei1 (name) values( 賀磊
Query OK, 2 rows affected (0.00 sec)
root@127.0.0.1 (helei) select * from helei1;
+----+-----------+-----+
| id | name | age |
+----+-----------+-----+
| 2 | 小明 | 28 |
| 3 | 小紅 | 26 |
| 4 | 賀磊 | 0 |
+----+-----------+-----+
3 rows in set (0.00 sec)
root@127.0.0.1 (helei) replace into helei1 (name) values( 愛璇
Query OK, 1 row affected (0.00 sec)
root@127.0.0.1 (helei) select * from helei1;
+----+-----------+-----+
| id | name | age |
+----+-----------+-----+
| 2 | 小明 | 28 |
| 3 | 小紅 | 26 |
| 4 | 賀磊 | 0 |
| 5 | 愛璇 | 0 |
+----+-----------+-----+
4 rows in set (0.00 sec)
replace 的用法
當(dāng)沒有 key 沖突時,replace into 相當(dāng)于 insert,其余列默認(rèn)值
當(dāng) key 沖突時,自增列更新,replace 沖突列,其余列默認(rèn)值
Insert into …on duplicate key:
root@127.0.0.1 (helei) select * from helei1;
+----+-----------+-----+
| id | name | age |
+----+-----------+-----+
| 2 | 小明 | 28 |
| 3 | 小紅 | 26 |
| 4 | 賀磊 | 0 |
| 5 | 愛璇 | 0 |
+----+-----------+-----+
4 rows in set (0.00 sec)
root@127.0.0.1 (helei) insert into helei1 (name,age) values(賀磊 ,0) on duplicate key update age=100;
Query OK, 2 rows affected (0.00 sec)
root@127.0.0.1 (helei) select * from helei1;
+----+-----------+-----+
| id | name | age |
+----+-----------+-----+
| 2 | 小明 | 28 |
| 3 | 小紅 | 26 |
| 4 | 賀磊 | 100 |
| 5 | 愛璇 | 0 |
+----+-----------+-----+
4 rows in set (0.00 sec)
root@127.0.0.1 (helei) select * from helei1;
+----+-----------+-----+
| id | name | age |
+----+-----------+-----+
| 2 | 小明 | 28 |
| 3 | 小紅 | 26 |
| 4 | 賀磊 | 100 |
| 5 | 愛璇 | 0 |
+----+-----------+-----+
4 rows in set (0.00 sec)
root@127.0.0.1 (helei) insert into helei1 (name) values(愛璇) on duplicate key update age=120;
Query OK, 2 rows affected (0.01 sec)
root@127.0.0.1 (helei) select * from helei1;
+----+-----------+-----+
| id | name | age |
+----+-----------+-----+
| 2 | 小明 | 28 |
| 3 | 小紅 | 26 |
| 4 | 賀磊 | 100 |
| 5 | 愛璇 | 120 |
+----+-----------+-----+
4 rows in set (0.00 sec)
root@127.0.0.1 (helei) insert into helei1 (name) values(不存在) on duplicate key update age=80;
Query OK, 1 row affected (0.00 sec)
root@127.0.0.1 (helei) select * from helei1;
+----+-----------+-----+
| id | name | age |
+----+-----------+-----+
| 2 | 小明 | 28 |
| 3 | 小紅 | 26 |
| 4 | 賀磊 | 100 |
| 5 | 愛璇 | 120 |
| 8 | 不存在 | 0 |
+----+-----------+-----+
5 rows in set (0.00 sec)
感謝各位的閱讀!關(guān)于“如何避免 MySQL 替換邏輯 SQL 的坑”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!