DB:數(shù)據(jù)庫
能夠完整保存數(shù)據(jù),可以盡可能較小冗余,支持多用戶共享的數(shù)據(jù)集合。
DBMS:數(shù)據(jù)庫管理系統(tǒng)
創(chuàng)建,管理,刪除,運行數(shù)據(jù)庫的軟件
數(shù)據(jù)模型:
- 概念模型
- 邏輯模型
- 物理模型
概念模型:解決存什么問題
邏輯模型:解決怎么存問題邏輯模型
- 層次模型
- 網(wǎng)狀模型
- 關(guān)系模型(主流的模型)
物理模型:解決存在哪的問題
Oracle三大文件:數(shù)據(jù)文件, 重做日志文件,控制文件
基礎(chǔ)命令
sqlplus sys/1 as sysdba
//以系統(tǒng)管理員的身份登錄oracle數(shù)據(jù)庫
sqlplus scott/tiger
//以scott用戶的身份登錄
conn scott/tiger
//切換scott用戶
alter user scott account unlock;
//給用戶scott解鎖
alter user scott identified by tiger;
//給用戶scott 默認密碼為:tiger
alter user haha identified by xixi;
SQL: 結(jié)構(gòu)化查詢語言
dql:數(shù)據(jù)查詢語言
dml:數(shù)據(jù)操作語言(第11章)
ddl:數(shù)據(jù)定義語言
dcl:數(shù)據(jù)控制語言
tpl/tcl:事物處理語言或者事物控制語言
dql:
語法:語言本身不區(qū)分大小寫
SELECT [distinct] | * |{列名 | 表達式 [列別名][,... ]}
FROM 表名 ;
SELECT :尋找哪些數(shù)據(jù)
FROM: 從哪里尋找數(shù)據(jù)
[]:被中括號括起來的內(nèi)容是可有可無的
| :或者
關(guān)鍵字
' *' :所有
列名: 表中某一列的名字
表達式:表達式的內(nèi)容可以和表無關(guān),但是受表行數(shù)的影響
字符串表達式 : || 是拼接操作符號 ,字符串由'' 括起來,字符串里的內(nèi)容可以和數(shù)據(jù)源無關(guān),null值和字符串做拼接沒有影響
算數(shù)表達式:算數(shù)運算
列別名:給列起外號,如果碰到格式中不符合可以使用雙引號括起來
distinct:去掉重復(fù)數(shù)據(jù)
,...:
空值/null:任何數(shù)和空值做任何運算的結(jié)果都為null
選擇查詢:
SELECT [distinct] | * |{列名 | 表達式 [列別名][,... ]}
FROM 表名
[where 邏輯表達式 ];
邏輯表達式 = 關(guān)系表達式 [邏輯運算符 ...]
關(guān)系表達式 = 列名|表達式 比較運算符 列名|表達式
SELECT [distinct] | * |{列名 | 表達式 [列別名][,... ]}
FROM 表名
[where {列名|表達式 比較運算符 列名|表達式} [邏輯運算符 ...] ];
比較運算符:
常用比較運算符:
>,< , = , >= , <= , != , <>
對日期的比較需要按照日期的格式
特殊比較運算符:
between and:
in:
is null:
like:模糊查詢
通配符:%: 任意長度的任意字符
_: 一個長度的任意字符
escape: 注冊轉(zhuǎn)譯字符
邏輯運算符:
and :
or :
not :
邏輯運算符的規(guī)則:
and | true | false | null |
---|---|---|---|
true | true | false | null |
false | false | false | false |
null | false | false | null |
or | true | false | null |
---|---|---|---|
true | true | true | true |
false | true | false | null |
null | false | false | null |
案例:
--查詢smith的信息
select *
from emp
where ename = 'SMITH';
--查詢工資超過1000的員工信息
select *
from emp
where sal>1000;
--查詢10部門職工的工資
select sal
from emp
where 10 = deptno
--查詢1980年12月17日入職的員工信息
select *
from emp
where hiredate = '17-12月-80';
--查詢1982年之前入職的員工的信息
select *
from emp
where hiredate<='31-12月-81';
--查詢工資低于2000 并且 工資高于1000的人員信息
select *
from emp
where sal<=2000 and sal>=1000;
select *
from emp
where sal between 2000 and 1000;
--查詢屬于10部門或者20部門或者30部門的員工的名字
select ename,deptno
from emp
where deptno =10 or deptno = 20 or deptno = 30;
select ename ,deptno
from emp
where deptno in(10,20,30);
--查詢不在10部門上班的員工的信息
select *
from emp
where (deptno <> 10)
--查詢獎金為null的人員的信息
select *
from emp
where comm is not null;
--查詢?nèi)藛T的實際收入
select ename ,sal,comm,sal+comm
from emp
where comm is not null;
select ename ,sal,comm,sal
from emp
where comm is null;
--查詢名字像KING的員工的信息
select *
from emp
where ename = 'KING';
--查詢出姓S的員工信息
select *
from emp
where ename like 'S%'
--查詢出姓名中包含S的員工信息
select *
from emp
where ename like '%S%'
--查詢出名字中倒數(shù)第二個字母是T的員工信息
select *
from emp
where ename like '%T_';
--查詢所有員工的姓名和工資,要求按照工資的大小,從小往大顯示
select ename AS NAME ,job
from emp
WHERE NAME = 'SMITH'
order by NAME desc
--查詢名字中包含_的人員的信息
select * from emp
where ename like '%\_%' escape '\';
排序:
SELECT [distinct] | * |{列名 | 表達式 [列別名][,... ]}
FROM 表名
[where {列名|表達式 比較運算符 列名|表達式} [邏輯運算符 ...] ]
[order by 列名|表達式|列別名 [asc] [desc] [,...] ];
函數(shù)
函數(shù):編程中的一個工具,關(guān)心函數(shù)功能,名字,參數(shù)類型,結(jié)果。
函數(shù)分類:根據(jù)返回數(shù)據(jù)結(jié)果集多少
單行函數(shù):一行數(shù)據(jù)得到一個結(jié)果,多行數(shù)據(jù)得到多個結(jié)果
多行函數(shù):多行數(shù)據(jù)得到一個結(jié)果。
單行函數(shù):根據(jù)數(shù)據(jù)類型字符函數(shù)
數(shù)字函數(shù)
日期函數(shù)
轉(zhuǎn)換函數(shù)
通用函數(shù)
語法:
函數(shù)的調(diào)用:
函數(shù)名(列名|表達式 [,...])
():參數(shù)列表
舉例:
電飯鍋(電,水,米) = 飯
作用場景:
除了from都能用
例子:
--函數(shù)
--字符函數(shù):
--Lower
select lower(ename) from emp;
--匹配名字是smith的人員忽略大小寫
select ename from emp where lower(ename) = 'smith';
select * from emp where lower(ename) = lower('smith')
--upper
select upper('xixi') from emp;
--initcap
select distinct initcap('xiasjdasd') from emp;
--dual
select initcap('xiasjdasd') from dual;
--concat
select concat(concat('a','b'),'c')from dual;
select 'a'||'b'||'c' from dual;
--substr
select substr('kakaxi',4,2) from dual;
--instr
select instr('kakaxi','s',1,2) from dual;
--查詢出所有名字帶A的人員的姓名中的A
select substr(ename,instr(ename,'A')) from emp where ename like '%A%';
--查詢出名字中包含S的人員信息,要求不允許使用like
select * from emp where instr(ename,'S')<>0;
--查詢出名字是以S開頭的人員的信息,要求不允許使用like 和instr
select *
from emp
where substr(ename,1,1) = 'S'
select ename from emp;
--replace
select replace('haha','a','c') from emp;
--查詢出名字中包含S的人員信息,要求不允許使用like ,instr,substr
select * from emp where replace(ename,'S','s') <>ename
--lpad
select lpad('abc',1,'$') from dual;
select rpad('abc',10,'$') from dual;
--length 字符長度
SELECT LENGTH('你我他')FROM DUAL;
--trim
select trim( ' hah ah ') from dual;
select trim(leading 'h' from 'haha') from dual;
select trim(trailing 'a' from 'haha') from dual;
select trim(both 'a' from 'haha') from dual;
--數(shù)字函數(shù):
--mod :取余
select mod(5,3) from dual;
--round:四舍五入
select round(4.5) from dual;
--trunc:截斷
select trunc(4.1) from dual;
-floor:向下取整
select floor(4.9) from dual;
--ceil:向上取整
select ceil(4.98) from dual;
select floor (-4.1) from dual;
--sign :取表達式或者數(shù)的結(jié)果為正負
select sign(-100) from dual;
--abs:絕對值
select abs(-100) from dual;
--日期函數(shù)
--加一天
select hiredate,hiredate+1 from emp;
--加一個小時
select hiredate,hiredate+1/24/60/60/1000 from emp;
--加一星期
select hiredate,hiredate +7 from emp;
--加一個月 :需要函數(shù)
select hiredate,add_months(hiredate,0.8) from emp;
select * from emp for update;
--17-十二月-80
--sysdate:系統(tǒng)當前時間
select sysdate from dual;
--顯示每個員工入職的天數(shù)
select HIREDATE ,sysdate - hiredate from emp WHERE HIREDATE ='23-5月-87'
--顯示每個員工入職的月數(shù)
select months_between(sysdate,hiredate) from emp;
--next_day:下一個周幾
select next_day(sysdate,2) from dual;
--last_day:當月最后一天
select last_day(sysdate) from dual;
select trunc(sysdate,'ss')from dual;
--區(qū)分年月日
select extract (day from sysdate) from dual;
轉(zhuǎn)換:顯示轉(zhuǎn)換和隱式轉(zhuǎn)換
隱式轉(zhuǎn)換:改變數(shù)據(jù)看原始的類型;一般轉(zhuǎn)換動字符;連接運算其他轉(zhuǎn)字符。
--數(shù)據(jù)類型轉(zhuǎn)換
-- 數(shù)據(jù)類型: date ,number ,char
-- 轉(zhuǎn)換規(guī)則: char->date, char ->number , number <=char => date
-- 轉(zhuǎn)換方法: to_char(number|date ,[fmt]) ;to_date (char,[fmt]);to_number(char[,fmt])
-- 格式碼:yyyy mm dd hh mi ss
-- to_char:
select to_char(1) from dual ;
select 1+2 from dual;
-- select to_char(sysdate)+1 from dual;
select sysdate+1 from dual;
select to_char(sysdate ,'yyyy$mm$dd $hh24$mi$ss')from dual;
select * from emp where hiredate>'1-1月-81';
-- to_date :
select to_date('1981-1-1','yyyy-mm-dd')+1 from dual;
-- to_number:
-- to_char: , . 0 9 $ L
select to_char(400000,'$9999,999.00') from dual;
--
select to_number('$400,000.00','$9999,999.00')+100 from dual;
--to_char
select to_char(sysdate,'yy$%-"年"mm') from dual;
--通用函數(shù):
--nvl
select nvl(comm,0) from emp;
select nvl(to_char(comm),'0') from emp;
--nvl2
select nvl2(comm ,comm,0) from emp;
--nullif
select nullif(3,3) from dual;
select 1/nullif(0,0) from dual;
--coalesce
select coalesce(null,null,null,4,null,6) from dual;
--case ,decode
--查詢各個部門的人員姓名,部門編號和部門的名稱
select ename,
deptno,
(case deptno
when 10 then
'10部門'
when 20 then
'20部門'
else
'30部門'
end)
from emp;
select ename ,deptno ,decode(deptno ,10,'10部門',20,'20部門','30部門')
from emp ;
select decode(haha,1,11,2,12,3,13,4,14,5,15,6,16,7,17,8,19) from dual;
select * from dept;
/*
10 ACCOUNTING
20 RESEARCH
30 SALES
40 OPERATIONS
*/
--要求查詢出 所有人員的姓名,工資,(如果工資低于1000,屌絲;如果1000到2000之間 ,
--能活;如果2001到3000之間,哎呦不錯哦;如果超過高富帥)
--case 的簡單寫法
select ename,
sal,
case
when sal < 1000 then
'X絲'
when sal between 1000 and 2000 then
'能活'
when sal between 2001 and 3000 then
'哎呦不錯哦'
else
'高富帥'
end
from emp;
select ename,
sal,
decode(sign(sal - 1000),
-1,
'X絲',
0,
'能活',
1,
decode(sign(sal - 2000),
-1,
'能活',
0,
'能活',
1,
decode(sign(sal- 3000),
-1,
'哎呦不錯哦',
0,
'哎呦不錯哦',
1,
'高富帥')))
from emp;
--order by
select *
from emp
order by decode(job,
'PRESIDENT',
1,
'ANALYST',
2,
'MANAGER',
3,
'SALESMAN',
4,
'CLERK',5)
--PRESIDENT,ANALYST,MANAGER,SALESMAN,CLERK
create table haha(name varchar2(10),class varchar2(10) ,cj number(10));
--select * from haha for update
select name,
sum(case class
when 'oracle' then
cj
end) oracle,
sum(case class
when 'java' then
cj
end) java,
sum(case class
when 'HTML' then
cj
end) HTML
from haha
group by name;
多行函數(shù)
sum:和
count:計數(shù)
max:最大
min:最小
avg:平均
多行函數(shù)不處理null值
計數(shù):count(1) ;count(*)
多行函數(shù)的作用是用來統(tǒng)計分析,每次統(tǒng)計相當于對原始數(shù)據(jù)進行壓縮提煉,會改變原表的結(jié)構(gòu)。
分組語句:分組的作用是細化統(tǒng)計的項目,讓統(tǒng)計數(shù)據(jù)更精準。
group by:分組條件
having: 能夠處理分組函數(shù)條件的條件子句
SELECT [distinct] | * |{列名 | 表達式 [列別名][,... ]}
FROM 表名
[where {列名|表達式 比較運算符 列名|表達式} [邏輯運算符 ...] ]
[group by 列名|表達式 [,...]]
[having (包含分組函數(shù)的)條件表達式]
[order by 列名|表達式|列別名 [asc] [desc] [,...] ];
語句執(zhí)行順序:
from
where
group by
having
select
order by
--函數(shù)嵌套
-- 單行嵌單行
select concat(concat('a','b'),'c') from dual;
-- 單行嵌多行
select round(avg(sal)) from emp;
-- 多行嵌單行
select avg(round(sal)) from emp;
-- 多行嵌多行
select avg(count(1)) from emp group by deptno
多表連接
--查詢smith的員工姓名,部門名稱,部門所在的地址
select empno ,ename ,deptno from emp where ename = 'SMITH';
select dname ,loc from dept where deptno = 20;
--多表連接
select * from emp , dept
WHERE DEPT.DEPTNO = EMP.DEPTNO
--sql 99
--sql99
--cross join (交叉連接)
select * from emp cross join dept;
--oracle 笛卡爾積
select * from emp , dept;
--natural joib (自然連接)
select * from emp natural join dept;
emp salgrade
--using 連接(natural 二代)
select * from emp join dept using(deptno);
select * from emp a join emp b using(empno)
--oracle 等值連接
select * from emp ,dept where emp.deptno = dept.deptno
--on 連接
select *
from emp
join salgrade
on (emp.sal between salgrade.losal and salgrade.hisal and ename = 'SMITH');
select * from emp join dept
on(emp.deptno = dept.deptno) ;
--不等值連接
select *
from emp
,salgrade
where emp.sal between salgrade.losal and salgrade.hisal;
--left outer join
--查詢領(lǐng)導(dǎo)的姓名和員工的姓名
select a.ename ,b.ename
from emp b ,emp a
where b.mgr =a.empno (+)
select a.ename ,b.ename
from emp b left outer join emp a on(b.mgr =a.empno);
--right outer join
--查詢部門的名稱和員工的名稱 (所有)
select dname ,ename
from emp,dept
where emp.deptno (+)= dept.deptno;
select dname ,ename
from emp right outer join dept on( emp.deptno = dept.deptno)
/*
1.顯示員工SMITH的姓名,部門名稱,直接上級名稱
2.顯示員工姓名,部門名稱,工資,工資級別,要求工資級別大于4級。
3.顯示員工KING和FORD管理的員工姓名及其經(jīng)理姓名。
4.顯示員工姓名,參加工作時間,經(jīng)理名,參加工作時間,要求參加時間比經(jīng)理早。
*/
SELECT A.ENAME ,C.DNAME ,B.ENAME
FROM EMP A, EMP B,DEPT C
WHERE A.MGR = B.EMPNO AND A.DEPTNO = C.DEPTNO AND A.ENAME = 'SMITH';
SELECT ENAME ,DNAME ,SAL ,GRADE
FROM EMP A ,DEPT B,SALGRADE C
WHERE A.DEPTNO = B.DEPTNO AND A.SAL BETWEEN C.LOSAL AND C.HISAL AND GRADE >4;
SELECT A.ENAME ,B.ENAME
FROM EMP A,EMP B
WHERE A.MGR = B.EMPNO AND B.ENAME IN ('KING','FORD');
SELECT A.ENAME ,A.HIREDATE ,B.ENAME ,B.HIREDATE
FROM EMP A ,EMP B
WHERE A.MGR = B.EMPNO AND A.HIREDATE<B.HIREDATE;
練習(xí)題:
查詢20號部門的所有員工信息:
查詢獎金(COMM)高于工資(SAL)的員工信息:
查詢獎金高于工資的20%的員工信息:
查詢10號部門中工種為MANAGER和20號部門中工種為CLERK的員工的信息:
查詢所有工種不是MANAGER和CLERK,且工資大于或等于2000的員工的詳細信息:
查詢沒有獎金或獎金低于100的員工信息:
查詢員工工齡大于或等于10年的員工信息:
查詢員工信息,要求以首字母大寫的方式顯示所有員工的姓名:
查詢在2月份入職的所有員工信息:
顯示所有員工的姓名、入職的年份和月份,按入職日期所在的月份排序,若月份相同則按入職的年份排序:
查詢'JONES'員工及所有其直接、間接下屬員工的信息:
查詢SCOTT員工及其直接、間接上級員工的信息:
試用SQL語言完成下列查詢(多表查詢):
查詢各個部門的詳細信息以及部門人數(shù)、部門平均工資:
查詢10號部門員工以及領(lǐng)導(dǎo)的信息:
查詢工資為某個部門平均工資的員工信息:
統(tǒng)計各個工種的人數(shù)與平均工資:
統(tǒng)計每個部門中各個工種的人數(shù)與平均工資:
查詢所有員工入職以來的工作期限,用“年月**日”的形式表示。
查詢?nèi)藬?shù)最多的部門信息:
部門平均薪水最高的部門編號:
比普通員工的最高薪水還要高的經(jīng)理人名稱:
查詢所有員工工資都大于1000的部門的信息:
查詢所有員工工資都大于1000的部門的信息及其員工信息:
查詢所有員工工資都在900~3000之間的部門的信息:
查詢所有工資都在900~3000之間的員工所在部門的員工信息:
查詢每個員工的領(lǐng)導(dǎo)所在部門的信息:
查詢工作等級為2級,1985年以后入職的工作地點為DALLAS的員工編號、姓名和工資:
查詢20號部門的所有員工信息:
select *
from emp
where deptno = 20;
查詢獎金(COMM)高于工資(SAL)的員工信息:
select *
from emp
where comm>sal;
查詢獎金高于工資的20%的員工信息:
select * from emp
where comm>sal*1.2;
查詢10號部門中工種為MANAGER和20號部門中工種為CLERK的員工的信息:
select *
from emp
where (deptno = 10 and job = 'MANAGER' ) or ( deptno = 20 and job = 'CLERK');
查詢所有工種不是MANAGER和CLERK,且工資大于或等于2000的員工的詳細信息:
select * from emp
where job not in ('MANAGER','CLERK') and sal>=2000;
查詢沒有獎金或獎金低于100的員工信息:
select * from emp
where comm is null or comm <100;
查詢員工工齡大于或等于10年的員工信息:
select * from emp
where months_between (sysdate,hiredate)/12>=10
查詢員工信息,要求以首字母大寫的方式顯示所有員工的姓名:
select initcap(ename)
from emp ;
查詢在2月份入職的所有員工信息:
select * from emp
where to_char(hiredate,'mm') = '02';
顯示所有員工的姓名、入職的年份和月份,按入職日期所在的月份排序,若月份相同則按入職的年份排序:
select ename ,to_char(hiredate,'yyyy-mm')
from emp
order by to_char(hiredate,'mmyy')
查詢'JONES'員工及所有其直接、間接下屬員工的信息:
select * from emp a
start with ename ='JONES'
connect by PRIOR EMPNO = MGR
查詢SCOTT員工及其直接、間接上級員工的信息:
select * from emp a
start with ename ='SCOTT'
connect by PRIOR mgr = empno
試用SQL語言完成下列查詢(多表查詢):
查詢各個部門的詳細信息以及部門人數(shù)、部門平均工資:
select dept.deptno, dname, loc, count(1), avg(sal)
from dept, emp
where dept.deptno = emp.deptno
group by dept.deptno, dname, loc
查詢10號部門員工以及領(lǐng)導(dǎo)的信息:
select *
from emp a ,emp b
where a.mgr = b.empno and a.deptno = 10;
查詢工資為某個部門平均工資的員工信息:
select *
from emp
where sal in(select avg(sal) from emp group by deptno);
統(tǒng)計各個工種的人數(shù)與平均工資:
select job,count(1),avg(sal)
from emp
group by job;
統(tǒng)計每個部門中各個工種的人數(shù)與平均工資:
select deptno,job,count(1),avg(sal)
from emp
group by deptno,job;
查詢所有員工入職以來的工作期限,用“**年**月**日”的形式表示。
select to_char(hiredate,'yy"年"mm"月"dd"日"')
from emp
查詢?nèi)藬?shù)最多的部門信息:
select *
from dept
where deptno =
(select deptno
from emp
having count(1) = (select max(count(1)) from emp group by deptno)
group by deptno);
select a.*
from dept a,
(select deptno, count(1) counts from emp group by deptno) b,
(select max(count(1)) counts from emp group by deptno) c
where a.deptno = b.deptno
and b.counts = c.counts
/* 部門平均薪水最高的部門編號:
比普通員工的最高薪水還要高的經(jīng)理人名稱:
查詢所有員工工資都大于1000的部門的信息:
查詢所有員工工資都大于1000的部門的信息及其員工信息:
查詢所有員工工資都在900~3000之間的部門的信息:
查詢所有工資都在900~3000之間的員工所在部門的員工信息:
查詢每個員工的領(lǐng)導(dǎo)所在部門的信息:
查詢工作等級為2級,1985年以后入職的工作地點為DALLAS的員工編號、姓名和工資:*/
--1
select deptno
from emp
group by deptno
having avg(sal) = (select max(avg(sal)) from emp group by deptno)
--2
select ename
from emp a
where exists
(select 1 from emp b where a.empno = b.mgr)
and a.sal >
(select max(sal)
from (select *
from emp a
where not exists
(select 1 from emp b where a.empno = b.mgr)));
select ename
from emp a
where empno in (select mgr from emp)
and a.sal >
(select max(sal)
from emp
where empno not in (select mgr from emp where mgr is not null));
select a.ename
from emp a,
(select max(sal) maxsal
from emp
where empno not in (select mgr from emp where mgr is not null)) b
where a.sal > b.maxsal
and a.empno in (select mgr from emp);
--3
select *
from dept
where deptno in
(select deptno from emp group by deptno having min(sal) > 1000);
--4
select *
from dept, emp
where dept.deptno in
(select deptno from emp group by deptno having min(sal) > 1000)
and dept.deptno = emp.deptno;
--5
select *
from dept
where deptno in
(select deptno from emp group by deptno having min(sal) >=900 and max(sal)<=3000);
--7
select *
from dept
where deptno in
(select deptno from emp where empno in (select mgr from emp));
--8查詢工作等級為2級,1985年以后入職的工作地點為DALLAS的員工編號、姓名和工資:
select empno, ename, sal
from emp a, salgrade s, dept d
where a.deptno = d.deptno
and a.sal between s.losal and s.hisal
and s.grade = 2
and loc = 'DALLAS'
and to_number(to_char(hiredate, 'yyyy')) > 1985;
子查詢
--分步查詢
select sal from emp where ename ='JONES';
SELECT ENAME FROM EMP WHERE SAL>2975.00;
--多表連接
SELECT B.ENAME
FROM EMP A, EMP B
WHERE A.ENAME ='JONES' AND a.sal <b.sal;
--子查詢
SELECT ENAME
FROM EMP
WHERE SAL > (select sal from emp where ename = 'JONES');
--多行子查詢
in, any ,all
any:翻譯是一些的意思
--多列子查詢
where中多個條件綁定判斷需要通過多列子查詢處理。
--NOT IN 空值問題
如果子查詢的返回值中有null值,需要通過is not null處理空值。
FROM子查詢:通過from子查詢能夠把一個子查詢作為臨時表供給主查詢當數(shù)據(jù)源使用,在使用from子查詢的時候一般需要給子查詢起表別名,其中的分組函數(shù)起列別名。
from子查詢的作用一般處理分組函數(shù)和非分組條件同時顯示的問題。
分頁:
```sql
--rownum:偽列
select b.*
from (select rownum rn, a.*
from (select * from emp order by sal desc) a
where rownum <= 5) b
where rn > 0
;
select b.*
from (select rownum rn, a.* from emp a order by sal desc) b
where rn<=10 and rn > 5;
--分頁
查詢語句分析整理
--查詢emp表
select * from emp;
--查詢?nèi)藛T的姓名
select ename from emp;
--查詢10部門人員的姓名
select ename from emp where deptno = 10;
--查詢出smith的部門名稱
select deptno from emp where ename ='SMITH';
select dname from dept where deptno = 20;
--不能處理所有人
--單行函數(shù):更好處理數(shù)據(jù),來源于原表,根據(jù)使用需求調(diào)整數(shù)據(jù)
--查詢smith,king,scott的部門名稱
--子查詢
select dname
from dept
where deptno in
(select deptno
from emp
where ename in ('SMITH', 'KING', 'SCOTT'));
--查詢出和1981年入職的任意一個員工的部門和職位完全相同員工姓名、部門、職位、入職日期,
select ename ,deptno ,job,hiredate
from emp
where (deptno,job)in(select deptno,job from emp where to_char(hiredate,'yyyy')='1981')
--查詢smith所在部門的平均工資,我們想要通過統(tǒng)計更清晰的理解數(shù)據(jù)情況
select avg(sal)
from emp
where deptno = (select deptno from emp where ename ='SMITH');
--如果多行函數(shù)當條件:必須having
--如果多行函數(shù)做查詢:select中有,groupby 必須有
--多列子查詢
--查詢所在部門工資最高的人的人員信息
--所在,其,自己
select *
from emp
where (deptno, sal) in (select deptno, max(sal) from emp group by deptno);
--高級子查詢
--顯示每個部門工資最低的人的姓名和所在部門的最低工資
--查詢比所在部門平均工資高的人員信息
--查詢員工的姓名和其所在部門部門的名稱
select ename, (select dname from dept b where a.deptno = b.deptno) dname
from emp a
select ename, sal
from emp a
where sal > (select avg(sal) from emp b where a.deptno = b.deptno);
--查詢員工的姓名和領(lǐng)導(dǎo)的姓名
select ename, (select ename from emp b where a.mgr = b.empno) ename
from emp a;
select a.ename, b.ename from emp a, emp b where a.mgr = b.empno(+)
--查詢哪些員工是經(jīng)理
select * from emp where empno in(select mgr from emp);
select distinct b.* from emp a,emp b where a.mgr = b.empno;
select *
from emp a
where empno =
(select distinct mgr from emp b where a.empno = b.mgr);
--高級子查詢改寫,原因是:尷尬
select *
from emp a
where (select count(1) from emp b where a.empno = b.mgr) > 0;
--查詢是領(lǐng)導(dǎo)的員工信息
select *
from emp a
where exists (select 1 from emp b where a.empno = b.mgr)
--查詢出每個部門工資前三的人員信息
-- 使用集合
select *
from emp e
where sal = (select max(sal)
from (select *
from emp
minus (select *
from emp a
where sal = (select max(sal)
from emp b
where a.deptno = b.deptno)
union
select *
from emp c
where sal = (select max(sal)
from (select *
from emp
minus (select *
from emp a
where sal =
(select max(sal)
from emp b
where a.deptno =
b.deptno))) d
where c.deptno = d.deptno))) f
where e.deptno = f.deptno)
union
select *
from emp a
where sal = (select max(sal) from emp b where a.deptno = b.deptno)
union
select *
from emp c
where sal = (select max(sal)
from (select *
from emp
minus (select *
from emp a
where sal = (select max(sal)
from emp b
where a.deptno = b.deptno))) d
where c.deptno = d.deptno)
union
select *
from emp a
where sal = (select max(sal) from emp b where a.deptno = b.deptno)
--使用相關(guān)子查詢
select *
from emp a
where (select count(1)
from emp b
where a.deptno = b.deptno
and a.sal < b.sal) <= 2;
--exists
select *
from emp a
where exists (select 1
from emp b
where a.deptno = b.deptno
and a.sal < b.sal having count(1) <= 2);
--多表連接
select distinct dname
from emp, dept
where emp.deptno = dept.deptno
and ename in ('SMITH', 'KING', 'SCOTT');
--多行函數(shù)使用問題
--顯示每個部門工資最低的人的姓名和所在部門的最低工資
select ename ,minsal
from emp a, (select deptno ,min(sal)minsal from emp group by deptno)b
where a.deptno = b.deptno
and a.sal = b.minsal;
--查詢比所在部門平均工資高的人員信息
select ename, sal
from emp a,
(select deptno, avg(sal) avgsal from emp group by deptno) b
where a.deptno = b.deptno
and a.sal > b.avgsal;
/* 如下練習(xí),用相關(guān)子查詢完成
1.查詢所有雇員編號,名字和部門名字。
2.查詢哪些員工是經(jīng)理?
3.查詢哪些員工不是經(jīng)理?
4.查詢每個部門工資最低的兩個員工編號,姓名,工資。
*/
--1
select empno ,ename ,(select dname from dept b where a.deptno = b.deptno) dname
from emp a ;
select a.empno ,a.ename ,b.dname
from emp a ,dept b
where a.deptno = b.deptno ;
--2
select *
from emp where empno in(select mgr from emp);
select distinct a.*
from emp a ,emp b
where a.empno = b.mgr;
select *
from emp a where exists(select 1 from emp b where a.empno =b.mgr);
--4
select empno ,ename ,sal,deptno
from emp a
where exists(select 1 from emp b where a.sal>b.sal and a.deptno = b.deptno having count(1)<=1)
層次查詢
select level,a.* from emp a
--start with ename ='SMITH'
where level = 2
start with ename ='KING'
connect by prior empno = mgr and ename <>'BLAKE'
level:層級
start with :起點
connect by prior :層次條件
DML
DELETE
刪除數(shù)據(jù)
delete 代替select * ,其他和查詢一直
INSERT
添加數(shù)據(jù)有兩種:
添加主數(shù)據(jù):
insert into 表 (列名[,...]) values(值[,...]);
添加業(yè)務(wù)數(shù)據(jù):
insert into 表 (列名[,...]) select語句( 查詢值 );
UPDATE
update語句分成兩部分: 分為修改哪些數(shù)據(jù)?修改的數(shù)據(jù)內(nèi)容為什么?
思路:根據(jù)“值等于”和“為什么”斷句,分別書寫兩個查詢語句再合成一個語句
insert語句一般和多表配合,update一般和子查詢配合使用
事物:一組sql語句(一般對應(yīng)前臺的一個業(yè)務(wù)操作),從第一個當前對話或者連接中運行的dml語句開始到第一個tpl語句結(jié)束。
作用:保證數(shù)據(jù)的一致性。
特性:原子性,一致性,持久性,隔離性。
隔離性是通過鎖來實現(xiàn)的。
DDL
數(shù)據(jù)庫對象分為:表,約束,索引,視圖,序列,同義詞,(函數(shù),過程,觸發(fā)器)等。
定義和操作數(shù)據(jù)庫對象的語句為ddl語句:
create drop truncate alter
數(shù)據(jù)庫對象命名:字母開頭,(數(shù)字,字母,下劃線,$,#)組成。
數(shù)據(jù)類型:
varchar2對比char ,能夠節(jié)省空間,可變長度;缺點是速度比char慢。如果是性別類型的字段最好用char,varchar2適合如名字這樣的字段。
number:可以不跟長度,但是最好是有長度的。(如果是兩個參數(shù),第一個數(shù)總長度,第二個是小數(shù)長度)
clob和blob:4g大小的字段,一個是字符,一個是圖片
drop table:刪除表的結(jié)構(gòu)和數(shù)據(jù),可以還原
delete :刪除某些數(shù)據(jù),可以還原
truncate table:刪除全部數(shù)據(jù)不能還原
alter table: 修改表的結(jié)構(gòu)。
--修改平均工資最低的部門的所有的做領(lǐng)導(dǎo)的人員 的工資為10部門平均工資
update emp a
set sal =
(select avg(sal) from emp where deptno = 10)
where empno in
(select empno
from emp b,
(select avg(sal) avgsal, deptno from emp group by deptno) d,
(select min(avg(sal)) minavgsal from emp group by deptno) c
where empno in (select distinct mgr from emp)
and b.deptno = d.deptno
and d.avgsal = c.minavgsal);
update emp a
set sal =
(select avg(sal) from emp where deptno = 10)
where exists (select 1
from emp b,
(select deptno
from emp
group by deptno
having avg(sal) = (select min(avg(sal))
from emp
group by deptno)) c
where a.empno = b.mgr
and a.deptno = c.deptno
and a.deptno = b.deptno)
--修改工資等級在3級的員工的領(lǐng)導(dǎo)的 工資為20部門的最低工資
update emp a
set sal =
(select min(sal) from emp where deptno = 20)
where empno in (select mgr
from emp, salgrade
where emp.sal between losal and hisal
and grade = 3)
--修改工作地點在芝加哥的部門中工資最低的人員的 崗位為他們所在部門工資最高的人員的崗位
update emp a
set job =
(select job
from emp b
where a.deptno = b.deptno
and b.sal = (select max(sal) from emp where deptno = b.deptno))
where EXISTS (SELECT 1
FROM DEPT B
WHERE A.DEPTNO = B.DEPTNO
AND LOC = 'CHICAGO')
AND EXISTS (SELECT 1
FROM EMP B
WHERE A.DEPTNO = B.DEPTNO
AND A.SAL < B.SAL HAVING count(1) <= 0)
update emp a
set job =
(select job
from emp,
dept,
(select min(sal) maxsal, deptno from emp group by deptno) c
where emp.deptno = dept.deptno
and loc = 'CHICAGO'
and c.deptno = dept.deptno
and c.maxsal = emp.sal)
where empno in
(select empno
from emp,
dept,
(select min(sal) maxsal, deptno from emp group by deptno) c
where emp.deptno = dept.deptno
and loc = 'CHICAGO'
and c.deptno = dept.deptno
and c.maxsal = emp.sal);
約束
--定義約束
--列級約束
create table Person(
name varchar2(50) not null,
sex char(1) constraint sex_person_ck check ( sex in ('1','2','0')),
id char(18) constraint id_person_uk unique,
dna char(50) constraint dna_person_pk primary key,
classid varchar2(10) constraint classid_Person_class_fk references class(classid)
)
--追加約束
--表級約束
create table class(
classid varchar2(10) ,
classcount number ,
classname varchar2(10) not null,
constraint classid_class_pk primary key (classid),
constraint classcount_class_ck check (classcount>0)
)
alter table Person add constraint classid_Person_class_fk foreign key (classid)
references class(classid)
視圖
create or replace view haha
as
select * from emp
where deptno = 10
with check option constraint hahacon
;
or replace 是替換的意思
with check option 根據(jù)視圖的查詢條件做約束
視圖分為:簡單和復(fù)雜
復(fù)雜視圖一般包含多個表,分組,函數(shù)等。
復(fù)雜視圖根據(jù)實際情況看是否能進行dml操作,簡單視圖可以做dml操作。
with read only:只讀視圖
序列
使用序列:生產(chǎn)系統(tǒng)的id
nextval ,currval 通過這兩個屬性使用
一般不適用循環(huán),緩存屬性可能會丟值
索引
索引:通過對某個字段建立索引表,對索引表進行排序之后使用二分查找提高查詢效率,最后通過rowid找回原表的數(shù)據(jù)位置。
索引:可以是某一列或者某幾列;如果給了主鍵或者唯一鍵約束默認給索引。
索引的優(yōu)缺點:
提高查詢速度,浪費控件,降低數(shù)據(jù)修改的速度
什么使用適合使用索引:
數(shù)據(jù)量很大,查詢的內(nèi)容占總內(nèi)容較少,表本身不經(jīng)常被修改 ,數(shù)據(jù)超過20萬
同義詞
只有和權(quán)限配合才有意義
用戶,權(quán)限,角色
用戶是oracle最大的對象,包含其他對象。
權(quán)限:
系統(tǒng)權(quán)限:
對象權(quán)限:
通過dcl語句來操作:
grant create session to scott
grant select on emp to scott
revoke create session from scott
角色:權(quán)限的集合,系統(tǒng)常用的是dba,connect,resourse
遠程連接
客戶端:需要配置本地服務(wù)
本地服務(wù)由:網(wǎng)絡(luò)服務(wù)名=數(shù)據(jù)庫名+ip地址+端口 組成
服務(wù)器:需要配置監(jiān)聽器