Oracle,SQL Server 數據庫較MySql數據庫,Sql語句差別

原文: Oracle,SQL Server 數據庫較MySql數據庫,Sql語句差別

Oracle,SQL Server 數據庫較MySql數據庫,Sql語句差別

1.關係型數據庫

百度百科
關係數據庫,是創建在關係模型基礎上的數據庫,藉助於集合代數等數學概念和方法來處理數據庫中的數據。現實世界中的各類實體以及實體之間的各類聯繫均用關係模型來表示。標準數據查詢語言SQL就是一種基於關係數據庫的語言,這種語言執行對關係數據庫中數據的檢索和操做。 關係模型由關係數據結構、關係操做集合、關係完整性約束三部分組成。
簡單說,關係型數據庫是由多張能互相聯接的二維行列表格組成的數據庫。sql

當前主流的關係型數據庫有MySQL、Oracle、 SQL Server、Microsoft Access等。數據庫

2.Oracle 操做語句與MySql的差別

  • with…as: Oracle數據庫子查詢不能直接使用as 命名,須要先使用with…as…語句命名,後查詢。
with tt as (select * from emp)
select select * from from tt ;
  • Oracle數據庫沒有limit函數,限制查詢結果行數,採用僞列rownum進行。
select rownum, e.* from e where rownum < 11;                            -- 顯示10條結果
  • Oracle數據庫對字段,表進行別名時,能夠省略 as:
select stuName as 姓名,stuAge as 年齡,stuID as 身份號 from stuInfo;
select stuName 姓名,stuAge 年齡,stuID  身份號 from stuInfo;
  • 表名修改 (rename ------- rename to):
alter table monthly_indicator rename to mi;
  • 針對字段進行修改時,字段名前須要加 column:
alter table mi rename column avgaqi to averageaqi;
alter table mi drop column averageaqi;
  • 虛表的使用:
select 1+1 from dual;                                                #並非真的須要去表裏查詢,僅用於語法補位。
select initcap('hello world') from dual;                             #initcap 首字母變成大寫。
select instr('hello world','w') from dual;                           #instr 查詢字符串出現的位置。
select concat(lower('HELLO'), upper('world')) from dual;             #concat 函數,鏈接字符串。
select replace('hello earth','earth','world') from dual;             #replace 函數,替換字符串。
select substr('hello world',3,5) from dual;                          #substr 函數,提取字符串中起始位置(3),提取長度(5)。
select last_day(sysdate) from dual;                                  #last_day 函數,當前月份最後一天的日期。
select to_date('2019-01-01','YYYY-MM-DD') from dual;                 #to_date 函數,將文本轉換成正確的日期型日期。

Oracle 存儲過程

Oracle數據庫存儲過程差別較大,過程詳細轉載:Oracle存儲過程markdown

3.SQL Server 操做語句與MySql的差別

SQL Server使用T-SQL語言,是SQL程序設計語言的加強版數據結構

  • 局部變量聲明,MySql中@爲用戶變量;SQL Server中語句結束不須要使用分號;確認
declare @myparam int             												--聲明局部變量
select @myparam = 1              												--爲局部變量賦值
print @myparam                   												--查看局部變量
  • 查詢結果增長備註列
select *, 備註 = case
when f_price < 10 then 
	'便宜'
when f_price >= 10 and f_price <= 20 then 
	'普通'
when f_price > 20 then 
	'貴'
else '-'
end
from fruits
  • 標記,flag_name: + goto flag_name 組成。
declare @x int
set @x = 1
printout:                       											 -- 設定標記
print @x
select @x = @x + 1
while @x < 5
	goto printout                 											-- 流程轉向標記
  • if,while等判斷,循環函數涵蓋範圍由begin…end肯定,MySql中則由end if; end while;做爲結束標識。
declare @m int
set @m = 10
while @m > 0
begin
  set @m = CAST(@m as varchar(4))
  exec('alter table fruits drop column c'+ @m)
  set @m = CAST(@m as int)
  set @m = @m -1
end
  • 儲存過程
create proc Proc_OutPutTest                          				--建立
@numA int,                                            				--numA爲存儲過程的參數
@numB int,                                            				--numB爲另外一參數
@numReturn int output                                 				--此爲Output,也就是存儲過程的返回值
as
begin
	if(@numA>@numB)
	    set @numReturn=@numA
	else
	    set @numReturn=@numB                              		   --A>B的時候返回A,不然返回B
end                                                  			   --begin...end肯定存儲過程包含語句範圍。 

declare @numReceive int 										   --先聲明一個變量用來接收存儲過程的返回值
exec Proc_OutPutTest 1,2, @numReceive output
 															       --調用存儲過程並用@numReturn接收存儲過程的返回值
select @numReceive                                                 --將會返回(1,2)中較大的數字:2
相關文章
相關標籤/搜索