ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:21.22KB ,
资源ID:8969988      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-8969988.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(oracle练习及答案.docx)为本站会员(b****8)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

oracle练习及答案.docx

1、oracle练习及答案实验一练习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或经理MANAGER的雇员姓名、工资。select ename,sal from emp where job=CLERK or job=MANAGER;练习5、请在EMP表中查找部门

2、号在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;练习10、在表EMP中查询所有工资高于JONES的所有雇员姓名、工作和工

3、资。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 where

4、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 rownum=2

5、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-01-01,yyyy-mm-dd) group by deptno;4 查询所有在CHICAGO工作的经理MANAGER和销售员SALE

6、SMAN的姓名、工资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 where hiredate=add_months(sysdate,-288);6 查询于81年来公司所有员工的总收入(SAL和COMM)select sum(sal+nvl(comm,0) from emp where to_char(hiredate,

7、yyyy)=1981;7 查询显示每个雇员加入公司的准确时间,按年月日 时分秒显示。select ename,to_char(hiredate,yyyy-mm-dd hh24:mi:ss) from emp;8 查询公司中按年份月份统计各地的录用职工数量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 查询列出各部门的部门名和部门经理名字select dname,ename from e

8、mp,dept where emp.deptno=dept.deptno and job=MANAGER;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

9、 group by deptno order by avg(sal) desc ) where rownum(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 emp;2 用INSERT语句输入5条记录,并提交。3 扩大该表的记录数到约40条,并使雇员号不重复;每个雇员都有所属部门

10、,雇员在同一部门的经理是同一人。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工作的雇员加工资,每人加200。6 *如果雇员姓名与部门名称中有一个或一个以上相同的字母,则该雇员的COMM增加500。update my_

11、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;delete from dept where deptno=30;commit8 新增列性别SEX,字符型。alter table emp

12、 add(sex char(2);9 修改新雇员表中的MGR列,为字符型。该列数据必须为空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=30 and job=MANAGER;2 查询员工SMITH的各个层次领导。select level,ename from emp con

13、nect by prior mgr= empno start with ENAME=SMITH;3 查询显示EMP表各雇员的工作类型,并翻译为中文显示用decode函数4 *查询显示雇员进入公司当年是什么属相年(不考虑农历的年份算法)用decode函数5 建立一个视图myV_emp,视图包括myEMP表的empno、ename、sal,并按sal从大到小排列。create view myV_EMP as select empno,ename,sal from emp;6 定义一个mySeq,对select mySeq.nextval,my_emp.* from my_emp的执行结果进行说明。

14、7 定义序列mySeq、myEMP、myV_emp的同义词,能否用同义词对上述对象进行访问。8 在myEMP表中建立ename的唯一性索引。9 如何在sql*plus中,运行sql的脚本(即后缀为.sql的文件)实验五、1 观察下列PL/SQL的执行结果declare s emp%rowtype;begin select * into s from emp where ename=KING;DBMS_OUTPUT.PUT_LINE(s.empno|s.ename|s.job|s.sal);END;2 编写一个PL/SQL,显示ASC码值从32至120的字符。begin for i in 32.

15、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(nvl(comm,0) into var2 from myemp; val_comm:=var1-var2; dbms_output.put_line(val_comm);end;4 根据表myEM

16、P中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 c2:=400; end if; update emp set sal=sal+c2 where ename=JONES;commit;end;5 计算显示部门人数最多的部门号、人数、工资总和

17、,以及部门人数最少的部门号、人数、工资总和。6 计算myEMP中所有雇员的所得税总和。假设所得税为累进税率,所得税算法为:工资收入为01000为免税;收入10002000者,超过1000的部分税率10;20003000者超过2000部分按20税率计算;30004000者超过3000部分按30税率计算;4000以上收入,超过4000部分按40税率计算。(请查阅累进税率的概念)declare sum_xx number:=0; xx number;begin-计算收入为10002000的所得税总额select sum(sal-1000)*0.1) into xx from emp where sa

18、l 1000 and sal2000 and sal3000 and sal4000;sum_xx:=sum_xx+xx;dbms_output.put_line(sum_xx);end;7 *(可选做,难题)假设有个表如myEMP,未建立主键,含有多条记录重复(列值完全相同),试编制一个PL/SQL,将多余的重复记录删除。实验六、1 用外部变量,实现两个PL/SQL程序间的数据交换。SQL variable a1 number;SQL begin 2 :a1:=1000; 3 end; 4 /PL/SQL 过程已成功完成。SQL begin 2 dbms_output.put_line(:a

19、1); 3 end; 4 /1000PL/SQL 过程已成功完成。2 插入myEMP表中的数据记录,考虑可能出现的例外,并提示。主要的例外提示:唯一性索引值重复DUP_VAL_ON_INDEX3 删除myDEPT表中的数据记录一条,考虑例外情况,并提示。主要的例外提示:违反完整约束条件4 将下列PL/SQL改为FOR游标declare 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

20、_output.put_line(r.ename); fetch cur_myemp into r; end loop; close cur_myemp; end;5 工资级别的表salgrade,列出各工资级别的人数。(用游标来完成)declarev1 number;cursor cur1 is select * from 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|

21、c1.grade| |v1); end loop;end;实验七、1 在myEMP表中增加一个字段,字段名为EMPPASS,类型为可变长字符。2 建立一个存储过程,用于操作用户登录的校验,登录需要使用EMPNO和EMPPASS,并需要提示登录中的错误,如是EMPNO不存在,还是EMPNO存在而是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)i

22、sx1 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=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

23、:=该用户号存在!; when TOO_MANY_ROWS then out_code:=3; out_desc:=该用户号有重复值!; when others thenout_code:=100; out_desc:=其他错误!; end;3 建立一个存储过程,实现myEMP表中指定雇员的EMPPASS字段的修改,修改前必须进行EMPPASS旧值的核对。CREATE OR REPLACE PROCEDURE P_CHANGEPASS( IN_EMPNO IN EMP.EMPNO%TYPE, IN_OLDPASS IN EMP.EMPPASS%TYPE, IN_NEWPASS IN EMP.E

24、MPPASS%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=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:=修改口令不成功;

25、END IF; exception when others then out_code:=100; out_desc:=其他错误; END;4 建立一个函数,输入一个雇员号,返回该雇员的所在同一部门的最高级别上司姓名。create or replace function f_leader(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

26、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 then return v3;end;5 试用上题函数,实现各雇员的同一部门最高级别上司的SELECT查询。select f_leader(7521) from dual;6 *编写实验五中第六题,关于各雇员工资的所得税计算函数实验八、1 建立一个触发器,当myEMP表中部门

27、号存在时,该部门不允许删除。create or replace trigger dept_line_deletebefore delete on dept for each rowdeclarev1 number;begin select count(*) into 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;procedure mgmt_login(in_empno in emp.empno%type,in_emppass in emp.emppass%type,out_code out number,out_desc out

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2