由於工作的原因,上家公司一直使用的oracle,目前加入acxiom后接觸的幾個項目,既有使用mysql的又有使用sqlserver,自己在使用sqlserver及mysql要實現某功能時,經常要在網上找來找去,所以打算大概整理下這3個數據庫在平時取數的一些不同點。
Sqlserver、mysql、oracle三者是目前市場占有率最高的關系型數據庫,網上總結的這3個數據庫總體的特點如下:
一、 Oracle:最貴,功能最多,安裝最不方便,Oracle環境里的其他相關組件最多,支持平台數據量一般,使用中等方便,開發中等方便,不開源,速度最慢、最安全。
二、 Microsoft SQL Server:中等貴,功能最少,安裝中等方便,sqlserver環境里的其他相關組件最少,支持平台最少,使用最方便,開發最方便,運維最方便,不開源,速度中等,一般安全
三、 Mysql:免費,功能中等,安裝最方便,mysql環境里的其他相關組件數量中等,支持平台最多,使用最不方便,開發最不方便,運維最不方便,有開源版本,速度最快,最不安全。
本文檔主要列出Sqlserver、mysql、oracle不同的地方,且以常用的日常的sql取數及存儲過程的相關內容為主。
1、字符串拼接
Sqlserver |
+ |
Mysql |
concat() |
Oracle |
|| |
2、空值處理數
Sqlserver |
isnull() |
Mysql |
ifnull() 注意:mysql也有isnull()函數,但意義不一樣 select a.mobile,isnull(a.mobile) ,ifnull(a. mobile,'空') from ud_connect_new a; |
Oracle |
Nvl() |
3、獲取系統時間
Sqlserver |
getdate() |
Mysql |
now() |
Oracle |
sysdate |
4、日期格式化(以常用的yyyymmdd格式為例)
Sqlserver |
convert(varchar(8),getdate(),112) |
Mysql |
date_format(xcs_received_date,'%Y%m%d') |
Oracle |
to_char(sysdate,'yyyymmdd') |
5、檢查是否有表再刪除
Sqlserver |
IF OBJECT_ID('xxx') IS NOT NULL 需要用到系統表(dbo.sysobjects )來判斷 |
Mysql |
drop table if exists tablename |
Oracle |
select count(1) from user_tables where table_name = 'xxx' |
6、日期增加一個時間間隔
Sqlserver |
SELECT DATEADD(month, -1, getdate()) |
Mysql |
select date_sub(now(),interval 1 month) |
Oracle |
select add_months(sysdate,1) from dual;
|
7、查詢部分記錄
Sqlserver |
top關鍵字 |
Mysql |
limit |
Oracle |
不支持mysql中limit功能,但可以通過rownum來限制返回的結果集的行數 |
8、Rollup()
Sqlserver |
group by with rollup(xx)
|
Mysql |
group by with rollup(xx) --mysql這邊不能帶order by 語句 |
Oracle |
group by rollup(xx) --區別於sqlserver及mysql沒有with |
9、定義變量
Sqlserver |
Begin
DECLARE @count int SET @count=123 Select @count
end
|
Mysql |
set @num1=(select max(rank) From tmp_ud_test where is_member=1)/3; set @num2=(select max(rank) From tmp_ud_test where is_member=1)/3*2; set @num3=(select max(rank) From tmp_ud_test where is_member=1)/3*3;
update tmp_ud_test a set type= case when a.rank<=@num1 then 1 when @num1<a.rank and a.rank<=@num2 then 2 when @num2<a.rank and a.rank<=@num3 then 3 end where a.is_member=1; |
Oracle |
declare count number := 20; currtime date := sysdate;
begin update xxx set aa= count ,bb= currtime; end;
|
10、 rownum為列
Sqlserver |
可直接當作字段使用,自動生成序列 |
Mysql |
select a.* ,@rownum:=@rownum+1 as rownum From UD_DAILY_REPORT a,(select @rownum:=0) b; |
Oracle |
同sqlserver |
11、 如何實現取每組的前幾名(場景:查詢出每個班級成績最高的前三名學生,用到的表名及字段如下:)
Table:tmp_class_score
班級:class_id
成績:score
Sqlserver |
Select From (select a.*,row_number() over(partition by class_id order by score) rank from tmp_class_score a) where rank<=3 |
Mysql |
select * from ( select b.*,@rownum:=@rownum+1 , if(@pdept=b.class_id,@rank:=@rank+1,@rank:=1) as rank, @pdept:=b.class_id from ( select * from tmp_class_score order by class_id, score desc ) b ,(select @rownum :=0 , @pdept := null ,@rank:=0) c ) result where rank<=3; |
Oracle |
Select From (select a.*,row_number() over(partition by class_id order by score) rank from tmp_class_score a) where rank<=3;
|
Sqlserver 及Mysql還有rank () OVER 用替換row_number()over 實現並列排序,就是如果一個班級最高的成績有2個人那用這個函數,取出來的排序,這2個同學顯示序號都是1,這2個同學下面的同學顯示的序號就會跳過2直接為3。
12、if ... else ...
Sqlserver |
IF @count>0 Set @num=36 [ ELSE set @num=30
|
Mysql |
IF search_condition THEN statement_list [ELSEIF search_condition THEN statement_list] [ELSE statement_list] END IF ;
|
Oracle |
同mysql
|
注意:對於mysql及oracle來說,then,end if是必須的。
13、主鍵自增長
Sqlserver |
create table tb(id int identity(1,1) primary key ) |
Mysql |
create table tb(id int auto increment primary key ) |
Oracle |
ORACLE沒有自動增長的數據類型,需要建立一個自動增長的序列號,插入記錄時要把序列號的下一個值賦於此字段。 例:使用序列自動增長: |
14、大體上講,Oracle的數字類型更加簡單,大部分情況直接設置number類型就行。而不需要像mysql及sqlserver設置個種數值類型
15、Oracle對子查詢的支持非常好。而Mysql中的子查詢效率就非常低
16、提交方式
Sqlserver |
默認是自動提交 |
Mysql |
mysql默認是自動提交 |
Oracle |
默認不自動提交,需要用戶手動提交。Sql腳本中經常用到commit; |
17、MySQL支持insert into tabl1 values (1,1), (1,1), (1,1), (1,1), (1,1), (1,1), (1,1)。而sqlserver及oracle則不能這么插入,可用union all實現
18、oracle中一些常用的一些函數非常使用使用簡單如decode(xx,1,’是’,2,’否’,3’未知’),同case when xx=1 then’是’ when xx=2 then ‘否’when xx=3 then’未知’ end。 其他還有to_char()、add_month、last_day()都是常用且容易理解簡單的函數。
當然,sqlserver、mysql及oracle差別實在太多,我這邊列的僅是平時可能經常遇到的一些區別,肯定還有一些遺漏或不准確的地方。