这篇文章给大家分享的是有关如何避免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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。