細微之處看看mysql與sql server的一些差別
2011-04-24 14:42
120 查看
以前不怎麼使用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中就不是這樣。例如下面這樣,其實兩個語句是一模一樣的,但每個都會被執行,一行被更改
為了證明這一點,我可以寫一個觸發器來檢測一下
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
再次執行語句,我們可以看到如下結果
這說明什麼問題?說明sql server的update語句確實每次都會執行,不管值是否有必要進行變化。
在sql server中,update操作會有兩個步驟,首先執行delete操作,然後執行insert操作。
上面的觸發器代碼中,訪問的deleted表,是一個邏輯表,裡面保存的就是delete的數據,也就是我們經常所說的“舊值”
發現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 UPDATE AS BEGIN DECLARE @id INT DECLARE @name NVARCHAR(50) SELECT @id=UID,@name=Name FROM DELETED PRINT @id PRINT @name END
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
再次執行語句,我們可以看到如下結果
這說明什麼問題?說明sql server的update語句確實每次都會執行,不管值是否有必要進行變化。
在sql server中,update操作會有兩個步驟,首先執行delete操作,然後執行insert操作。
上面的觸發器代碼中,訪問的deleted表,是一個邏輯表,裡面保存的就是delete的數據,也就是我們經常所說的“舊值”
相关文章推荐
- 細微之處看看mysql與sql server的一些差別
- 我们来看看load data infile 需要注意的一些地方
- 全局变量的一些思考(适合刚入门C#朋友看看)
- 【转载】看看一些著名的软件都用什么编程语言写成
- 看看我写汇编程序的一些原则
- iOS备忘录之IOS开发的一些网站(看看还是不错的)
- 我们来看看load data infile 需要注意的一些地方
- 一些医学健康常识,你遵循了几条呢?看看吧,对大家身体大有好处!
- 【一些事晚报】酷派360撕逼升级 看看昨天夜里都发生了什么
- 一些很有哲理的话,平时没事看看,感触颇多~~~
- 看看一些著名的软件都用什么编程语言写成(更新中...)?
- 一些有意思的题目,3题,看看你能不能全部做对?
- 看看这个经验里的一些题
- 学了一阵子python pygame, 写一些总结,回头看看哪些地方不足
- 可点进来看看的一些Python基础
- 在windows平台工作和开发的伙伴们,发现一个很牛B的公司Sysinternals,提供的一些工具大家可以看看是否可以用
- ecshop的一些东西,来看看
- 今天22岁,对于30岁以前的一些事情,我思考了很久,后来却发现网上已经有前辈总结出来了,呵呵,贴出来大家一起看看
- 看看一些著名的软件都用什么编程语言写成
- 看看一些著名的软件都用什么编程语言写成(更新中...)?