百度百科
關係數據庫,是創建在關係模型基礎上的數據庫,藉助於集合代數等數學概念和方法來處理數據庫中的數據。現實世界中的各類實體以及實體之間的各類聯繫均用關係模型來表示。標準數據查詢語言SQL就是一種基於關係數據庫的語言,這種語言執行對關係數據庫中數據的檢索和操做。 關係模型由關係數據結構、關係操做集合、關係完整性約束三部分組成。
簡單說,關係型數據庫是由多張能互相聯接的二維行列表格組成的數據庫。sql
當前主流的關係型數據庫有MySQL、Oracle、 SQL Server、Microsoft Access等。數據庫
with tt as (select * from emp) select select * from from tt ;
select rownum, e.* from e where rownum < 11; -- 顯示10條結果
select stuName as 姓名,stuAge as 年齡,stuID as 身份號 from stuInfo; select stuName 姓名,stuAge 年齡,stuID 身份號 from stuInfo;
alter table monthly_indicator rename to mi;
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存儲過程markdown
SQL Server使用T-SQL語言,是SQL程序設計語言的加強版數據結構
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
declare @x int set @x = 1 printout: -- 設定標記 print @x select @x = @x + 1 while @x < 5 goto printout -- 流程轉向標記
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