oracle sql 語句
1 檢索日期
?1 select birthday from ...使用的是日期的默認格式
?2 使用YYYY-MM_DD 格式 select to_char(birthday,'YYYY-MM-DD') from ..
2 處理null值
?1 使用nvl函數處理null值:nvl 函數用于將null 轉變為實際值,其語法格式為nvl(exp1,exp2),如果exp1是null 則反會exp2,否則返回exp1
? select nvl(comm,0) as salary from
?2 使用nvl2 處理null :nvl2(exp1,exp2,exp3),如果exp1 是null 返回exp3,否則返回exp3,exp2 ,和exp3 不可以是long,并需要和exp1匹配
3 連接字符:select eanme||'is a '|| job as "employee detail" form emp
4 在where 中使用 日期值
? select * from hiredate>to_date('1982-01-01','YYYY-MM-DD')
5 在where 子句中使用like
? select * from ename like 'S%'? select * from ename like '__A%'? select * from ename like '%a_%' 字符a為轉義符
6 插入數據
?insert into emp(empno ,ename,job,hiredate)values(1234,'mary','clerk',to_datee('1983-02-02','YYYY-MM-DD'))
?insert into dept values(50,'train','boston')
7 使用子查詢插入數據
?? 1 使用子查詢
?? insert into employee (empno,ename,sal,deptno,form emp where deptno=20);
?? 2 使用查詢執行直接轉載
?? insert /*+append*/ into employee (empno,ename,sal,deptno)
?? select ..............(大批量數據直接轉載時速度更快一些)
8使用多表插入數據
? 1 使用all 操作符執行奪標插入
?? insert all
?? when deptno=10 then into dept10
?? when deptno=20 then into dept20
?? when job='clerk' then into clerk
?? else into other
?? select * from emp;
? 2 使用first 操作符執行多表插入L:如果數據已經滿足先前條件,并且已經被插入到某表,那么
?? 該行數據在后續插入中將不會被再次使用。即不會出現既插入到dept10 中又插入到 clerk 中的
?? 情況
?? insert first
?? when deptno=10 then into dept10
?? when deptno=20 then into dept20
?? when job='clerk' then into clerk
?? else into other
?? select * from emp
9 更新數據
?? 1 update emp set job default where cname='scott'? 如果存在默認使用默認,否則使用null
?? 2 使用子查詢更新數據,可以減少網絡開銷
?? update emp set (job,sal,comm)= (select job,sal,comm from emp where ename='cmith')
?? where ename='scott'
?? 3 復制數據 update employee set deptno=7788 where job=(select job form emp where empno=7788)
10 刪除數據
? 1 delete 使用delete 的時候只刪去數據,而不會釋放空間,可以回退
? 2 truncate table emp? 不僅刪除數據,而起回釋放空間,不可一回退
11 使用事務控制語句
? 1 提交事務 commit
? 2 回退事務
??? 1 回退部分事務:savepoint a
??????????????????? rollback a
??? 2 回退全部事務? rollback
? 3 只讀事務,只允許運行查詢操作。可重復讀 set transaction read only
? 4 順序事務 set transaction isolation level serializable
12 分組函數,作用于多行,一般情況下于group by 字句結合使用,在使用分組函數時,如果忽略了 groub by 則匯總所有的行
?? select max(sal),min(sal) from emp
?? select avg(sal),sum(sal) from emp
?? select count(*) form emp
?? select count(emp) from emp
13 使用group by and having
?? 1 select deptno,avg(sal),max(sal) from emp group by deptno
?? 2 select deptno ,avg(sal),max(sal) from emp group by deptno having avg(sal)<2000
?? 分組函數只能出現在選擇列表,having 和order 中
?? 當選擇表包含有列表達式,和分組函數,那么這些列表和表達式必須出現在group by 字句中
?? 3 rollup 和cube 中 產生橫向縱向 的統計結果
??? 在使用rollup操作符時,在生成原有統計結果的基礎,還會生成橫向小計結果,在使用cube 操作
??? 符時,在軟有rollup 統計結果的基礎,還會生成縱向小計結果
??? select deptno,job,avg(sal) from emp group by rollup(deptnojob);
??? select deptno,job,avg(sal) from emp group by cube(deptnojob);
14 連接查詢
?? 在使用連接查詢時,必須在where 子句中指定有效的連接條件。如果不指定連接條件,或者指定無效的連接條件
?? 那么會導致生成笛卡爾乘積。
?? 1 select e.name,esal, from emp e,dept d where e.deptno=d.deptno;
?? 2 自然連接:在同一張表之間連接查詢
??? select manager.ename form emp manager,emp worker
??? where manager.empno=worker.mgr and worker.ename='blanke'
?? 3 內連接由于返回滿足連接條件的記錄,而外連接則是內連接的擴展,還會返回不滿足的連接條件的記錄
?? 4 左外連接: 不僅返回滿足條件的所有記錄,而且還會返回不滿足連接條件的連接符左表的其它行
??? select a.name,b,ename from adpt a left join emp b on a.deptno=b.deptno
?? 5 右外連接? right join
?? 6 完全外連接 不僅返回滿足條件的所有行,而且還會返回不滿足連接條件的所有其它行
??? select a.dname,b,ename from dept a full join emp b on a.deptno=b.deptno
15 子查詢
?? 1 單行子查詢 返回一行數據的子查詢語句
?? select ename,sal,deptno form emp where deptno=(select deptno from emp where ename='scott')
?? 2 多行子查詢,返回多行子查詢
???? 1) 使用in操作符
????? select ename ,job,sal,deptno from emp where job in
????? (select distince job from emp where deptno='10')
???? 2)在多行子查詢中使用all操作符
????? select ename,sal,deptno from emp where sal>all(select sal from emp where deptno='30')
???? 3)在多行子查詢中使用any操作符 任何一個結果即可
????? select ename,sal,deptno from em where sal>any(select sal from emp where deptno='30')
?? 3 多列子查詢
???? select ename,job,sal,deptno from emp where (deptno,job)=(select deptno,job from emp where ename='smith')
???? 1) 成對比較示例
???? select ename,sal,comm,deptno from emp where (sal,nvl(cpmm.-1)) in (select sal,nvl(comm,-1) from emp where deptno='30')
???? 2) 非成對比較
???? select ename,sal.comm,deptno from emp where sal_in(select sal from emp where deptno='30')
????? and nvl(comm,-1) in (select nvl) in (select nvl(comm,-1) from emp where deptno=30)
?? 4 相關子查詢
???? SELECT ENAME,JOB.SAL,DEPTNO FROM EMP FROM EXISTS(SELECT l FROM DEPT WHERE.......)
16 在dml 中使用子查詢
?? 1)在insert 中使用
?? insert into employee(id,name,title,salary)
?? select ename,job,sal from emp
?? 2)update emp sset (sal,comm)=
??? (select sal,comm fromm emp where ename='smtp')
??? where job=(select job from emp where ename='smith')
17 在ddl???
? 1 在create table 語句中使用子查詢
? create table new_emp(id,name,sql,jog,deptno) as
? select empno,ename,sal,job,deptno from emp
18 合并查詢
? 1) union? 自動去掉集合中重復的行,定對第一列結果排序
?? select ename,sal,job from emp where sal>2500
?? union
?? select ename,sal,job from emp where job='manager'
? 2) union all 不會取消重復值
? 3)intersect 取兩個結果繼的交集
? 4) ninus 取兩個結果結的差集
19 其它復雜查詢
?1 層次查詢