![oracle_sql練習(xí)題及答案_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/2/57af2c59-5fe0-4520-ab22-780e77bf2471/57af2c59-5fe0-4520-ab22-780e77bf24711.gif)
![oracle_sql練習(xí)題及答案_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/2/57af2c59-5fe0-4520-ab22-780e77bf2471/57af2c59-5fe0-4520-ab22-780e77bf24712.gif)
![oracle_sql練習(xí)題及答案_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/2/57af2c59-5fe0-4520-ab22-780e77bf2471/57af2c59-5fe0-4520-ab22-780e77bf24713.gif)
![oracle_sql練習(xí)題及答案_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/2/57af2c59-5fe0-4520-ab22-780e77bf2471/57af2c59-5fe0-4520-ab22-780e77bf24714.gif)
![oracle_sql練習(xí)題及答案_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/2/57af2c59-5fe0-4520-ab22-780e77bf2471/57af2c59-5fe0-4520-ab22-780e77bf24715.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、Oracle_sql練習(xí)題及答案2012年11月20日來源:福星寶實驗一練習(xí)1、請查詢表DEPT中所有部門的情況。select * from dept;練習(xí)2、查詢表DEPT中的部門號、部門名稱兩個字段的所有信息。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
2、9; or job='MANAGER'練習(xí)5、請在EMP表中查找部門號在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 em
3、p where sal<=2000 order by sal desc;練習(xí)8、請從表中查詢工作是CLERK的所有人的姓名、工資、部門號、部門名稱以及部門地址的信息。 select ename,sal,emp.deptno,dname,loc from emp,dept whereemp.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
4、;練習(xí)10、在表EMP中查詢所有工資高于JONES的所有雇員姓名、工作和工資。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 i
5、n (select distinct deptno from emp where 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
6、* from emp order by sal desc) where 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-
7、mm-dd);3 查詢各部門中81年1月1日以后來的員工數(shù)select deptno,count(*) from emp wherehiredate>=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 查詢列出來公司就職時間
8、超過24年的員工名單select ename from emp where hiredate<=add_months(sysdate,-288);6 查詢于81年來公司所有員工的總收入(SAL和COMM)select sum(sal+nvl(comm,0) from emp where to_char(hiredate,yyyy)=1981;7 查詢顯示每個雇員加入公司的準(zhǔn)確時間,按××××年××月××日 時分秒顯示。select ename,to_char(hiredate,'yyyy-mm-dd
9、hh24:mi:ss') from emp;8 查詢公司中按年份月份統(tǒng)計各地的錄用職工數(shù)量select to_char(hiredate,'yyyy-mm'),loc,count(*) from emp,deptwhere 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 查詢部
10、門平均工資最高的部門名稱和最低的部門名稱select dname from 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&l
11、t;=1);11 *查詢與雇員號為7521員工的最接近的在其后進(jìn)入公司的員工姓名及其所在部門名select ename,dnamefrom (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,deptwhere e.deptno=dept.deptno實驗三、1 建立一個表(表名自定),表結(jié)構(gòu)與EMP相同,沒有任何記錄。create
12、 table my_emp as select * from emp;2 用Insert語句輸入5條記錄,并提交。3 擴大該表的記錄數(shù)到約40條,并使雇員號不重復(fù);每個雇員都有所屬部門,雇員在同一部門的經(jīng)理是同一人。insert .update commit4 建立一個與DEPT表結(jié)構(gòu)和記錄完全相同的新表,并與前項新表建立參照完整性約束。 alter table my_dept add( constraint s1 primary key(deptno);alter table my_emp add(constraint s2 foreign key(deptno) referencesdep
13、t(deptno);5 對在NEW YORK工作的雇員加工資,每人加200。6 *如果雇員姓名與部門名稱中有一個或一個以上相同的字母,則該雇員的COMM增加500。 update my_emp aset comm=NVL(comm,0)+500where 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的記錄,并刪除該部門的所有成員。delet
14、e from emp where deptno=30;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 empconnect by mgr=prio
15、r empnostart with deptno=30 and job='MANAGER'2 查詢員工SMITH的各個層次領(lǐng)導(dǎo)。select level,ename from empconnect by prior mgr= empnostart with ENAME='SMITH'3 查詢顯示EMP表各雇員的工作類型,并翻譯為中文顯示用decode函數(shù)4 *查詢顯示雇員進(jìn)入公司當(dāng)年是什么屬相年(不考慮農(nóng)歷的年份算法)用decode函數(shù)5 建立一個視圖myV_emp,視圖包括myEMP表的empno、ename、sal,并按sal從大到小排列。 create v
16、iew myV_EMP as select empno,ename,sal from emp;6 定義一個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中,運行sql的腳本(即后綴為.sql的文件)實驗五、1 觀察下列PL/SQL的執(zhí)行結(jié)果declares emp%rowtype;beginselect * into sfrom empwhere ename=&
17、#39;KING'DBMS_OUTPUT.PUT_LINE(s.empno|s.ename|s.job|s.sal);END;2 編寫一個PL/SQL,顯示ASC碼值從32至120的字符。beginfor i in 32.120loopdbms_output.put_line(chr(i);end loop;end;3 計算myEMP表中COMM最高與最低的差值,COMM值為空時按0計算。declarevar1 number;var2 number;val_comm number;beginselect max(nvl(comm,0) into var1 from myemp;sele
18、ct min(nvl(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。declarec1 number;c2 number;beginselect deptno into c1 from emp where ename=JONES;if c1=10 thenc2:=100;elsif c1=20 thenc2:=200;els
19、e c2:=400;end if;update emp set sal=sal+c2 where ename=JONES;commit;end;5 計算顯示部門人數(shù)最多的部門號、人數(shù)、工資總和,以及部門人數(shù)最少的部門號、人數(shù)、工資總和。6 計算myEMP中所有雇員的所得稅總和。假設(shè)所得稅為累進(jìn)稅率,所得稅算法為:工資收入為01000為免稅;收入10002000者,超過1000的部分稅率10;20003000者超過2000部分按20稅率計算;30004000者超過3000部分按30稅率計算;4000以上收入,超過4000部分按40稅率計算。(請查閱累進(jìn)稅率的概念)declaresum_xx nu
20、mber:=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-30
21、00)*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 *(可選做,難題)假設(shè)有個表如myEMP,未建立主鍵,含有多條記錄重復(fù)(列值完全相同),試編制一個PL/SQL,將多余的重復(fù)記錄刪除。實驗六、1 用外部變量
22、,實現(xiàn)兩個PL/SQL程序間的數(shù)據(jù)交換。SQL> variable a1 number;SQL> begin2 :a1:=1000;3 end;4 /PL/SQL 過程已成功完成。SQL> begin2 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)declare
23、cursor cur_myemp is select * from emp;r emp%rowtype;beginopen cur_myemp;fetch cur_myemp into r;while cur_myemp%foundloopdbms_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 * from salgrade;beginfo
24、r c1 in cur1loopselect 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不存在,還是EMPNO存在而是EMPPASS錯誤等。creat
25、e 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 emppass=in_emppass; if x2
26、=1 thenout_code:=0;out_desc:=x1;elseout_code:=2;out_desc:=用戶登陸密碼錯誤!;end if;exceptionwhen NO_DATA_FOUND thenout_code:=1;out_desc:=該用戶號存在!;when TOO_MANY_ROWS thenout_code:=3;out_desc:=該用戶號有重復(fù)值!;when others thenout_code:=100;out_desc:=其他錯誤!;end;3 建立一個存儲過程,實現(xiàn)myEMP表中指定雇員的EMPPASS字段的修改,修改前必須進(jìn)行EMPPASS舊值的核對。
27、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)ISX1 NUMBER;BEGINSelect COUNT(*) INTO X1 FROM EMP Where EMPNO=IN_EMPNO AND EMPPASS=IN_OLDPASS; IF X1=1 THENupdate emp set emppas
28、s=in_newpass where empno=in_empno;commit;OUT_CODE:=0;OUT_DESC:=修改口令成功;ELSEOUT_CODE:=1;OUT_DESC:=修改口令不成功;END IF;exceptionwhen others thenout_code:=100;out_desc:=其他錯誤;END;4 建立一個函數(shù),輸入一個雇員號,返回該雇員的所在同一部門的最高級別上司姓名。create or replace function f_leader(in_empno in emp.empno%type) return varchar2isv1 number;v
29、2 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;end loop;exceptionwhen others thenreturn v3;end;5 試用上題函數(shù),實現(xiàn)
30、各雇員的同一部門最高級別上司的Select查詢。 select f_leader(7521) from dual;6 *編寫實驗五中第六題,關(guān)于各雇員工資的所得稅計算函數(shù)實驗八、1 建立一個觸發(fā)器,當(dāng)myEMP表中部門號存在時,該部門不允許刪除。 create or replace trigger dept_line_deletebefore delete on dept for each rowdeclarev1 number;beginselect count(*) into v1 from emp where deptno=:old.deptno; if v1>=1 then RA
31、ISE_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;procedure mgmt_login(in_empno in emp.empno%type,in_emppas
32、s 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)isbeginupdate emp set deptno=in_newdept where empno=mgmt_empno;commit;out_code:=0;out_desc:=ok;end;procedure mgmt_login(in_empno in emp.empn
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(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)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 煤炭購銷合同擔(dān)保書
- 代購合作協(xié)議合同
- 江蘇商品房買賣合同模板
- 合同書電子版
- 安裝合同簡單版樣本
- 2025年人教A版九年級歷史下冊月考試卷含答案
- 2025年外研銜接版九年級歷史下冊階段測試試卷
- 2025年滬教版必修2歷史上冊月考試卷含答案
- 2025年湘教版九年級地理下冊月考試卷
- 客車交通安全培訓(xùn)課件
- 藝術(shù)培訓(xùn)校長述職報告
- ICU新進(jìn)人員入科培訓(xùn)-ICU常規(guī)監(jiān)護(hù)與治療課件
- 人教版一年數(shù)學(xué)下冊全冊分層作業(yè)設(shè)計
- 選擇性必修一 期末綜合測試(二)(解析版)2021-2022學(xué)年人教版(2019)高二數(shù)學(xué)選修一
- 學(xué)校制度改進(jìn)
- 各行業(yè)智能客服占比分析報告
- 年產(chǎn)30萬噸高鈦渣生產(chǎn)線技改擴建項目環(huán)評報告公示
- 民謠酒吧項目創(chuàng)業(yè)計劃書
- 2023年珠海市招考合同制職員筆試參考題庫(共500題)答案詳解版
- 心電監(jiān)護(hù)考核標(biāo)準(zhǔn)
評論
0/150
提交評論