日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
MySQLVSSQLServer之用法差別

由于工作的原因:上家公司的數(shù)據(jù)庫(kù)全采用MySQL,所以不得不用它。因此也學(xué)到了MySQL的一些知識(shí),但考慮到今后可能沒(méi)機(jī)會(huì)使用了,所以想趁現(xiàn)在離職在家休息,打算把這些東西整理一下,也為了萬(wàn)一今后能用上,留個(gè)參考的資源??紤]到一直在使用SQL Server,所以就打算直接與SQL Server對(duì)比來(lái)寫(xiě)。

專(zhuān)注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)忠縣免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

本文將主要列出MySQL與SQL Server不同的地方,且以常用的存儲(chǔ)過(guò)程的相關(guān)內(nèi)容為主。

1. 標(biāo)識(shí)符限定符

數(shù)據(jù)庫(kù) 標(biāo)識(shí)符限定符
SQL Server []
MySQL ``

2. 字符串相加

數(shù)據(jù)庫(kù) 字符串相加
SQL Server 直接用 +
MySQL concat()

3. isnull()

數(shù)據(jù)庫(kù) isnull()
SQL Server isnull()
MySQL ifnull()

注意:MySQL也有isnull()函數(shù),但意義不一樣

4. getdate()

數(shù)據(jù)庫(kù) getdate()
SQL Server getdate()
MySQL now()

5. newid()

數(shù)據(jù)庫(kù) newid()
SQL Server newid()
MySQL uuid()

6. @@ROWCOUNT

數(shù)據(jù)庫(kù) @@ROWCOUNT
SQL Server @@ROWCOUNT
MySQL row_count()

注意:MySQL的這個(gè)函數(shù)僅對(duì)于update, insert, delete有效

7. SCOPE_IDENTITY()

數(shù)據(jù)庫(kù) SCOPE_IDENTITY()
SQL Server SCOPE_IDENTITY()
MySQL last_insert_id()

8. if ... else ...

數(shù)據(jù)庫(kù) if ... else ...
SQL Server
     
     
  1.  IF Boolean_expression   
  2.      { sql_statement | statement_block }   
  3. [ ELSE   
  4.      { sql_statement | statement_block } ]  

-- 若要定義語(yǔ)句塊,請(qǐng)使用控制流關(guān)鍵字 BEGIN 和 END。

MySQL
     
     
  1. IF search_condition THEN statement_list  
  2.     [ELSEIF search_condition THEN statement_list] ...  
  3.     [ELSE statement_list]  
  4. END IF  

注意:對(duì)于MySql來(lái)說(shuō),then, end if是必須的。類(lèi)似的還有其它的流程控制語(yǔ)句,這里就不一一列出。

9. declare

其實(shí),SQL Server和MySQL都有這個(gè)語(yǔ)句,用于定義變量,但差別在于:在MySQL中,DECLARE僅被用在BEGIN ... END復(fù)合語(yǔ)句里,并且必須在復(fù)合語(yǔ)句的開(kāi)頭,在任何其它語(yǔ)句之前。這個(gè)要求在寫(xiě)游標(biāo)時(shí),會(huì)感覺(jué)很BT.

10. 游標(biāo)的寫(xiě)法

SQL Server

 
 
  1. declare @tempShoppingCart table (ProductId int, Quantity int)  
  2. insert into @tempShoppingCart (ProductId, Quantity)  
  3.     select ProductId, Quantity from ShoppingCart where UserGuid = @UserGuid  
  4.  
  5.  
  6. declare @productId int 
  7. declare @quantity int 
  8. declare tempCartCursor cursor for   
  9.         select ProductId, Quantity from @tempShoppingCart  
  10.  
  11. open tempCartCursor  
  12. fetch next from tempCartCursor into @productId, @quantity  
  13. while  @@FETCH_STATUS = 0  
  14. begin 
  15.     update Product set SellCount = SellCount + @quantity    where productId = @productId  
  16.  
  17.     fetch next from tempCartCursor into @productId, @quantity  
  18. end 
  19.  
  20. close tempCartCursor  
  21. deallocate tempCartCursor 

MySQL

 
 
  1. declare m_done int default 0;  
  2. declare m_sectionId int;  
  3. declare m_newsId int;  
  4.  
  5. declare _cursor_SN cursor for select sectionid, newsid from _temp_SN;  
  6. declare continue handler for not found set m_done = 1;  
  7.  
  8. create temporary table _temp_SN select sectionid, newsid from SectionNews  group by sectionid, newsid having count(*) > 1;  
  9.  
  10. open _cursor_SN;  
  11. while( m_done = 0 ) do  
  12.     fetch _cursor_SN into m_sectionId, m_newsId;  
  13.       
  14.     if( m_done = 0 ) then   
  15.         -- 具體的處理邏輯  
  16.     end if;  
  17. end while;  
  18. close _cursor_SN;  
  19. drop table _temp_SN; 

注意:為了提高性能,通常在表變量上打開(kāi)游標(biāo),不要直接在數(shù)據(jù)表上打開(kāi)游標(biāo)。

11. 分頁(yè)的處理

SQL Server

 
 
  1. create procedure GetProductByCategoryId(   
  2.     @CategoryID int,   
  3.     @PageIndex int = 0,   
  4.     @PageSize int = 20,   
  5.     @TotalRecords int output 
  6. )   
  7. as 
  8. begin 
  9.        
  10. declare @ResultTable table 
  11. (   
  12.     RowIndex int,   
  13.     ProductID int,   
  14.     ProductName nvarchar(50),   
  15.     CategoryID int,   
  16.     Unit nvarchar(10),   
  17.     UnitPrice money,   
  18.     Quantity int 
  19. );   
  20.        
  21. insert into @ResultTable   
  22. select row_number() over (order by ProductID asc) as RowIndex,   
  23.        p.ProductID, p.ProductName, p.CategoryID, p.Unit, p.UnitPrice, p.Quantity   
  24. from   Products as p   
  25. where CategoryID = @CategoryID;   
  26.          
  27. select  @TotalRecords = count(*) from  @ResultTable;   
  28.        
  29. select *   
  30. from   @ResultTable   
  31. where  RowIndex > (@PageSize * @PageIndex) and RowIndex <= (@PageSize * (@PageIndex+1));   
  32.        
  33. end; 

當(dāng)然,SQL Server中并不只有這一種寫(xiě)法,只是這種寫(xiě)法是比較常見(jiàn)而已。

MySQL

 
 
  1. create procedure GetProductsByCategoryId(  
  2.    in _categoryId int,  
  3.    in _pageIndex int,  
  4.    in _pageSize int,  
  5.    out _totalRecCount int 
  6. )  
  7. begin 
  8.    
  9.    set @categoryId = _categoryId;  
  10.    set @startRow = _pageIndex * _pageSize;  
  11.    set @pageSize = _pageSize;  
  12.    
  13.    prepare PageSql from 'select sql_calc_found_rows * from product  where categoryId = ? order by ProductId desc limit ?, ?';  
  14.    execute PageSql using @categoryId, @startRow, @pageSize;  
  15.    deallocate prepare PageSql;  
  16.    set _totalRecCount = found_rows();  
  17.    
  18. end 

MySQL與SQL Server的差別實(shí)在太多,以上只是列出了本人認(rèn)為經(jīng)常在寫(xiě)存儲(chǔ)過(guò)程中會(huì)遇到的一些具體的差別之處。

 原文鏈接:http://www.cnblogs.com/fish-li/archive/2011/04/05/2006107.html


本文名稱(chēng):MySQLVSSQLServer之用法差別
URL地址:http://www.5511xx.com/article/dpgdjgj.html