Mysql与sql server的区别

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 由于工作的原因:上家公司的数据库全采用MySql,所以不得不用它。因此也学到了MySql的一些知识,但考虑到今后可能没机会使用了,所以想趁 现在离职在家休息,打算把这些东西整理一下,也为了万一今后能用上,留个参考的资源。
+关注继续查看

由于工作的原因:上家公司的数据库全采用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

MySql与SqlServer的差别实在太多,以上只是列出了我认为经常在写存储过程中会遇到的一些具体的差别之处。

去年我将一些MySql的常用函数作了一番整理,

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
8月前
|
SQL 网络协议 安全
SQLServer 远程链接MySql数据库详解
SQLServer 远程链接MySql数据库详解
339 0
|
11月前
|
存储 Oracle 关系型数据库
MySQL数据库: 添加列、修改列、删除列、修改列属性、修改表名(包括MySQL、SQLServer、Oracle)
MySQL数据库: 添加列、修改列、删除列、修改列属性、修改表名(包括MySQL、SQLServer、Oracle)
361 0
MySQL数据库: 添加列、修改列、删除列、修改列属性、修改表名(包括MySQL、SQLServer、Oracle)
|
12月前
|
存储 SQL 关系型数据库
C# 中的数据库操作~存储过程篇Mysql SqlServer
C# 中的数据库操作~存储过程篇Mysql SqlServer
|
关系型数据库 MySQL
mysql分页原理和sqlserver里面序列的用法
mysql分页原理和sqlserver里面序列的用法
127 0
|
SQL Oracle 关系型数据库
Oracle、DB2、SQLSERVER、Mysql、Access分页SQL语句
最近把平时在项目中常用到的数据库分页sql总结了下。大家可以贴出分页更高效的sql语句。
138 0
|
SQL 关系型数据库 MySQL
.NET5发布了,腾讯招聘点名要求精通MySQL,而不是SQLServer
.NET5发布了,腾讯招聘点名要求精通MySQL,而不是SQLServer
145 0
.NET5发布了,腾讯招聘点名要求精通MySQL,而不是SQLServer
|
SQL Oracle 关系型数据库
Mybatis中oracle、mysql、db2、sql server的like模糊查询
Mybatis中oracle、mysql、db2、sql server的like模糊查询
346 0
|
存储 关系型数据库 数据库
mysql,sqlserver数据库单表数据过大的处理方式
经常混迹于技术社区,频繁看到这个题目,今天干脆在自己博客重复一遍解决办法: 针对mysql,sqlserver等关系型数据库单表数据过大的处理方式 如果不是阿里云的分布式数据库 DRDS 那种多机器集群方案的话: 先考虑表分区 ;然后考虑分表 ;然后考虑分库。
2417 0
|
关系型数据库 MySQL 数据库
使用SyncNavigator进行SqlServer或者MYsql数据库的实时、增量同步
SyncNavigator是一款高效的数据库同步工具,支持sqlserver数据库和mysql数据库,采用增量同步的方式实时保存数据库数据。
1807 0
|
安全 关系型数据库 MySQL
mysql到sqlserver数据库实时同步工具syncnavigator注册使用
syncnavigator数据库同步工具注册使用教程,授权后实时同步mysql、sqlserver数据库,增量传输.
3867 0
推荐文章
更多