oracle練習-答案_第1頁
oracle練習-答案_第2頁
oracle練習-答案_第3頁
oracle練習-答案_第4頁
oracle練習-答案_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、實驗一練習1、請查詢表DEPT中所有部門的情況。select * from dept;練習2、查詢表DEPT中的部門號、部門名稱兩個字段的所有信息。select deptno,dname from dept;練習3、請從表EMP中查詢10號部門工作的雇員姓名和工資。select ename,sal from emp where deptno=10;練習4、請從表EMP中查找工種是職員CLERK或經(jīng)理MANAGER的雇員姓名、工資。select ename,sal from emp where job='CLERK' or job='MANAGER'練習5、請在E

2、MP表中查找部門號在1030之間的雇員的姓名、部門號、工資、工作。select ename,deptno,sal,job from emp where deptno between 10 and 30;練習6、請從表EMP中查找姓名以J開頭所有雇員的姓名、工資、職位。select ename,sal,job from emp where ename like 'J%'練習7、請從表EMP中查找工資低于2000的雇員的姓名、工作、工資,并按工資降序排列。select ename,job,sal from emp where sal<=2000 order by sal de

3、sc;練習8、請從表中查詢工作是CLERK的所有人的姓名、工資、部門號、部門名稱以及部門地址的信息。select ename,sal,emp.deptno,dname,loc from emp,dept where emp.deptno=dept.deptno and job=CLERK;練習9、查詢表EMP中所有的工資大于等于2000的雇員姓名和他的經(jīng)理的名字。select a.ename,b.ename from emp a,emp b where a.mgr=b.empno(+) and a.sal>=2000;練習10、在表EMP中查詢所有工資高于JONES的所有雇員姓名、工作和

4、工資。select ename,job,sal from emp where sal>(select sal from emp where ename=JONES);練習11、列出沒有對應部門表信息的所有雇員的姓名、工作以及部門號。select ename,job,deptno from emp where deptno not in (select deptno from dept);練習12、查找工資在10003000之間的雇員所在部門的所有人員信息select * from emp where deptno in (select distinct deptno from emp w

5、here sal between 1000 and 3000);練習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;*練習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;實驗二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 查詢列出來公司就職時間超過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 查詢顯示每個雇員加入公司的準確時間,按××××年××月××日 時分秒顯示。select ename,to_char(hiredate,'yyyy-mm-dd hh24:mi:ss') from emp;8 查詢公司中按年

9、份月份統(tǒng)計各地的錄用職工數(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員工的最接近的在其后進入

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實驗三、1 建立一個表(表名自定),表結構與EMP相同,沒有任何記錄。create table my_emp as select * from

12、 emp;2 用INSERT語句輸入5條記錄,并提交。3 擴大該表的記錄數(shù)到約40條,并使雇員號不重復;每個雇員都有所屬部門,雇員在同一部門的經(jīng)理是同一人。insert .update commit4 建立一個與DEPT表結構和記錄完全相同的新表,并與前項新表建立參照完整性約束。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 *如果雇員姓名與部門名稱中有一個或一個以上相同的字母,則該雇員的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必須有主外鍵連接,否則可能出錯,為什么?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 試著去刪除新表中的一個列。alter table my_emp drop (comm);實驗四、1 查詢部門號為30的所有人員的管理層次圖。select level,ename from emp connect by mgr=prior empno start with deptno

15、=30 and job='MANAGER'2 查詢員工SMITH的各個層次領導。select level,ename from emp connect by prior mgr= empno start with ENAME='SMITH'3 查詢顯示EMP表各雇員的工作類型,并翻譯為中文顯示用decode函數(shù)4 *查詢顯示雇員進入公司當年是什么屬相年(不考慮農歷的年份算法)用decode函數(shù)5 建立一個視圖myV_emp,視圖包括myEMP表的empno、ename、sal,并按sal從大到小排列。create view myV_EMP as select e

16、mpno,ename,sal from emp;6 定義一個mySeq,對select mySeq.nextval,my_emp.* from my_emp的執(zhí)行結果進行說明。7 定義序列mySeq、myEMP、myV_emp的同義詞,能否用同義詞對上述對象進行訪問。8 在myEMP表中建立ename的唯一性索引。9 如何在sql*plus中,運行sql的腳本(即后綴為.sql的文件)實驗五、1 觀察下列PL/SQL的執(zhí)行結果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 編寫一個PL/SQL,顯示ASC碼值從32至120的字符。begin for i in 32.120 loop dbms_output.put_line(chr(i); end loop;end;3 計算myEMP表中COMM最高與最低的差值,COMM值為空時按0計算。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 計算顯示部門人數(shù)最多的部門號、人數(shù)、工資總和,以及部門人數(shù)最少的部門號、人數(shù)、工資總和。6 計算myEMP中所有雇員的所得稅總和。假設所得稅為累進稅率,所得稅算法為:工資收入為01000為免稅;收入10002000者,超過1000的部分稅率10;20003000者超過2000部分按20稅率計算;30004000者超過3000部分按30稅率計算;4000以上收入,超過4000部分按40稅率計算。(請查閱累進稅率的概念)declare sum_xx

20、number:=0; xx number;begin-計算收入為10002000的所得稅總額select sum(sal-1000)*0.1) into xx from emp where sal >1000 and sal<=2000;sum_xx:=sum_xx+xx;-計算收入為20003000的所得稅總額select sum(sal-2000)*0.2+100) into xx from emp where sal >2000 and sal<=3000;sum_xx:=sum_xx+xx;-計算收入為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;-計算收入為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 *(可選做,難題)假設有個表如myEMP,未建立主鍵,含有多條記錄重復(列值完全相同),試編制一個PL/SQL,將多余的重復記錄刪除。實驗六、1 用外部變量

22、,實現(xiàn)兩個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)的例外,并提示。主要的例外提示:唯一性索引值重復DUP_VAL_ON_INDEX3 刪除myDEPT表中的數(shù)據(jù)記錄一條,考慮例外情況,并提示。主要的例外提示:違反完整約束條件4 將下列PL/SQL改為FOR游標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ù)。(用游標來完成)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;實驗七、1 在myEMP表中增加一個字段,字段名為EMPPASS,類型為可變長字符。2 建立一個存儲過程,用于操作用戶登錄的校驗,登錄需要使用EMPNO和EMPPASS,并需要提示登錄中的錯誤,如是EMPNO不存在,還是EMP

25、NO存在而是EMPPASS錯誤等。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:=用戶登陸密碼錯誤!; end if;exception when NO_DATA_FOUND then out_code:=1; out_desc:=該用戶號存在!; when TOO_MANY_ROWS then out_code:=3; out_desc:=該用戶號有重復值!; when others thenout_code:=100; out_desc:=其他錯誤!; end;3 建立一個存儲過程,實現(xiàn)myEMP表中指

27、定雇員的EMPPASS字段的修改,修改前必須進行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:=其他錯誤; END;4 建立一個函數(shù),輸入一個雇員號,返回該雇員的所在同一部門的最高級別上司姓名。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ù),實現(xiàn)各雇員的同一部門最高級別上司的SELECT查詢。select f_leader(7521) from dual;6 *編寫實驗五中第六題,關于各雇員工資的所得稅計算函數(shù)實驗八、1 建立一個觸發(fā)器,當myEMP表中部門號存在時,該部門不允許刪除。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,錯誤); end if;end;實驗九、1 建立一個示例包emp_mgmt中,新增一個修改雇員所在部門的過程。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)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論