欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL中Replace語句用法實例詳解

 更新時間:2022年08月08日 09:01:22   作者:ixxqq  
mysql的replace函數(shù)是一個非常方便的替換函數(shù),下面這篇文章主要給大家給大家介紹了關(guān)于MySQL中Replace語句用法的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

replace into平時在開發(fā)中很少用到,這次是因為在做一個生成分布式ID的開源項目,調(diào)研雅虎推出的一個基于數(shù)據(jù)庫生成唯一id生成方案:flickr 碰到的一個知識盲點,僅以此篇記錄一下。

一、replace into函數(shù)

表結(jié)構(gòu)

CREATE TABLE `id_generator` (  
`id` bigint(20) unsigned NOT NULL auto_increment,  
`stub` char(1) NOT NULL default '',  
  PRIMARY KEY  (`id`),  
UNIQUE KEY `stub` (`stub`)  
) ENGINE=MyISAM;

id列是主鍵索引,stub 列是唯一索引 。

replace into向數(shù)據(jù)庫中插入數(shù)據(jù)時,如果數(shù)據(jù)重復(fù),則刪除重復(fù)的那行數(shù)據(jù),然后在插入一行。

replace具備替換擁有唯一索引或者主鍵索引重復(fù)數(shù)據(jù)的能力,也就是如果使用replace into插入的數(shù)據(jù)的唯一索引或者主鍵索引與之前的數(shù)據(jù)有重復(fù)的情況,將會刪除原先的數(shù)據(jù),然后再進(jìn)行添加。

那怎么判斷是否重復(fù)的標(biāo)準(zhǔn)就是:唯一索引或者主鍵索引是否一致。語法:replace into table( col1, col2, col3 ) values ( val1, val2, val3 )
語義:向table表中col1, col2, col3列replace數(shù)據(jù)val1,val2,val3

以id_generator表為例:

REPLACE INTO id_generator(stub) VALUES ('trade')
 
 
執(zhí)行信息如下:
No errors; 1 rows affected, taking 20ms

表示只添加了一行。

執(zhí)行結(jié)果:

+----+--------------+
| id | stub         |
+----+--------------+
| 1  | trade        |
+----+--------------+

接續(xù)執(zhí)行如下語句:

REPLACE INTO id_generator(stub) VALUES ('trade')
 
 
執(zhí)行信息如下:
No errors; 2 rows affected, taking 23ms

因為刪除了一行,添加了一行。

執(zhí)行結(jié)果:

+----+--------------+
| id | stub         |
+----+--------------+
| 3  | trade        |
+----+--------------+

因為stub列創(chuàng)建列唯一索引,所以replace into當(dāng)stub有重復(fù)的數(shù)據(jù)行時,會刪除這行數(shù)據(jù)重新添加,導(dǎo)致id發(fā)生變化。

如果replace into唯一索引 id 重復(fù)的話,會怎么樣呢?

REPLACE INTO id_generator(id) VALUES (3)
 
 
執(zhí)行信息如下:
No errors; 2 rows affected, taking 28ms

因為刪除了一行,添加了一行。

執(zhí)行結(jié)果:

+----+--------------+
| id | stub         |
+----+--------------+
| 3  |              |
+----+--------------+

這時我們看到stub列的值為空字符串,因為id是唯一索引,id=3的記錄已經(jīng)存在,所以replace into會刪除id=3的記錄,重新添加,導(dǎo)致stub為空。

二、replace into 、insert ignore 和 insert into的區(qū)別

replace into 跟 insert into 功能類似。

不同點在于:

insert into 最普遍的插入,如果表中存在主鍵相同的數(shù)據(jù),執(zhí)行會報錯。

insert ignore 如果表中存在主鍵相同的數(shù)據(jù)不在插入該條數(shù)據(jù),反之則插入(存在則忽略,反之插入)。

replace into 首先嘗試插入數(shù)據(jù)到表中。

  1. 如果發(fā)現(xiàn)表中已經(jīng)有此行數(shù)據(jù)(根據(jù)主鍵或者唯一索引判斷)則先刪除此行數(shù)據(jù),然后插入新的數(shù)據(jù)。
  2. 否則,直接插入新數(shù)據(jù)。

注意:**插入數(shù)據(jù)的表必須有主鍵或者是唯一索引!**否則的話,replace into 會直接插入數(shù)據(jù),這將導(dǎo)致表中出現(xiàn)重復(fù)的數(shù)據(jù)

三、replace函數(shù)

語法:replace(field,search,replace)

說明:field - 數(shù)據(jù)庫表的列名

search - 需要替換的字符串

replace - 替換成的字符串

語義:將列名:field 中出現(xiàn)的search字符串,全部替換成replace字符串。

實例:

update id_generator set stub = replace(stub,'trade','user')
 
select replace(uuid(), '-', '');

附:Mysql中的replace into跟insert into用法類似

  • INSERT :會每次插入一條新的數(shù)據(jù)。
  • REPLACE:先看表中是否存在此條數(shù)據(jù),如果存在,先刪除該條數(shù)據(jù),再插入一條新的數(shù)據(jù);如果不存在,則直接插入一條新的數(shù)據(jù)

重點:

  • 根據(jù)表中的主鍵或唯一索引來判斷,如果表中沒有主鍵或唯一索引,那么REPLACE INTO 就相當(dāng)于 INSERT

INTO,會直接插入一條數(shù)據(jù)。

總結(jié)

到此這篇關(guān)于MySQL中Replace語句用法詳解的文章就介紹到這了,更多相關(guān)MySQL Replace用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論