金钟街网站建设哪家好,seo1新地址在哪里,视频教程网,网站开发数据库连接失败以前不怎麼使用mysql#xff0c;最近有些事情#xff0c;需要用到php和mysql。目前使用的版本是5.5.1 發現mysql與sql server有很多不同。可能逐漸地會整理一些文檔出來給大家參考。 今天第一篇說說#xff0c;update操作的差異。在mysql中#xff0c;如果update語句要設置…以前不怎麼使用mysql最近有些事情需要用到php和mysql。目前使用的版本是5.5.1 發現mysql與sql server有很多不同。可能逐漸地會整理一些文檔出來給大家參考。 今天第一篇說說update操作的差異。在mysql中如果update語句要設置的新值與數據庫中當前的值是一樣的其實意味着無需更改。這種操作是不會被執行的。 為什麼會發現這個問題呢因為我在php程序中需要獲取update語句所影響的行數。我發現很多時候為0. 追查下去才明白他是這樣做的。從下圖中可以看到我執行update語句兩次第一次是修改了數據的(Changed:1)而第二次則沒有修改Changed:0)。 而查閱php的幫助文檔也很清楚地定義了這個行為 http://php.net/manual/en/function.mysql-affected-rows.php Returns the number of affected rows on success, and -1 if the last query failed. If the last query was a DELETE query with no WHERE clause, all of the records will have been deleted from the table but this function will return zero with MySQL versions prior to 4.1.2. When using UPDATE, MySQL will not update columns where the new value is the same as the old value. This creates the possibility thatmysql_affected_rows() may not actually equal the number of rows matched, only the number of rows that were literally affected by the query. The REPLACE statement first deletes the record with the same primary key and then inserts the new record. This function returns the number of deleted records plus the number of inserted records. 同樣的語句在sql server中就不是這樣。例如下面這樣其實兩個語句是一模一樣的但每個都會被執行一行被更改 為了證明這一點我可以寫一個觸發器來檢測一下CREATE TRIGGER TestTrigger
ON employees
FOR UPDATEAS
BEGINDECLARE id INTDECLARE name NVARCHAR(50)SELECT idUID,nameName FROM DELETEDPRINT idPRINT nameEND 再次執行語句我們可以看到如下結果 這說明什麼問題說明sql server的update語句確實每次都會執行不管值是否有必要進行變化。 在sql server中update操作會有兩個步驟首先執行delete操作然後執行insert操作。 上面的觸發器代碼中訪問的deleted表是一個邏輯表裡面保存的就是delete的數據也就是我們經常所說的“舊值”转载于:https://www.cnblogs.com/chenxizhang/archive/2011/04/24/2026223.html