1、练习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 order by sal) where rownum实
2、验二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和销售员SALESMAN的姓名、工资select ename,sal fro
3、m 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,yyyy)=1981;7 查询显示每个雇员加入公司的准确时间
4、,按年月日 时分秒显示。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,),loc;9 查询列出各部门的部门名和部门经理名字select dname,ename from emp,dept where emp.deptno=dept.deptno
5、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 group by deptno order by avg(sal) de
6、sc ) 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条,并使雇员号不重复;每个雇员都有所属部门,雇员在同一部门的经理是同一人。insert .update commit
7、4 建立一个与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_emp a set comm=NVL(comm,0)+500 where
8、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;8 新增列性别SEX,字符型。alter table emp add(sex char(2);9 修改新雇员表中的MGR列,为字符型。该列数据必须
9、为空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=2 查询员工SMITH的各个层次领导。 connect by prior mgr= empno start with ENAME=SMITH3 查询显示EMP表各雇员的工作类型,并翻译为中文显示用dec
10、ode函数4 *查询显示雇员进入公司当年是什么属相年(不考虑农历的年份算法)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的执行结果进行说明。7 定义序列mySeq、myEMP、myV_emp的同义词,能否用同义词对上述对象进行访问。8 在myEMP表中建立ename的唯一性索引。9 如何在sql*plus中,运行s
11、ql的脚本(即后缀为.sql的文件)实验五、1 观察下列PL/SQL的执行结果declare s emp%rowtype;begin select * into s from emp where ename=KINGDBMS_OUTPUT.PUT_LINE(s.empno|s.ename|s.job|s.sal);END;2 编写一个PL/SQL,显示ASC码值从32至120的字符。 for i in 32.120 loop dbms_output.put_line(chr(i); end loop;end;3 计算myEMP表中COMM最高与最低的差值,COMM值为空时按0计算。 var1
12、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);4 根据表myEMP中deptno字段的值,为姓名为JONES的雇员修改工资;若部门号为10,则工资加100;部门号为20,加200;其他部门加400。 c1 number; c2 number; select deptno i
13、nto c1 from emp where ename=JONES; if c1=10 then c2:=100;elsif c1=20 then=200;else c2:=400; end if; update emp set sal=sal+c2 where ename=JONES;5 计算显示部门人数最多的部门号、人数、工资总和,以及部门人数最少的部门号、人数、工资总和。6 计算myEMP中所有雇员的所得税总和。假设所得税为累进税率,所得税算法为:工资收入为01000为免税;收入10002000者,超过1000的部分税率10;20003000者超过2000部分按20税率计算;300040
14、00者超过3000部分按30税率计算;4000以上收入,超过4000部分按40税率计算。(请查阅累进税率的概念) sum_xx number:=0; xx number;-计算收入为10002000的所得税总额select sum(sal-1000)*0.1) into xx from emp where sal 1000 and sal2000 and sal3000 and sal4000;dbms_output.put_line(sum_xx);7 *(可选做,难题)假设有个表如myEMP,未建立主键,含有多条记录重复(列值完全相同),试编制一个PL/SQL,将多余的重复记录删除。实验六
15、、1 用外部变量,实现两个PL/SQL程序间的数据交换。SQL variable a1 number; 2 :a1:=1000; 3 end; 4 /PL/SQL 过程已成功完成。 2 dbms_output.put_line(:a1);10002 插入myEMP表中的数据记录,考虑可能出现的例外,并提示。主要的例外提示:唯一性索引值重复DUP_VAL_ON_INDEX3 删除myDEPT表中的数据记录一条,考虑例外情况,并提示。违反完整约束条件4 将下列PL/SQL改为FOR游标 cursor cur_myemp is select * from emp; r emp%rowtype; op
16、en cur_myemp; fetch cur_myemp into r; while cur_myemp%found dbms_output.put_line(r.ename); close cur_myemp; end;5 工资级别的表salgrade,列出各工资级别的人数。(用游标来完成)v1 number;cursor cur1 is select * from salgrade; for c1 in cur1 select count(*) into v1 from emp where sal between c1.losal and c1.hisal; dbms_output.pu
17、t_line(grade|c1.grade| |v1);实验七、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)is
18、x1 emp.ename%type;x2 number;select 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:out_desc:=x1;else out_code:=2; out_desc:=用户登陆密码错误!;exception when NO_DATA_FOUND then=该用户号存在! when TOO_MANY_ROWS then=3;=该用户
19、号有重复值! when others then=其他错误!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.EMPPASS%TYPE, OUT_CODE OUT NUMBER, OUT_DESC OUT VARCHAR2) IS X1 NUMBER; BEGIN SELECT COUNT(*)
20、 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: OUT_DESC:=修改口令成功; ELSE=修改口令不成功; END IF; exception=其他错误; END;4 建立一个函数,输入一个雇员号,返回该雇员的所在同一部门的最高级别上司姓名。create or replace function f_leader(in_empno in emp
21、.empno%type) return varchar2v2 number;v3 emp.ename%type;v4 emp.deptno%type;v1:=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;=v2;end loop;when others then return v3;5 试用上题函数,实现各雇
22、员的同一部门最高级别上司的SELECT查询。select f_leader(7521) from dual;6 *编写实验五中第六题,关于各雇员工资的所得税计算函数实验八、1 建立一个触发器,当myEMP表中部门号存在时,该部门不允许删除。create or replace trigger dept_line_deletebefore delete on dept for each row select count(*) into v1 from emp where deptno=:old.deptno; if v1=1 then RAISE_APPLICATION_ERROR(-20000,错误);实验九、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_desc o
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1