這篇文章給大家分享的是有關如何避免MySQL替換邏輯SQL的坑的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
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)
感謝各位的閱讀!關于“如何避免MySQL替換邏輯SQL的坑”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。