Oracle基礎總結
1.基本查詢
DML(數據庫操作語言):包括insert、update和delete,用于對表中的行進行添加、修改和刪除的操作;
DDL(數據庫定義語言):包括create和drop等,用于創(chuàng)建表和刪除表;
DCL(數據庫控制語言):包括grant和revoke,用于為用戶分配權限;
DQL(數據庫查詢語言):最常用,用于查詢數據;
DQL格式:
常規(guī):select需要展示的信息from表名where條件1group by ‘用于分類的列名’having條件2order by ‘用于排序的列名’asc/desc;
去重復查詢:select distinct …;
四則運算:select ename , sal*12 from emp;
null值處理:nulls first、nulls last;
連接符||的用法:select ?“編號:” ?|| ?empno ?|| ?“的雇員姓名是:” || ?ename;
邏輯運算:and、or、not(配合其他的關鍵詞使用,如not in,not null等)
比較運算:>、<、>=、<=、!=、<>、=
范圍限制:between…and…
模糊查詢:select * from emp where ename like ‘%郭%’;//郭德綱、郭小綱、東郭先生
select * from emp where ename like ‘郭_綱’;//郭德綱、郭小綱
排序:可以單一字段排序,也可以多字段排序;order by一定要放在SQL語句最后的位置;
SQL的函數:
單行函數:只影響單行的函數;
字符拼接函數:select concat(‘hello’,‘world’) from dual;//helloworld
字符截取函數:select substr(‘hello’, 1 , 3 ) from dual;//hel
計算字符串長度:select length(‘hello’) from dual;//5
字符替換函數:select replace(‘hello’,‘l’,‘d’) from dual;//heddo
四舍五入:select round(45.926,2) from dual;//45.93
截取指定位數:select trunc(45.926,2) from dual;//45.92
求余數:select mod(10,3) from dual;//1
日期:oracle中默認的日期格式是:dd-MM-yy
select ename,round((sysdate - hirdate)/365) from emp;查詢員工入職年限;
select ename,round(months_between(sysdate - hiredate)) from emp;查詢員工入職月數;
select add_months(sysdate,3) from dual;//顯示的結果為月份加3后的系統(tǒng)時間;
轉換函數:
to_char(sysdate,‘yyyy-mm-dd’);將日期轉為2017-07-13這種格式;
to_char(sysdate,‘fmyyyy-mm-dd’);將日期轉為2017-7-13這種格式;to_char(9,‘玖’);將數字轉換為金額大寫的‘玖’;
to_number(‘10’);將字符串形式的10轉換為數字10參與運算;
to_date(‘1990-05-20’,‘yyyy-mm-dd’);將字符串格式的日期轉換為日期;
通用函數:
nvl(comm,0);如果comm為null則按照0處理;
條件表達式:
select ename,sal,decode(
trunc(sal/2000,0),
0,0.00,
1,0.09,
2,0.20,
3,0.30,
0.40 ) TAX_RATE from emp;
多行函數:即聚合函數,常用的有count()、avg()、min()、max()、sum();
幾點注意:count(ename)執(zhí)行效率要高于count(*);
使用group by只能查出分組條件字段和分組函數查詢出來,不能有其他字段;
2.多表查詢
笛卡爾積:多表相乘的結果;
要去除笛卡爾積,則需要關聯(lián)查詢;
兩張表的關聯(lián)查詢字段一般是其中一張表的主鍵,另一張表的外鍵;
Oracle的連接條件的類型:
等值連接/不等值連接/外連接/自連接
隱式內連接:select * from emp,dept where emp.deptno = dept.deptno;
顯式內連接:select * from emp inner join dept on emp.deptno = dept.deptno;
左外連接:select * from emp left join dept on emp.deptno = dept.deptno;
右外連接:select * from dept right join emp on emp.deptno = dept.deptno;
Oracle特有的外連接:select * from emp,dept where emp.deptno = dept.deptno(+);//哪邊需要補充數據,就把(+)加在哪邊
自連接:select * from emp e1,emp e2 where e1.mgr =
e2.empno;
嵌套查詢:
select * from emp where emp.deptno = (select depno from dept where dname = ‘ANALYST’);
select * from emp where emp.deptno in (select deptno from dept where dname <> ‘ANALYST’);
select * from emp e,(select * from dept where dname <> ‘ANALYST’) r where e.deptno = r.deptno;
exists的用法:
select * from dept where exists (select * from emp where emp.deptno = dept.deptno);
rownum的用法:Oracle中用來做分頁顯示效果的特有方法;
select rownum,emp.* from emp;
rowid的用法:rowid是Oracle向數據庫中插入數據時生成的物理地址,修改表數據的時候用;
select rowid,emp.* from emp;
3.集合的運算
交集:(SQL語句1)intersect (SQL語句2);
并集:(SQL語句1)union (SQL語句2);//去除重復元素;
(SQL語句1)union all (SQL語句2);//不去除重復元素;
差集:(SQL語句1)minus (SQL語句2);//在前SQL語句執(zhí)行的結果集中,去除后SQL語句執(zhí)行的結果集中的元素后的結果集;