Oracle,SQL Server 数据库较MySql数据库,Sql语句差异

Oracle,SQL Server 数据库较MySql数据库,Sql语句差异

1.关系型数据库

百度百科
关系数据库,是建立在关系模型基础上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据。现实世界中的各种实体以及实体之间的各种联系均用关系模型来表示。标准数据查询语言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存储过程

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