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
目录
相关文章
|
20天前
|
存储 关系型数据库 MySQL
MySQL存储引擎MyISAM与InnoDB两者之前的区别
MySQL存储引擎MyISAM与InnoDB两者之前的区别
25 0
|
1月前
|
关系型数据库 MySQL Unix
【MySQL用法】MySQL 中 datetime 和 timestamp 的区别与选择
【MySQL用法】MySQL 中 datetime 和 timestamp 的区别与选择
27 0
|
1月前
|
SQL 关系型数据库 MySQL
【MySQL系列】ALTER语句详解,以及UPDATE,DELECT,TRUNCATE语句的使用+区别
上一篇博客讲解了字符集,数据类型,还有简单的数据库的创建与删除。本篇博客将讲解MySQL的修改更新删除等操作 文章目录
|
1月前
|
关系型数据库 MySQL Java
Java 最常见的面试题:mysql 的内连接、左连接、右连接有什么区别?
Java 最常见的面试题:mysql 的内连接、左连接、右连接有什么区别?
|
2月前
|
存储 缓存 关系型数据库
2.3 【MySQL】命令行和配置文件中启动选项的区别
2.3 【MySQL】命令行和配置文件中启动选项的区别
16 0
|
2月前
|
存储 关系型数据库 MySQL
【必看】MySQL中float、double、decimal三个浮点数据类型的区别与总结!
你还不知道MySQL中float、double、decimal三个浮点类型的区别吗?快来看看吧!
31 0
【必看】MySQL中float、double、decimal三个浮点数据类型的区别与总结!
|
2月前
|
存储 关系型数据库 MySQL
Mysql索引类型区别和使用场合
Mysql索引类型区别和使用场合
25 0
|
2月前
|
SQL Oracle 关系型数据库
MySQL入门和各种数据库的区别
MySQL入门和各种数据库的区别
40 0
|
2月前
|
存储 关系型数据库 MySQL
MySQL存储引擎MyISAM和InnoDB的区别
你都工作3年了,怎么还不知道MyISAM和InnoDB有什么区别?一位粉丝被面试官这样一个问题。当时,这位粉丝直接回复“不知道”,这位粉丝自己也怪不好意思的。
57 0
|
3月前
|
关系型数据库 MySQL
关于MySQL中的LEFT JOIN和LEFT OUTER JOIN的区别
LEFT JOIN是LEFT OUTER JOIN的简写版;
推荐文章
更多