博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]MySQL的replace into
阅读量:7283 次
发布时间:2019-06-30

本文共 2384 字,大约阅读时间需要 7 分钟。

转载自: 网络

一。 REPLACE INTO table_name ( `col_A`, `col_B`) VALUES ( `col A data`, `col B data`) ;

Replaces items in a row. This action is dependant on the "id" because when doing a REPLACE, you must include the PRIMARY (unique) column. Since we established the "id" column as our PRIMARY key (when establishing the table), MySQL needs this info so it knows which row we are talking about. If we didn't include the "id" coumn, MySQL will have no idea which row we are trying to replace.

In this example, we are replacing row #2

$sql = "REPLACE INTO music (id,artist,album) VALUES ('2','The Beatles','Let It Be')"; mysql_query($sql);
id artist album title track year
1 the beatles Abbey Road
2 The Beatles Let It Be
3 Abbey Road 3 test
Records: 3

 

Here, we haven't defined the "id" column. Hence, MySQL doesn't know which row to replace, so it just adds a new row.

So as we can see, the REPLACE feature acts very similar to INSERT. We can use this to our advantage!

Again, REPLACE behaves much like INSERT except that:

- if the PRIMARY ("unique column") is supplied, the existing row will be updated
- if the PRIMARY ("unique column") is not provided, a new row will be added.

$sql = "REPLACE INTO music (artist,album) VALUES ('The Beatles','The Magical Mystery Tour')"; mysql_query($sql);
id artist album title track year
1 the beatles Abbey Road
2 The Beatles Let It Be
3 Abbey Road 3 test
4 The Beatles The Magical Mystery Tour
Records: 4

NOTE: You may get errors when trying this kind of REPLACE, because usually the "unique column" can not be null (or empty). Some people like to set up the database where the "unique column" is automatically incremented by MySQL. This is kind of confusing and can lead to a headache. So just remember to include the "unique column" when using the REPLACE statement, or you'll get duplicate rows... OR use the UPDATE statement.

二。 Mysql replace into 与 insert into on duplicate key update 的区别这两种方式的作用是如果数据库中存在记录就更新,否则就插入新记录,但是在使用上也是有一点区别的。 总结如下: 1. 如果表中不存在主键记录,replace和insert*update都与insert是一样的特点。 2. 如果表中存在主键记录,replace相当于执行delete 和 insert两条操作,而insert*update的相当于执行if exist do update else do insert操作。因此,如果replace填充的字段不全,则会导致未被更新的字段都会修改为默认值,并且如果有自增id的话,自增id会变化为最新的值(这样如果是以自增id为标志的话可能导致记录丢失);而insert*update只是更新部分字段,对于未被更新的字段不会变化(不会强制修改为默认值)。

三。unique键的情况

如果存在unique键(无论是某个字段或者是组合字段), replace into执行的效果是:如果unique键对应的内容已经存在,那么更新主键;如果unique键对应的内容不存在,则插入一条记录。

你可能感兴趣的文章
苹果Facetime支持32人视频通话,一年前,即构科技已经做到了
查看>>
免费SSL证书Let’s Encrypt安装使用教程
查看>>
面向对象编程的设计原则
查看>>
Bitcoin Cash 的链上交易数
查看>>
渐进式Express源码教程,保证你能看懂 | 小白也能懂源码
查看>>
Android微信新版全自动抢红包助手
查看>>
解决 "Script Error" 的另类思路
查看>>
前端每周清单第 55 期: MobX 4 特性概览,iOS Hacks 分享, 分布式事务详解
查看>>
前端每周清单第 53 期:Go 与 WebAssembly, React Suspense 演练, CSS 技巧
查看>>
微服务如何落地
查看>>
微内核专题系列
查看>>
利用tess-two和cv4j实现简单的ocr功能
查看>>
Http1.1客户端缓存总结以及hapi实践
查看>>
shell基础学习笔记
查看>>
【centos7.2部署第7期】jenkins+github+nodejs+pm2实现持续集成
查看>>
JavaScript数组的十八般武艺
查看>>
用 Python 写脚本发送工作周报
查看>>
程序员算法练习四
查看>>
搭建本地 mock 服务
查看>>
iOS笔记:进一步认识 ==、isEqual、hash
查看>>