目錄
子句 |
描述 |
是否必需 |
select |
返回的列或表達式 |
是 |
from |
要檢索的表 |
在oracle是,其他不一定 |
where |
行過濾 |
否 |
group by |
分組規範 |
僅當分組計算聚會值時是必需的 |
having |
分組過濾 |
否 |
order by |
排序,desc,asc |
否 |
篩選 distinct;
連接字段 ||
>select [列名1]||[列名2] from [表1] >列名1列名2
>select [列名1]||',('||[列名2]||')'' from [表1] >列名1,(列名2)
數字計算 + - * /
函數
(參考 Oracle學習筆記——常用函數總結)
文本函數 Length() Lower() Upper() LPad() LTrim() Soundex() SubString()
數值函數 Abs Cos Exp Mod Sin Sqrt Tan
日期函數 Add_Month() Last_Day() Months_Between() Next_day() Sysdate() ; To_Date(sysdate,'YYYY-MM-DD HH24:mi:ss');Extact( ? from sysdate) ?=day|month|year
系統函數
聚合函數 AVG() COUNT() MAX() MIN() SUM()
別名 as
過濾條件:
where [列名1] between [範圍開頭] and [範圍結尾] --值範圍
where [列名1] <> [值]; -- 不等於
where [列名1] != [值]; --不等於
where [列名1] IS NULL; --沒有值
where [列名] IN ( [有效值1],[有效值2]) --IN運算符,清晰,效率高,推薦使用;()可以包含另一個select語句;
where [列名] NOT IN ( [有效值1],[有效值2])
where [列名] LIKE 'jet%' --LIKE 模糊匹配,%通配符,%匹配n個字符
where [列名] LIKE 'jet_' --LIKE 模糊匹配,_通配符,%匹配單個字符
where [條件1] AND [條件2]; --多重條件,且,同時
where [條件1] OR [條件2]; --多重條件,或,任一條
where ([條件1] OR [條件2]) AND [條件3]; --多重條件,顯性組合();隱形默認處理順序爲and 優於 or;
where regexp_like() --返回滿足條件的字段
……regexp_instr() --返回滿足條件的字符或字符串的位置
……regexp_replace() --返回替換後的字符串
……regexp_substr() --返回滿足條件的字符或字符串
/*正則表達式*/
--模糊匹配字符值;.是正則表達式的_運算符,意味匹配任意單個字符
WHERE REGEXP_LIKE( [列名], '值')
input> WHERE REGEXP_LIKE( v_id, '.000') ;
output> 所有含?000 的列,包括1000/2000/;
--匹配多個字符值;|是正則表達式的or運算符
WHERE REGEXP_LIKE( [列名], '值1|值2|值n')
--匹配特殊單一字符;[]內定義一組字符,[123]表示匹配 1ton或2ton或3ton;
WHERE REGEXP_LIKE( [列名], '[值1|值2|值n]ton')
可以寫作WHERE REGEXP_LIKE( [列名], '[1|2|3]ton');更容易理解
--匹配特殊單一字符;^表示取反,[^123]定義一組字符,表示匹配除 1ton或2ton或3ton以爲的其他字符,並且返回他們;
WHERE REGEXP_LIKE( [列名], '[^值1|值2|值n]ton')
--匹配範圍字符;-定義一個範圍
WHERE REGEXP_LIKE( [列名], '[值1-值n]')
特殊字符,用\作前綴;即轉義字符,"\." 即.;"\_" 即_
匹配字符類別;匹配多個實例; 參考:Oracle 正則表達式 - Oracle - 博客園
--根據[列1]分組統計[列2]的總數,也可以用其他聚會函數
select [列1],COUNT([列2]) from [表1] GROUP BY [列1];
--根據[列1]分組統計[列2]的總數,也可以用其他聚會函數
select [列1],COUNT([列2]) from [表1]
GROUP BY [列1]
HAVING COUNT([列2]) >2;
desc;asc
select [l1]
from [b1]
where [l2] in (select [l2] from [b2] where [l3]=值);
--等於聯表查詢,查詢符合表2中指定列3對應的列2名稱,在表1中查詢列2名稱對應的列1名稱;
--可以多重嵌套,最多255個層級,但性能會退化;
select [l1] ,(select count([l2]) from [b2] where [l3]=值) as 別名
from [b1];
union;union all
--組合SQL查詢語句,把他們的結果組合成單個結果集。
select 語句1
union
select 語句2;
--union會自動刪除查詢結果集中任何重複的行。union all,返回所有行。
select 語句1
union all
select 語句2;
外鍵(Foreign)
一個表中的一列,其中包含另一個表中的主鍵(Primary Key),因此定義了兩個表之間的關係(relation)
表連接方式
等值連接,from A,B where 條件
內連接,from A inner join B on 條件
外連接,from A right/left outer join B on 條件
where做連接條件;
select 列名1,列名2,列名3
from 表名1,表名2
where 表名1.列名1 等於表名2.列名1
內連接,即等值連接
select 列名1,列名2,列名3
from 表名1
inner join 表名2
on 表名1.列名1 等於表名2.列名1;
外連接
select 列名1,列名2,列名3
from 表名1
right/left outer join 表名2
on 表名1.列名1 等於表名2.列名1;
視圖,是虛擬表,包含查詢(select語句);可以複用,但要考慮性能
create view
create view 視圖名 as
select 語句;
之後可以多次引用虛擬表(視圖名),就當做是一個表來處理。
更新update
update 表名1 set 列名2=‘新值’where 列名1=‘指定行';
刪除delect
delect from 表名1 where 列名1=‘指定行';
插入Insert
1.直接插入
insert into 表名(列名1,列名2,列名n)
values(值1,值2,值n);
2.從表導入
insert select
insert into 表名(列名1,列名2,列名n)
select 列名1,列名2,列名n from 表名2; --select 語句也可以加入過濾條件
創建新表
create tables 表名
(列名 字符類型 not null,
列名 字符類型 null, --允許空值
列名 字符類型 default 1 not null) --空值時,默認1
定義主鍵
alter table 表名 add constraint pk_表名 Primary Key(列名);
定義外鍵
alter table 表名1 add constraint fk_表名1_表名2 Foreign Key(列名) references 表名2(列名);
更新表
update tables 表名
add 列名 字符類型;
刪除表
drop table 表名;
重命名錶
alter table 新表名新 rename to 表名;
查詢
select * from dba_users; create user 用戶賬戶 identified by 「密碼」; drop user 用戶賬戶
修改
alter user 用戶賬戶 indentified by 「新密碼」replace「舊密碼」
查詢
select privilege from sys.dba_sys_privs --存儲用戶權限的表 where grantee='用戶賬戶'
授權
grant select on 表名 to 用戶賬戶 grant select,insert,update on 表名 to 用戶賬戶
撤銷
revoke……
創建
create or replace procedure 存儲過程名 is\as 可選的變量聲明 begin 【過程的主體】 end;
執行
execute 存儲過程1;
刪除
drop procedure 存儲過程1;
declare
--declare variables定義變量名
v_vend_id vendors.vend_id%type;
v_vend_name vendors.vend_name%type;
--declare cursor定義遊標名
cursor c_vendors is
select vend_id,vend_name
from vendors;
begin
open cursor
--loop through cursor遍巡遊標,依次把把遊標中的記錄放入指定變量中,知道when條件滿足。
loop
fetch c_vendors into v_vend_id,v_vend_name;
exit when c_vendors%notfound;
end loop;
close c_vendors;
end;
declare
變量名 表名.列名%type;
cursor 遊標名 is
select 語句;
begin
open cursor
loop
【循環操作】
exit when 退出條件
end loop
close c_vendors;
定義: 遊標cursor是一個存儲在oracle服務器上的數據庫查詢,是由select語句檢索的結果集
操作
declare cursor 遊標名 is select 語句;
open 遊標名; close 遊標名;
定義:用來跟蹤表的更改;做記錄
操作
create or replace trigger 觸發器名 after 操作 on 表名 begin 觸發動作 end;
drop trigger 觸發器名;
insert觸發器--用來跟蹤表的更改;
create or replace trigger order_after_insert
after insert on orders
for each row
begin
insert into orders_log(change_on,change_type,order_num)
values(sysdate,'A',NEW.order_num)
end;
create or replace trigger 觸發器名稱
after insert on 表名 | before delect on 表名|before update on 表名
for each row
begin
insert into orders_log(change_on,change_type,order_num)
values(sysdate,'A',NEW.order_num)
end;
定義: 事務處理transaction processing是一種機制,用於管理必須成批執行的SQL操作集。
操作
savepoint 保存點名;
rollback
rollback to savepoint 保存點名;
Commit
end;