SQL Server和My SQL某些T-SQL用法的区别

1、设置初始流水号,比如设置某一列的各个依次为1、2、3、4、5...... ;在SS中是IDENTITY(1, 1),而在MS写法是auto_increment

2、修改表中列的数据类型,在SS中是ALTER TABLE students01 ALTER COLUMN Id SMALLINT,而在MS中写法是ALTER TABLE students01 MODIFY Id SMALLINT

.......

因为之前一直在用SQL Server,所以一时间适应不过来。 还有好多细节在写法上有各种各样的区别,先记录一下自己碰到的,以后再补上去

=====================================================================

转载一下网上的资料 Link:http://www.jb51.net/article/54346.htm

这篇文章主要介绍了MySQL与SQL Server的一些区别浅析,本文罗列了MySQL与SQL Server的25个不同的地方,并对它们的分页技术的不同做了讲解,需要的朋友可以参考下

1、mysql支持enum,和set类型,sql server不支持
2、mysql不支持nchar,nvarchar,ntext类型
3、mysql的递增语句是AUTO_INCREMENT,而mssql是identity(1,1)
4、mssql默认到处表创建语句的默认值表示是((0)),而在mysql里面是不允许带两括号的
5、mysql需要为表指定存储类型
6、mssql识别符是[],[type]表示他区别于关键字,但是mysql却是 `,也就是按键1左边的那个符号
7、mssql支持getdate()方法获取当前时间日期,但是mysql里面可以分日期类型和时间类型,获取当前日期是cur_date(),当前完整时间是 now()函数
8、mssql不支持replace into 语句,但是在最新的sql20008里面,也支持merge语法
9、mysql支持insert into table1 set t1 = ‘', t2 = ‘' ,但是mssql不支持这样写
10、mysql支持insert into tabl1 values (1,1), (1,1), (1,1), (1,1), (1,1), (1,1), (1,1)
11 mssql不支持limit语句,是非常遗憾的,只能用top 取代limt 0,N,row_number() over()函数取代limit N,M
12、mysql在创建表时要为每个表指定一个存储引擎类型,而mssql只支持一种存储引擎
13、mysql不支持默认值为当前时间的datetime类型(mssql很容易做到),在mysql里面是用timestamp类型
14、mssql里面检查是否有这个表再删除,需要这样:if exists (select * from dbo.sysobjects where id=id (N'uc_newpm') and OBJECTPROPERTY(id, N'IsUserTable') = 1) 但是在mysql里面只需要 DROP TABLE IF EXISTS cdb_forums;
15、mysql支持无符号型的整数,那么比不支持无符号型的mssql就能多出一倍的最大数存储
16、mysql不支持在mssql里面使用非常方便的varchar(max)类型,这个类型在mssql里面既可做一般数据存储,也可以做blob数据存储
17、mysql创建非聚集索引只需要在创建表的时候指定为key就行,比如:KEY displayorder (fid,displayorder) 在mssql里面必须要:

复制代码 代码如下:
create unique nonclustered index index_uc_protectedmembers_username_appid on dbo.uc_protectedmembers (username asc,appid asc)

18、mysql text字段类型不允许有默认值
19、mysql的一个表的总共字段长度不超过65XXX。
20、一个很表面的区别就是mysql的安装特别简单,而且文件大小才23M左右(5.5.23),相比微软这个庞然大物,安装进度来说简直就是.....
21、mysql的管理工具有几个比较好的,mysql_front,和官方那个套件,不过都没有SSMS的使用方便,这是mysql很大的一个缺点。
22、mysql的存储过程只是出现在最新的版本中,稳定性和性能可能不如mssql。
23、同样的负载压力,mysql要消耗更少的CPU和内存,mssql的确是很耗资源。
24、php连接mysql和mssql的方式都差不多,只需要将函数的mysql替换成mssql即可,如果是PDO方式只需要把mysql替换mssql即可。
25、mysql支持date,time,year类型,mssql到2008才支持date和time。

附:MySQL与MSSQL分页的区别

之前一直用MySQL,虽然比起mssql这个庞大的数据库系统mysql很苗条,但它并不逊色。以下说说这两个在数据库各自的分页区别
例1,取出前十条

复制代码 代码如下:

SELECT * FROM table LIMIT 10;

在mssql中
复制代码 代码如下:

SELECT TOP 10 * FROM table

例2,每页十条,取出第三页
在MySQL中
复制代码 代码如下:

SELECT * FROM table LIMIT 20,10

在mssql中
复制代码 代码如下:

SELECT TOP 10 * FROM table WHERE id NOT IN(
SELECT TOP 20 id FROM table ORDER BY id DESC
) ORDER BY id DESC;

由以上例子可以看出,在MySQL中分页用LIMIT关键字,如果是LIMIT 10表示取前十条,如果是LIMIT 10,10表示偏移十条取前十条记录。在mssql中用top关键字,如果只取前n条记录直接top n即可,但是要是分页取就有点麻烦。
==================================================================================
Link:http://www.cnblogs.com/fish-li/archive/2011/04/05/2006107.html

由于工作的原因:上家公司的数据库全采用MySql,所以不得不用它。因此也学到了MySql的一些知识,但考虑到今后可能没机会使用了,所以想趁现在离职在家休息,打算把这些东西整理一下,也为了万一今后能用上,留个参考的资源。考虑到一直在使用SqlServer,所以就打算直接与SqlServer对比来写。

本文将主要列出MySql与SqlServer不同的地方,且以常用的存储过程的相关内容为主。

1. 标识符限定符

SqlServer []
MySql ``

2. 字符串相加

SqlServer 直接用 +
MySql concat()

3. isnull()

SqlServer isnull()
MySql ifnull()
注意:MySql也有isnull()函数,但意义不一样

4. getdate()

SqlServer getdate()
MySql now()

5. newid()

SqlServer newid()
MySql uuid()

6. @@ROWCOUNT

SqlServer @@ROWCOUNT
MySql row_count()
注意:MySql的这个函数仅对于update, insert, delete有效

7. SCOPE_IDENTITY()

SqlServer SCOPE_IDENTITY()
MySql last_insert_id()

8. if ... else ...

SqlServer
IF Boolean_expression 
     { sql_statement | statement_block } 
[ ELSE 
     { sql_statement | statement_block } ] 
 
-- 若要定义语句块,请使用控制流关键字 BEGIN 和 END。
MySql
IF search_condition THEN statement_list
    [ELSEIF search_condition THEN statement_list] ...
    [ELSE statement_list]
END IF

注意:对于MySql来说,then, end if是必须的。类似的还有其它的流程控制语句,这里就不一一列出。

9. declare

其实,SqlServer和MySql都有这个语句,用于定义变量,但差别在于:在MySql中,DECLARE仅被用在BEGIN ... END复合语句里,并且必须在复合语句的开头,在任何其它语句之前。这个要求在写游标时,会感觉很BT.

10. 游标的写法

SqlServer
declare @tempShoppingCart table (ProductId int, Quantity int)
insert into @tempShoppingCart (ProductId, Quantity)
	select ProductId, Quantity from ShoppingCart where UserGuid = @UserGuid


declare @productId int
declare @quantity int
declare tempCartCursor cursor for 
		select ProductId, Quantity from @tempShoppingCart

open tempCartCursor
fetch next from tempCartCursor into @productId, @quantity
while  @@FETCH_STATUS = 0
begin
	update Product set SellCount = SellCount + @quantity	where productId = @productId

	fetch next from tempCartCursor into @productId, @quantity
end

close tempCartCursor
deallocate tempCartCursor
MySql
declare m_done int default 0;
declare m_sectionId int;
declare m_newsId int;

declare _cursor_SN cursor for select sectionid, newsid from _temp_SN;
declare continue handler for not found set m_done = 1;

create temporary table _temp_SN 
	select sectionid, newsid from SectionNews  group by sectionid, newsid having count(*) > 1;

open _cursor_SN;
while( m_done = 0 ) do
	fetch _cursor_SN into m_sectionId, m_newsId;
	
	if( m_done = 0 ) then 
		-- 具体的处理逻辑
	end if;
end while;
close _cursor_SN;
drop table _temp_SN;

注意:为了提高性能,通常在表变量上打开游标,不要直接在数据表上打开游标。

11. 分页的处理

SqlServer
create procedure GetProductByCategoryId( 
    @CategoryID int, 
    @PageIndex int = 0, 
    @PageSize int = 20, 
    @TotalRecords int output
) 
as
begin
     
declare @ResultTable table
( 
    RowIndex int, 
    ProductID int, 
    ProductName nvarchar(50), 
    CategoryID int, 
    Unit nvarchar(10), 
    UnitPrice money, 
    Quantity int
); 
     
insert into @ResultTable 
select row_number() over (order by ProductID asc) as RowIndex, 
       p.ProductID, p.ProductName, p.CategoryID, p.Unit, p.UnitPrice, p.Quantity 
from   Products as p 
where CategoryID = @CategoryID; 
       
select  @TotalRecords = count(*) from  @ResultTable; 
     
select * 
from   @ResultTable 
where  RowIndex > (@PageSize * @PageIndex) and RowIndex <= (@PageSize * (@PageIndex+1)); 
     
end;

当然,SqlServer中并不只有这一种写法,只是这种写法是比较常见而已。

MySql
create procedure GetProductsByCategoryId(
   in _categoryId int,
   in _pageIndex int,
   in _pageSize int,
   out _totalRecCount int
)
begin
 
   set @categoryId = _categoryId;
   set @startRow = _pageIndex * _pageSize;
   set @pageSize = _pageSize;
 
   prepare PageSql from 
	'select sql_calc_found_rows * from product  where categoryId = ? order by ProductId desc limit ?, ?';
   execute PageSql using @categoryId, @startRow, @pageSize;
   deallocate prepare PageSql;
   set _totalRecCount = found_rows();
 
end


posted @ 2015-08-03 17:54  awenzero  阅读(361)  评论(0编辑  收藏  举报