




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、實(shí)驗(yàn)一練習(xí)1、請查詢表DEPT中所有部門的情況。select * from dept;練習(xí)2、查詢表DEPT中的部門號、部門名稱兩個(gè)字段的所有信息。select deptno,dname from dept;練習(xí)3、請從表EMP中查詢10號部門工作的雇員姓名和工資。select ename,sal from emp where deptno=10;練習(xí)4、請從表EMP中查找工種是職員CLERK或經(jīng)理MANAGER的雇員姓名、工資。select ename,sal from emp where job='CLERK' or job='MANAGER'練習(xí)5、請?jiān)贓
2、MP表中查找部門號在1030之間的雇員的姓名、部門號、工資、工作。select ename,deptno,sal,job from emp where deptno between 10 and 30;練習(xí)6、請從表EMP中查找姓名以J開頭所有雇員的姓名、工資、職位。select ename,sal,job from emp where ename like 'J%'練習(xí)7、請從表EMP中查找工資低于2000的雇員的姓名、工作、工資,并按工資降序排列。select ename,job,sal from emp where sal<=2000 order by sal de
3、sc;練習(xí)8、請從表中查詢工作是CLERK的所有人的姓名、工資、部門號、部門名稱以及部門地址的信息。select ename,sal,emp.deptno,dname,loc from emp,dept where emp.deptno=dept.deptno and job=CLERK;練習(xí)9、查詢表EMP中所有的工資大于等于2000的雇員姓名和他的經(jīng)理的名字。select a.ename,b.ename from emp a,emp b where a.mgr=b.empno(+) and a.sal>=2000;練習(xí)10、在表EMP中查詢所有工資高于JONES的所有雇員姓名、工作和
4、工資。select ename,job,sal from emp where sal>(select sal from emp where ename=JONES);練習(xí)11、列出沒有對應(yīng)部門表信息的所有雇員的姓名、工作以及部門號。select ename,job,deptno from emp where deptno not in (select deptno from dept);練習(xí)12、查找工資在10003000之間的雇員所在部門的所有人員信息select * from emp where deptno in (select distinct deptno from emp w
5、here sal between 1000 and 3000);練習(xí)13、雇員中誰的工資最高。select ename from emp where sal=(select max(sal) from emp);select ename from (select * from emp order by sal desc) where rownum<=1;*練習(xí)14、雇員中誰的工資第二高(考慮并列第一的情況,如何處理)。select ename from (select ename ,sal from (select * from emp order by sal desc) where
6、rownum<=2 order by sal) where rownum<=1;實(shí)驗(yàn)二1 查詢所有雇員的姓名、SAL與COMM之和。select ename,sal+nvl(comm,0) “sal-and-comm” from emp;2 查詢所有81年7月1日以前來的員工姓名、工資、所屬部門的名字select ename,sal,dname from emp,dept where emp.deptno=dept.deptno and hiredate<=to_date(1981-07-01,yyyy-mm-dd);3 查詢各部門中81年1月1日以后來的員工數(shù)select
7、deptno,count(*) from emp where hiredate>=to_date(1981-01-01,yyyy-mm-dd) group by deptno;4 查詢所有在CHICAGO工作的經(jīng)理MANAGER和銷售員SALESMAN的姓名、工資select ename,sal from emp where (job=MANAGER or job=SALES) and deptno in (select deptno from dept where loc=CHICAGO);5 查詢列出來公司就職時(shí)間超過24年的員工名單select ename from emp whe
8、re hiredate<=add_months(sysdate,-288);6 查詢于81年來公司所有員工的總收入(SAL和COMM)select sum(sal+nvl(comm,0) from emp where to_char(hiredate,yyyy)=1981;7 查詢顯示每個(gè)雇員加入公司的準(zhǔn)確時(shí)間,按××××年××月××日 時(shí)分秒顯示。select ename,to_char(hiredate,'yyyy-mm-dd hh24:mi:ss') from emp;8 查詢公司中按年
9、份月份統(tǒng)計(jì)各地的錄用職工數(shù)量select to_char(hiredate,'yyyy-mm'),loc,count(*) from emp,dept where emp.deptno=dept.deptno group by to_char(hiredate,'yyyy-mm'),loc;9 查詢列出各部門的部門名和部門經(jīng)理名字select dname,ename from emp,dept where emp.deptno=dept.deptno and job=MANAGER;10 查詢部門平均工資最高的部門名稱和最低的部門名稱select dname f
10、rom dept where deptno=(select deptno from (select deptno from emp group by deptno order by avg(sal) ) where rownum<=1) union all select dname from dept where deptno=(select deptno from (select deptno from emp group by deptno order by avg(sal) desc ) where rownum<=1);11 *查詢與雇員號為7521員工的最接近的在其后進(jìn)入
11、公司的員工姓名及其所在部門名select ename,dname from (select ename,deptno from (select ename,deptno from emp where hiredate>(select hiredate from emp where empno=7521) order by hiredate ) where rownum<=1) e,dept where e.deptno=dept.deptno實(shí)驗(yàn)三、1 建立一個(gè)表(表名自定),表結(jié)構(gòu)與EMP相同,沒有任何記錄。create table my_emp as select * from
12、 emp;2 用INSERT語句輸入5條記錄,并提交。3 擴(kuò)大該表的記錄數(shù)到約40條,并使雇員號不重復(fù);每個(gè)雇員都有所屬部門,雇員在同一部門的經(jīng)理是同一人。insert .update commit4 建立一個(gè)與DEPT表結(jié)構(gòu)和記錄完全相同的新表,并與前項(xiàng)新表建立參照完整性約束。alter table my_dept add( constraint s1 primary key(deptno);alter table my_emp add(constraint s2 foreign key(deptno) references dept(deptno);5 對在NEW YORK工作的雇員加工資
13、,每人加200。6 *如果雇員姓名與部門名稱中有一個(gè)或一個(gè)以上相同的字母,則該雇員的COMM增加500。update my_emp a set comm=NVL(comm,0)+500 where a.ename<>( select translate(a.ename,b.dname,CHR(27) from my_dept b where b.deptno=a.deptno );-a.deptno與b.deptno必須有主外鍵連接,否則可能出錯(cuò),為什么?commit;7 刪除部門號為30的記錄,并刪除該部門的所有成員。delete from emp where deptno=30
14、;delete from dept where deptno=30;commit8 新增列性別SEX,字符型。alter table emp add(sex char(2);9 修改新雇員表中的MGR列,為字符型。該列數(shù)據(jù)必須為空alter table emp modify(mgr varchar2(20);10 試著去刪除新表中的一個(gè)列。alter table my_emp drop (comm);實(shí)驗(yàn)四、1 查詢部門號為30的所有人員的管理層次圖。select level,ename from emp connect by mgr=prior empno start with deptno
15、=30 and job='MANAGER'2 查詢員工SMITH的各個(gè)層次領(lǐng)導(dǎo)。select level,ename from emp connect by prior mgr= empno start with ENAME='SMITH'3 查詢顯示EMP表各雇員的工作類型,并翻譯為中文顯示用decode函數(shù)4 *查詢顯示雇員進(jìn)入公司當(dāng)年是什么屬相年(不考慮農(nóng)歷的年份算法)用decode函數(shù)5 建立一個(gè)視圖myV_emp,視圖包括myEMP表的empno、ename、sal,并按sal從大到小排列。create view myV_EMP as select e
16、mpno,ename,sal from emp;6 定義一個(gè)mySeq,對select mySeq.nextval,my_emp.* from my_emp的執(zhí)行結(jié)果進(jìn)行說明。7 定義序列mySeq、myEMP、myV_emp的同義詞,能否用同義詞對上述對象進(jìn)行訪問。8 在myEMP表中建立ename的唯一性索引。9 如何在sql*plus中,運(yùn)行sql的腳本(即后綴為.sql的文件)實(shí)驗(yàn)五、1 觀察下列PL/SQL的執(zhí)行結(jié)果declare s emp%rowtype;begin select * into s from emp where ename='KING'DBMS_O
17、UTPUT.PUT_LINE(s.empno|s.ename|s.job|s.sal);END;2 編寫一個(gè)PL/SQL,顯示ASC碼值從32至120的字符。begin for i in 32.120 loop dbms_output.put_line(chr(i); end loop;end;3 計(jì)算myEMP表中COMM最高與最低的差值,COMM值為空時(shí)按0計(jì)算。declare var1 number; var2 number; val_comm number; begin select max(nvl(comm,0) into var1 from myemp; select min(nv
18、l(comm,0) into var2 from myemp; val_comm:=var1-var2; dbms_output.put_line(val_comm);end;4 根據(jù)表myEMP中deptno字段的值,為姓名為JONES的雇員修改工資;若部門號為10,則工資加100;部門號為20,加200;其他部門加400。declare c1 number; c2 number;begin select deptno into c1 from emp where ename=JONES; if c1=10 then c2:=100;elsif c1=20 then c2:=200;else
19、 c2:=400; end if; update emp set sal=sal+c2 where ename=JONES;commit;end;5 計(jì)算顯示部門人數(shù)最多的部門號、人數(shù)、工資總和,以及部門人數(shù)最少的部門號、人數(shù)、工資總和。6 計(jì)算myEMP中所有雇員的所得稅總和。假設(shè)所得稅為累進(jìn)稅率,所得稅算法為:工資收入為01000為免稅;收入10002000者,超過1000的部分稅率10;20003000者超過2000部分按20稅率計(jì)算;30004000者超過3000部分按30稅率計(jì)算;4000以上收入,超過4000部分按40稅率計(jì)算。(請查閱累進(jìn)稅率的概念)declare sum_xx
20、number:=0; xx number;begin-計(jì)算收入為10002000的所得稅總額select sum(sal-1000)*0.1) into xx from emp where sal >1000 and sal<=2000;sum_xx:=sum_xx+xx;-計(jì)算收入為20003000的所得稅總額select sum(sal-2000)*0.2+100) into xx from emp where sal >2000 and sal<=3000;sum_xx:=sum_xx+xx;-計(jì)算收入為30004000的所得稅總額select sum(sal-3
21、000)*0.3+300) into xx from emp where sal >3000 and sal<=4000;sum_xx:=sum_xx+xx;-計(jì)算收入為4000以上的所得稅總額select sum(sal-4000)*0.4+600) into xx from emp where sal >4000;sum_xx:=sum_xx+xx;dbms_output.put_line(sum_xx);end;7 *(可選做,難題)假設(shè)有個(gè)表如myEMP,未建立主鍵,含有多條記錄重復(fù)(列值完全相同),試編制一個(gè)PL/SQL,將多余的重復(fù)記錄刪除。實(shí)驗(yàn)六、1 用外部變量
22、,實(shí)現(xiàn)兩個(gè)PL/SQL程序間的數(shù)據(jù)交換。SQL> variable a1 number;SQL> begin 2 :a1:=1000; 3 end; 4 /PL/SQL 過程已成功完成。SQL> begin 2 dbms_output.put_line(:a1); 3 end; 4 /1000PL/SQL 過程已成功完成。2 插入myEMP表中的數(shù)據(jù)記錄,考慮可能出現(xiàn)的例外,并提示。主要的例外提示:唯一性索引值重復(fù)DUP_VAL_ON_INDEX3 刪除myDEPT表中的數(shù)據(jù)記錄一條,考慮例外情況,并提示。主要的例外提示:違反完整約束條件4 將下列PL/SQL改為FOR游標(biāo)d
23、eclare cursor cur_myemp is select * from emp; r emp%rowtype; begin open cur_myemp; fetch cur_myemp into r; while cur_myemp%found loop dbms_output.put_line(r.ename); fetch cur_myemp into r; end loop; close cur_myemp; end;5 工資級別的表salgrade,列出各工資級別的人數(shù)。(用游標(biāo)來完成)declarev1 number;cursor cur1 is select * fro
24、m salgrade;begin for c1 in cur1 loop select count(*) into v1 from emp where sal between c1.losal and c1.hisal; dbms_output.put_line('grade'|c1.grade|' '|v1); end loop;end;實(shí)驗(yàn)七、1 在myEMP表中增加一個(gè)字段,字段名為EMPPASS,類型為可變長字符。2 建立一個(gè)存儲過程,用于操作用戶登錄的校驗(yàn),登錄需要使用EMPNO和EMPPASS,并需要提示登錄中的錯(cuò)誤,如是EMPNO不存在,還是EMP
25、NO存在而是EMPPASS錯(cuò)誤等。create or replace procedure p_login( in_empno in emp.empno%type, in_emppass in emp.emppass%type, out_code out number, out_desc out varchar2)isx1 emp.ename%type;x2 number;beginselect ename into x1 from emp where empno=in_empno;select count(*) into x2 from emp where empno=in_empno and
26、 emppass=in_emppass;if x2=1 thenout_code:=0;out_desc:=x1;else out_code:=2; out_desc:=用戶登陸密碼錯(cuò)誤!; end if;exception when NO_DATA_FOUND then out_code:=1; out_desc:=該用戶號存在!; when TOO_MANY_ROWS then out_code:=3; out_desc:=該用戶號有重復(fù)值!; when others thenout_code:=100; out_desc:=其他錯(cuò)誤!; end;3 建立一個(gè)存儲過程,實(shí)現(xiàn)myEMP表中指
27、定雇員的EMPPASS字段的修改,修改前必須進(jìn)行EMPPASS舊值的核對。CREATE OR REPLACE PROCEDURE P_CHANGEPASS( IN_EMPNO IN EMP.EMPNO%TYPE, IN_OLDPASS IN EMP.EMPPASS%TYPE, IN_NEWPASS IN EMP.EMPPASS%TYPE, OUT_CODE OUT NUMBER, OUT_DESC OUT VARCHAR2) IS X1 NUMBER; BEGIN SELECT COUNT(*) INTO X1 FROM EMP WHERE EMPNO=IN_EMPNO AND EMPPASS
28、=IN_OLDPASS; IF X1=1 THEN update emp set emppass=in_newpass where empno=in_empno; commit; OUT_CODE:=0; OUT_DESC:=修改口令成功; ELSE OUT_CODE:=1; OUT_DESC:=修改口令不成功; END IF; exception when others then out_code:=100; out_desc:=其他錯(cuò)誤; END;4 建立一個(gè)函數(shù),輸入一個(gè)雇員號,返回該雇員的所在同一部門的最高級別上司姓名。create or replace function f_lead
29、er(in_empno in emp.empno%type) return varchar2isv1 number;v2 number;v3 emp.ename%type;v4 emp.deptno%type;beginv1:=in_empno;v3:='未找到'select deptno into v4 from emp where empno=v1;loopselect mgr into v2 from emp where empno=v1;select ename into v3 from emp where empno=v2 and deptno=v4;v1:=v2;e
30、nd loop;exceptionwhen others then return v3;end;5 試用上題函數(shù),實(shí)現(xiàn)各雇員的同一部門最高級別上司的SELECT查詢。select f_leader(7521) from dual;6 *編寫實(shí)驗(yàn)五中第六題,關(guān)于各雇員工資的所得稅計(jì)算函數(shù)實(shí)驗(yàn)八、1 建立一個(gè)觸發(fā)器,當(dāng)myEMP表中部門號存在時(shí),該部門不允許刪除。create or replace trigger dept_line_deletebefore delete on dept for each rowdeclarev1 number;begin select count(*) into
31、 v1 from emp where deptno=:old.deptno; if v1>=1 then RAISE_APPLICATION_ERROR(-20000,錯(cuò)誤); end if;end;實(shí)驗(yàn)九、1 建立一個(gè)示例包emp_mgmt中,新增一個(gè)修改雇員所在部門的過程。create or replace package emp_mgmt asprocedure change_dept(in_newdept in emp.deptno%type,out_code out number,out_desc out varchar2);mgmt_empno emp.empno%type;
32、procedure mgmt_login(in_empno in emp.empno%type,in_emppass in emp.emppass%type,out_code out number,out_desc out varchar2);end;create or replace package body emp_mgmt asprocedure change_dept(in_newdept in emp.deptno%type,out_code out number,out_desc out varchar2)isbegin update emp set deptno=in_newdept where empno=mgmt_empno; commit; out_code:=0; out_desc:=ok;end;procedure mgmt_login( in_empno in emp.empno%ty
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中國FM自動(dòng)選臺收音機(jī)望遠(yuǎn)鏡數(shù)據(jù)監(jiān)測研究報(bào)告
- 2025年中國BOPP鍍鋁膜數(shù)據(jù)監(jiān)測研究報(bào)告
- 2025年中國2、5-二甲基苯胺數(shù)據(jù)監(jiān)測報(bào)告
- 2025至2030年中國集中操作式電梯井筒模市場分析及競爭策略研究報(bào)告
- 2025至2030年中國鋁合金吸頂燈市場分析及競爭策略研究報(bào)告
- 2025至2030年中國茴香粉市場分析及競爭策略研究報(bào)告
- 2025至2030年中國砼攪拌站市場分析及競爭策略研究報(bào)告
- 2025至2030年中國電力控制開關(guān)柜市場分析及競爭策略研究報(bào)告
- 2025至2030年中國深溝外球面球軸承市場分析及競爭策略研究報(bào)告
- 2025至2030年中國汽車儲液器支架市場分析及競爭策略研究報(bào)告
- CLSI EP25-A 穩(wěn)定性考察研究
- SJG 44-2018 深圳市公共建筑節(jié)能設(shè)計(jì)規(guī)范-高清現(xiàn)行
- 職工子女暑期工會(huì)愛心托管班的方案通知
- (5年高職)客戶服務(wù)實(shí)務(wù)(第二版)教學(xué)課件全套電子教案匯總整本書課件最全教學(xué)教程完整版教案(最新)
- 精品中文版b4a新手指南第4章開發(fā)環(huán)境
- 兒科患兒及家屬的溝通技巧
- 光纜線路的故障分析及障礙搶修
- 童聲合唱訓(xùn)練講座
- (防火閥)檢驗(yàn)報(bào)告
- 輸變電工程建設(shè)管理程序指南(共61頁)
- 《屏蔽泵培訓(xùn)講義》
評論
0/150
提交評論