国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

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

2024-07-24 12:50:28
字體:
來源:轉載
供稿:網友

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;

表數據

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的用法

當沒有key沖突時,replace into 相當于insert,其余列默認值

當key沖突時,自增列更新,replace沖突列,其余列默認值

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)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宿松县| 美姑县| 常熟市| 福安市| 四子王旗| 陵水| 托克逊县| 手游| 蕲春县| 象山县| 栾城县| 颍上县| 南江县| 弥勒县| 湟源县| 大洼县| 交口县| 公主岭市| 岳阳市| 淮南市| 紫云| 大连市| 乐至县| 新疆| 江达县| 旺苍县| 巴楚县| 盱眙县| 原平市| 东安县| 南华县| 渝北区| 舟曲县| 大庆市| 邢台县| 平果县| 兴仁县| 闻喜县| 嫩江县| 珲春市| 武平县|