? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 數(shù)據(jù)庫(kù)3
--當(dāng)要查詢的數(shù)據(jù)來(lái)自于不同的表,需要實(shí)現(xiàn) 多表聯(lián)查
--92語(yǔ)法? 99語(yǔ)法
--內(nèi)連接 :有where ,滿足條件才顯示,不滿足不顯示
--92語(yǔ)法 select 數(shù)據(jù) from 表1,表2,表3...;
--笛卡爾積? 對(duì)乘效果
select * from emp,dept;
--查詢所有員工的信息,以及所在的部門信息
--數(shù)據(jù): 員工信息? 部門信息
--來(lái)源: emp dept
--等值連接
select empno,ename,emp.deptno,dname from emp,dept where emp.deptno = dept.deptno;
--注意:當(dāng)使用同名字段時(shí)候,需要指明字段出處
--非等值連接
--查詢員工信息以及每個(gè)員工的薪資等級(jí)
select empno,ename,sal,grade from emp,salgrade where sal between losal and hisal;
--2500這個(gè)薪資在幾等級(jí)
select grade from salgrade where 2500 between losal and hisal;
select * from salgrade;
--自連接? 一張表拓售,當(dāng)多張表使用
--查詢有上級(jí)的員工的員工信息,以及這個(gè)員工的上級(jí)信息
--數(shù)據(jù):?jiǎn)T工信息 上級(jí)信息
--來(lái)源: 員工表 emp e1? 上級(jí)表 emp e2
select * from emp e1,emp e2 where e1.mgr = e2.empno;
--外連接
--左外連接|左連接? 右外連接|右連接 :主表在逗號(hào)的左邊叫做左連接,主表在逗號(hào)的右邊叫做右連接
--主表: 主表中的數(shù)據(jù)無(wú)論是否滿足連接條件都要顯示
--查詢所有員工信息,以及這個(gè)員工的上級(jí)信息
--員工表作為主表
select * from emp e2,emp e1 where e1.mgr = e2.empno(+);--右鏈接
select * from emp e1,emp e2 where e1.mgr = e2.empno(+);--左連接
select * from emp e1,emp e2 where e1.mgr = e2.empno(+) and e1.deptno in(10,20) order by e1.empno desc;
-- 找出30部門的員工名稱及部門名稱
select ename,dname from emp e,dept d where e.deptno? = d.deptno? and? e.deptno= 30;
-- 找出 所有部門的員工數(shù) 及部門名稱
--需求:所有部門都要,部門表中所有的數(shù)據(jù)都要? 部門表作為主表存在
select dname,count(empno) from emp e,dept d where e.deptno = d.deptno group by dname;
-- 找出所有有員工的部門名稱以及員工數(shù)
select dname,count(1) from emp e,dept d where e.deptno = d.deptno group by dname;
select dname,c 人數(shù) from dept d,(select deptno,count(1) c from emp group by deptno) e where d.deptno = e.deptno;
select * from emp,dept where emp.deptno(+) = dept.deptno;
--先把每一個(gè)部門的部門編號(hào)和部門人數(shù)求出來(lái)
select deptno,count(1) from emp group by deptno;
--上面這個(gè)結(jié)果集與部門表進(jìn)行連接
select dname,nvl(c,0) 人數(shù) from dept d,(select deptno,count(1) c from emp group by deptno) e where d.deptno = e.deptno(+);
--查詢員工信息 e1,部門信息 d,薪資等級(jí) s,上級(jí)信息 e2
select dname 部門名稱, e1.ename 員工名稱, grade 薪資等級(jí), e2.ename 上級(jí)名稱
? from emp e1, dept d, salgrade s, emp e2
where e1.deptno = d.deptno
? and e1.sal between losal and hisal
? and e1.mgr = e2.empno;
-- 非等值連接 > < != <>between and
--查詢員工姓名予弧,工資及等級(jí)
--900 屬于哪個(gè)等級(jí)
select ename,sal,grade from emp e ,salgrade s where sal between losal and hisal;
select grade from salgrade where 900 between losal and hisal;
-- 自連接: 特殊的等值連接 (來(lái)自于同一張表)
----找出 存在上級(jí)的員工姓名 及上級(jí)名稱
-- 數(shù)據(jù)來(lái)源: emp e, emp m
-- 字段: e.ename, m.ename
-- 條件: e.mgr=m.empno
select e.ename,m.ename from emp e, emp m where e.mgr = m.empno;
-- 外連接
--找出 所有部門的員工數(shù) 及部門名稱
select dname,nu from dept d,(select? count(1) nu,deptno from emp group by deptno ) e? where d.deptno(+) = e.deptno;
--99語(yǔ)法
--select 數(shù)據(jù) from 數(shù)據(jù)來(lái)源1 join 數(shù)據(jù)來(lái)源2;
--笛卡爾積? 對(duì)乘? cross join? --交叉連接
? --交叉連接 cross join --->笛卡爾積
? -- 自然連接(主外鍵闷袒、同名列) natural join -->等值連接
? -- join using連接(同名列) -->等值連接
?? --[inner]join on 連接 -->等值連接 非等值 自連接 (解決一切) 關(guān)系列必須區(qū)分
? --left|right [outer] join on|using -->外連接
?
? --full join on|using -->全連接 滿足直接匹配,不滿足 相互補(bǔ)充null ,確保 所有表的記錄 都至少出現(xiàn)一次
select * from emp,dept; --92
select * from emp e cross join dept d ; --99
select * from emp natural join dept; --自然連接
select empno,deptno from emp e natural join dept d;
--注意;如果使用相同名字段 不能指明出處
select empno,deptno from emp e join dept d using(deptno);--join..using
--注意;如果使用相同名字段 不能指明出處
--非等值連接
--A join B on 連接條件 join c on 連接條件 可以做等值連接 可以做非等值連接
select * from emp e join dept d on e.deptno = d.deptno where e.deptno =30;
--注意;如果使用相同名字段 不能指明出處
--查詢員工信息,薪資等級(jí)
select grade,count(empno)
? from emp e
? join salgrade s
? ? on e.sal between losal and hisal
? join dept d
? ? on e.deptno = d.deptno
? ? where e.deptno in(10,30) group by grade
? ? having grade != 1 order by grade;
--員工信息,上級(jí)信息
select * from emp e1 join emp e2 on e1.mgr = e2.empno;
--內(nèi)連接(inner)join
--外連接 left join 左連接? right join右鏈接? 全連接full join 兩邊的表都作為主表
--主表 主表在join的左邊勒极,叫做左連接 left join? 主表在join的右邊,叫做左連接 right join
select * from emp e1 left join emp e2 on e1.mgr = e2.empno;
--視圖
--視圖是虛擬表,操作視圖其實(shí)數(shù)據(jù)還是在對(duì)應(yīng)的表中,使用視圖最大的好處可以簡(jiǎn)化select語(yǔ)句代碼
--不是所有的賬號(hào)都有權(quán)限創(chuàng)建視圖
--需要授權(quán)? 1)切換到管理員賬號(hào)sys? 2) grant dba to 賬戶名;授權(quán)? 回收權(quán)限r(nóng)evoke dba from 賬號(hào)名;
--創(chuàng)建視圖
--create or replace view 視圖名 as select語(yǔ)句 [with read only];
--drop view 視圖名; 刪除視圖
--update 表名 set 字段=值 [,....] where 過(guò)濾行記錄;
--索引
--是數(shù)據(jù)庫(kù)的對(duì)象之一,是透明的,有沒(méi)有索引,sql都一樣
--大量的數(shù)據(jù)的查詢,如果數(shù)據(jù)量比較小,如果不是大量做查詢,二十大量的執(zhí)行增刪改的操作,反而會(huì)降低效率,因?yàn)樾枰S護(hù)索引
--字典的目錄
--create index 索引名 on表名 (字段列表...)
--drop index 索引名
--表設(shè)計(jì)
--三范式的規(guī)范
--表的名稱, 字段,類型,要求(約束)
--表和表之間的關(guān)系? 一對(duì)一? ? 一對(duì)多|多對(duì)一(主外鍵)? 多對(duì)多(中間表)
--創(chuàng)建表 和添加約束
--先創(chuàng)建表,后續(xù)為這個(gè)表的什么字段添加約束
--創(chuàng)建表的同時(shí)添加約束
? ? ? --創(chuàng)建表 字段后直接添加約束? 1)默認(rèn)的約束名? ? 2)指定約束名
? ? ? --? ? ? 字段定義完成之后,統(tǒng)一為不同的字段的添加約束
--DDL 定義語(yǔ)句? ? 創(chuàng)建表 create 刪除表drop? 修改alter
--創(chuàng)建表
--create table 表名(
? ? ? --字段名 類型? 約束,
? ? ? --字段名 類型? 約束,
? ? -- 字段名 類型 ,
? ? --約束;
? ? --約束....
--)
--追加約束
--當(dāng)有主從表關(guān)系的兩張表
--1.刪除表時(shí)候
? ? ? --1)默認(rèn)先刪除從表后刪除主表? ? ? ? ? ? 2)刪除主表并同時(shí)刪除主外鍵約束 cascade constraints
--2.刪除主表中的數(shù)據(jù)的時(shí)候,
? ? ? --這個(gè)主表數(shù)據(jù)如果沒(méi)有被從表中的數(shù)據(jù)引用,可以直接刪除
? ? ? --這個(gè)主表數(shù)據(jù)如果已經(jīng)被從表中的數(shù)據(jù)引用,需要處理
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? --1)先刪除從表中引用了的數(shù)據(jù)? 再刪除主表的這個(gè)數(shù)據(jù)? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? --2) 添加外鍵約束的時(shí)候 on delete set null,當(dāng)主表數(shù)據(jù)被刪除時(shí),從表引用這條數(shù)據(jù)的外鍵字段值為null? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? --3) on delete cascade 刪除主表數(shù)據(jù)的時(shí)候級(jí)聯(lián)刪除從表中引用了的這些數(shù)據(jù)