程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> MySQL的replace into詳解

MySQL的replace into詳解

編輯:MySQL綜合教程

本教程主要拿了兩篇文章來介紹關於mysql的replace into語句的用法,有需要的朋友可以參考一下。

REPLACE的運行與INSERT很相像。只有一點除外,如果表中的一個舊記錄與一個用於PRIMARY KEY或一個UNIQUE索引的新記錄具有相同的值,則在新記錄被插入之前,舊記錄被刪除。

注意,除非表有一個PRIMARY KEY或UNIQUE索引,否則,使用一個REPLACE語句沒有意義。該語句會與INSERT相同,因為沒有索引被用於確定是否新行復制了其它的行。

所有列的值均取自在REPLACE語句中被指定的值。所有缺失的列被設置為各自的默認值,這和INSERT一樣。您不能從當前行中引用值,也不能在新行中使用值。如果您使用一個例如“SET col_name = col_name + 1”的賦值,則對位於右側的列名稱的引用會被作為DEFAULT(col_name)處理。因此,該賦值相當於SET col_name = DEFAULT(col_name) + 1。

為了能夠使用REPLACE,您必須同時擁有表的INSERT和DELETE權限。

REPLACE語句會返回一個數,來指示受影響的行的數目。該數是被刪除和被插入的行數的和。如果對於一個單行REPLACE該數為1,則一行被插入,同時沒有行被刪除。如果該數大於1,則在新行被插入前,有一個或多個舊行被刪除。如果表包含多個唯一索引,並且新行復制了在不同的唯一索引中的不同舊行的值,則有可能是一個單一行替換了多個舊行。

受影響的行數可以容易地確定是否REPLACE只添加了一行,或者是否REPLACE也替換了其它行:檢查該數是否為1(添加)或更大(替換)。

如果您正在使用C API,則可以使用mysql_affected_rows()函數獲得受影響的行數。

目前,您不能在一個子查詢中,向一個表中更換,同時從同一個表中選擇。

以下是所用算法的更詳細的說明(該算法也用於LOAD DATA…REPLACE):

1. 嘗試把新行插入到表中

2. 當因為對於主鍵或唯一關鍵字出現重復關鍵字錯誤而造成插入失敗時:

a. 從表中刪除含有重復關鍵字值的沖突行

b. 再次嘗試把新行插入到表中

今天喉嚨疼死我了,閃人睡覺去.

使用格式:

 代碼如下 復制代碼

REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
VALUES ({expr | DEFAULT},…),(…),…
或:

REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name
SET col_name={expr | DEFAULT}, …
或:

REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
SELECT …


一。 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鍵對應的內容不存在,則插入一條記錄。

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved