replace into 和 insert into on duplicate key 區別

replace 的用法
當不衝突時相當於 insert,其餘列預設值
當 key 衝突時,自增列更新,replace 衝突列,其餘列預設值
Insert into …on duplicate key 的用法
不衝突時相當於 insert,其餘列預設值
當與 key 衝突時,只 update 相應欄位值。
實驗展示

表結構
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)
總結

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