Oracle数据库SQL技术-题库

1、查询职位为SALESMAN的员工编号、职位、入职日期。
答案: select empno,job,hiredate from emp where job=‘SALESMAN’;
2、查询入职日期比20部门入职日期最早的员工还要早的员工姓名、入职日期。
答案: select ename,hiredate from emp where hiredate<all(select hiredate from emp where deptno=20);
select ename,hiredate from emp where hiredate<(select min(hiredate) from emp where deptno=20);
3、写一个查询,显示每个部门最高工资和最低工资的差额。
答案: select max(sal)-min(sal) from emp group by deptno;
(只有说显示差额,书上答案是:select dname,max(sal)-min(sal) from dept d,emp e where d.deptno=e.deptno(+) group by dname;)
4、查询(EMP)员工编号、员工姓名、员工职位、员工月薪、工作部门编号。
答案:select empno,ename,job,sal,deptno from emp;
5、写一个查询显示其工资比全体职员平均工资高的员工编号、姓名。
答案: select empno,ename from emp where sal>(select avg(sal) from emp);
*6、显示出平均工资最高的部门平均工资及部门名称。
答案: select avg(sal),dname from emp e,dept d where e.deptno=d.deptno group by dname having avg(sal)=(select max(avg(sal)) from emp group by deptno);
7、显示经理是KING的员工姓名、工资。
答案: select ename,sal from emp where mgr=(select empno from emp where ename=‘KING’);

*8、显示每个部门的最高工资的员工。
答案:方法一:select ename,sal from emp e where not exists(select 1 from emp where deptno=e.deptno and sal>e.sal);(书上答案)
方法二:select ename,deptno from emp where (deptno,sal) in (select deptno ,max(sal) from emp group by deptno);
9、查询每个员工截止到现在一共入职多少天?
答案: select sysdate-hiredate from emp;(不取整数)
select trunc(sysdate-hiredate) from emp;(取整数)
10、查询比所在职位平均工资高的员工姓名、职位。
答案: select ename,job from emp e where sal>(select avg(sal) from emp where job=e.job);
*11、列出至少有一个雇员的所有部门。
答案: select deptno from emp group by deptno having count(empno)>=1;
12、查询经理编号在7902、7566、7788的员工姓名,经理编号。
答案: select ename,mgr from emp where mgr in (7902,7566,7788);
13、显示出工资大于本部门平均工资的员工姓名、工资。
答案: select ename,sal from emp e where sal>(select avg(sal) from emp where deptno=e.deptno);
14、显示出和姓名中包含“W”的员工相同部门的员工姓名。
答案: select ename from emp where deptno in (select deptno from emp where ename like '%W% ’ );
15、使用两种方式查询所有员工(EMP)的信息。
答案: 第一种:select * from emp;
第二种:select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp;(用所有列选)

16、查询工作在CHICAGO的员工人数、最高工资及最低工资。
答案: select count(empno),max(sal),min(sal) from emp e,dept d where e.deptno=d.deptno and loc=‘CHICAGO’;

17、显示每位经理管理员工的最低工资,及最低工资者的姓名。
答案: select ename,sal from emp e where sal = (select min(sal) from emp where mgr=e.mgr);
18、删除工作在NEW YORK的员工记录。
答案: delete from emp where deptno in (select deptno from dept where loc='NEWYORK ');
19、员工试用期6个月,转正后月薪上调20%,请查询出所有员工工作第一年的所有收入(需考虑奖金部分),要求显示格式为:XXX的第一年总收入为XXX。
答案: select ename || ‘的第一年总收入为’ || NVL2(comm,6
sal+6sal1.2+comm,6sal+6sal*1.2) from emp;
*20、查询每个部门工资前两名高的员工姓名、工资。
答案: select ename,sal,deptno from emp e where (select count(empno) from emp where deptno=e.deptno and sal>e.sal)<2;(课本答案)
21、查询员工表中一共有几种岗位类型。
答案: select count(distinct job) from emp;
22、查询员工工资为其部门最低工资的员工的编号和姓名及工资。
答案: select empno,ename,sal from emp e where sal=(select min(sal) from emp where e.deptno=deptno);
*23、查询比自己职位平均工资高的员工姓名、职位、部门名称、职位平均工资。
答案: select ename,e.job,dname,e.sal,avgsal from emp e,dept d,(select job,avg(sal) avgsal from emp group by job) a where e.deptno=d.deptno and e.job=a.job and e.sal>a.avgsal;
24、查询岗位不为SALESMAN,工资和大于等于2500的岗位及每种岗位的工资和。
答案: select job,sum(sal) from emp where job<>‘SALESMAN’ group by job having sum(sal)>=2500;
25、查询工资为其部门最低工资的员工编号、姓名、工资。
答案: select empno,ename,sal from emp e where sal=(select min(sal) from emp where deptno=e.deptno);
26、显示比员工SMITH参加工作时间晚的员工姓名、工资、参加工作时间。
答案: select ename,sal,hiredate from emp where hiredate >(select hiredate from emp where ename = ‘SMITH’);
27、显示出和员工号7369部门相同的员工姓名、工资。
答案: select ename,sal from emp where deptno=(select deptno from emp where empno=7369);
28、显示员工姓名,参加工作时间,经理名,参加工作时间,要求参加时间比经理早。
答案: select e.ename,e.hiredate,m.ename,m.hiredate from emp e,emp m where m.empno=e.mgr and e.hiredate<m.hiredate;
*29、显示员工姓名、部门名称、工资、工资级别,要求工资级别大于4级。
答案: select ename,dname,sal,grade from emp e,salgrade s,dept d where e.deptno=d.deptno and sal between losal and hisal and grade>4;
30、查询部门最低工资高于10号部门最低工资的部门的编号、名称及部门最低工资。
答案: select d.deptno,dname,min(sal) from emp e,dept d where e.deptno=d.deptno group by d.deptno,dname having min(sal)>(select min(sal) from emp where deptno=10);
31、查询月薪在3000到5000的员工姓名,月薪。
答案: select ename,sal from emp where sal between 3000 and 5000;
32、查询工资高于编号为7782的员工工资,并且和7369号员工从事相同工作的员工的编号、姓名及工资。
答案: select empno,ename,sal from emp where sal>(select sal from emp where empno=7782) and job=(select job from emp where empno=7769);

33、查询部门平均工资在2500元以上的部门名称及平均工资。
答案: select dname,avg(sal) from emp e,dept d where e.deptno=d.deptno group by dname having avg(sal)>2500;
34、查询工资最高的员工姓名和工资。
答案: select ename,sal from emp where sal=(select max(sal) from emp);
35、显示所有工作在RESEARCH部门的员工姓名、职位。
答案: select ename,job from emp where deptno=(select deptno from dept where loc=‘RESEARCH’);
36、列出薪金比“SMITH”多的所有雇员。
答案: select * from emp where sal>(select sal from emp where ename=‘SMITH’);
37、创建一个视图,通过该视图可以查询到每个部门的部门名称及最低工资。
答案: create or replace view v1 as select dname,min(sal) minsal from emp e,dept d where e.deptno=d.deptno group by dname;
上面的v1是随意取的,而且min(sal)后面必须要有别名,也是随意的。
38、查询每个部门及每个岗位的部门编号、部门名称、岗位名称、部门人数、最高工资、最低工资、工资总和、平均工资。
答案: select d.deptno,dname,job,count(empno),max(sal),min(sal),
sum(sal),avg(sal) from emp e,dept d where e.deptno=d.deptno group by d.deptno,dname,job;
39、显示出工资大于平均工资的员工姓名、工资。
答案: select ename,sal from emp where sal>(select avg(sal) from emp);

40、查询每个部门的部门编号、部门名称、部门人数、最高工资、最低工资、工资总和、平均工资。
答案: select d.deptno,dname,count(empno),max(sal),min(sal),sum(sal),
avg(sal) from emp e,dept d where e.deptno=d.deptno group by d.deptno,dname;
41、查询部门20的员工,每个月的工资总和及平均工资。
答案: select sum(sal),avg(sal) from emp where deptno=20;
42、查询职位和经理同员工SCOTT或BLAKE完全相同的员工姓名、职位,不包括SCOTT和BLAKE本人。
答案: select ename,job from emp where (job,mgr) in (select job,mgr from emp where ename in (‘SCOTT’,‘BLAKE’)) and ename not in (‘SCOTT’,‘BLAKE’);
43、查询员工表中一共有哪几种岗位类型。
答案: select distinct job from emp;
44、向员工表中新增一个员工,员工编号为8888,姓名为BOB,岗位为CLERK,领导编号7788,入职日期为1985-03-03,薪资3000,奖金和部门为空。
答案: insert into emp values(8888,‘BOB’,‘CLERK’,7788,to_date(‘1985-03-03’,‘YYYY-MM-DD’),3000,null,null);
45、查询每个经理所管理的人数、经理编号、经理姓名,要求包括没有经理的人员信息。
答案: select m.empno,m.ename,count(e.empno) from emp m,emp e where m.empno(+)=e.mgr group by m.empno,m.ename;
上面使用count(
)一样的效果,没有差别。
46、查询员工岗位中不是以“SA”开头并且平均工资在2500元以上的岗位及平均工资,并按平均工资降序排序。
答案: select job,avg(sal) from emp e where job not like ‘SA%’ group by job having avg(sal)>2500 order by avg(sal) desc;
47、创建与emp表结构相同的表empl,并将其部门编号为前30号的员工信息复制到empl表中。
答案: create table empl as select * from emp where deptno<30;
48、查询每个员工的编号、姓名、工资、工资等级、所在工作城市,按照工资等级进行升序排序。
答案: select empno,ename,sal,grade,loc from emp e,dept d,salgrade where e.deptno=d.deptno and e.sal between losal and hisal order by grade;
49、查询所有雇员编号、名字和部门名称。
答案: select empno,ename,dname from emp e,dept d where e.deptno=d.deptno;
50、查询工资比SMITH工资高并且工作地点在CHICAGO的员工姓名、工资、部门名称。
答案: select ename,sal,dname from emp e,dept d where e.deptno=d.deptno and sal>(select sal from emp where ename=‘SMITH’) and loc=‘CHICAGO’;
51、查询每个部门工资最低的两个员工编号、姓名、工资。
答案: select empno,ename,sal from emp e where (select count(empno) from emp where deptno=e.deptno and sal<e.sal)<2;
*52、显示经理号码和经理姓名,这个经理所管理员工的最低工资,没有经理的KING也要显示,不包括最低工资小于3000的,按最低工资由高到低排序。
答案: select m.empno 经理号码,m.ename 经理姓名,min(e.sal) from emp m,emp e where m.empno(+)=e.mgr group by m.empno,m.ename having min(e.sal)>=3000 order by min(e.sal) desc;
53、查询入职日期比10部门任意一个员工晚的员工姓名、入职日期,不包括10部门员工。
答案: select ename,hiredate from emp where hiredate>any(select hiredate from emp where deptno=10) and deptno<>10;

54、查询职位和10部门任意一个员工职位相同的员工姓名、职位,不包括10部门员工。
答案: select ename,job from emp where job in (select job from emp where deptno=10) and deptno<>10;
55、修改工作地点在NEW YORK 或CHICAGO的员工工资,工资增加500.
答案: update emp set sal=sal+500 where deptno in (select deptno from dept where loc in (‘NEW YORK’,‘CHICAGO’));
56、查询1985年12月31日之前人职的员工姓名及人职日期。
答案: select ename,hiredate from emp where hiredate<‘31-12月-85’;
57、修改部门20的员工信息,把1982年之后入职的员工入职日期向后调整10天。
答案: update emp set hiredate=hiredate+10 where deptno=20 and extract(year from hiredate)>1982;
58、查询每个部门的部门编号、平均工资,要求部门的平均工资高于部门20的平均工资。
答案: select deptno ,avg(sal) from emp group by deptno having avg(sal)>(select avg(sal) from emp where deptno=20);
59、按照每页显示5条记录,分别查询工资最高的第1页、第2页、第3页信息,要求显示员工姓名、入职日期、部门名称、工资。
答案: 第一页:select * from (select rownum rn,b.
from (select ename,hiredate,dname,sal from emp e,dept d where e.deptno=d.deptno order by sal desc) b where rownum<=15) where rn>(1-1)5;
第二页:select * from (select rownum rn,b. from (select ename,hiredate,dname,sal from emp e,dept d where e.deptno=d.deptno order by sal desc) b where rownum<=25) where rn>(2-1)5;
第三页:select * from (select rownum rn,b.
from (select ename,hiredate,dname,sal from emp e,dept d where e.deptno=d.deptno order by sal desc) b where rownum<=35) where rn>(3-1)5;
60、向部门表新增一个部门,部门编号为60,部门名称为MARKET。
答案: insert into dept values(60,‘MARKET’,null);
61、数据库管理员 sys 如何查询scott用户emp表中的所有数据。
答案: select * from scott.emp;
62、创建一个班级学生表,里面包含学号、姓名、性别、家庭住址、家庭联系方式五列,其中学号设置为主键。
答案: create table student(id int constraint student_id_pk primary key,name varchar2(50),sex varchar2(50),address varchar2(500),phonenum int);
63、查询入职日期比10部门所有员工晚的员工姓名、入职日期,不包括10部门员工。
答案: select ename,hiredate from emp where hiredate>all(select hiredate from emp1 where deptno=10) and deptno<>10;
64、分别选择员工表、部门表、薪资等级表中的所有数据。
答案: select * from emp;select * from dept; select * from salgrade;
65、显示比工资最高的员工参加工作时间晚的员工姓名、参加工作时间。
答案: select ename,hiredate from emp1 where hiredate>(select hiredate from emp where sal=(select max(sal) from emp));
66、员工试用期6个月,转正后月薪上调20%,请查询出所有员工工作第一年的所有收人(需考虑奖金部分),要求显示列标题为员工姓名,工资收入,奖金收入.总收入。
答案: select ename,(sal
6+1.2
sal6,comm),(sal6+sal1.26+comm) from emp;
67、显示部门名称和人数。
答案: select dname, count(empno) from dept d,emp1 e where e.deptno=d.deptno group by dname;
68、写个查询,分别计算 100.456四舍五入到小数点后2位,第1位、整数位的值。
答案: select round(100.456,2),round(100.456,1),round(100.456) from dual;
69、创建一个用户,并赋予该用户登录数据库的权限。
答案: 前提:conn sys/orcl as sysdba;
create user shop identified by 123456; grant connect to shop;
70、查询大于自己部门平均工资的员工姓名、工资、所在部门平均工资、高于部门平均工资的额度。
答案: select ename,sal,avgsal,sal-avgsal from emp e,(select deptno,avg(sal) avgsal from emp group by deptno )d where e.deptno=d.deptno and e.sal>d.avgsal;
71、删除经理编号为7566的员工记录。
答案: delete from emp where mgr=7566;
72、查询所有工作在NEW YORK和CHICAGO的员工姓名、员工编号,以及他们的经理姓名、经理编号。
答案: select e.ename,e.empno,m.ename,m.empno from emp e,emp m,dept d where e.mgr=m.empno and e.deptno=d.deptno and d.loc in(‘NEW YORK’,‘CHICAGO’);
73、查询职位及经理和10部门任意一个员工职位及经理相同的员工姓名、职位,不包括10部门员工。
答案: select ename,job from emp where (mgr,job)in(select job,mgr from emp where deptno=10) and deptno<>10;
74、删除工资大于所在部门平均工资的员工记录。
答案: delete from emp where sal>(select avg(sal) from emp where deptno=emp1.deptno);
75、写个查询,显示所有姓名中含A字符的员工姓名,工作地点。
答案:select ename,loc from emp e,dept d where ename like ‘%A%’;
76、员工试用期6个月,转正后月薪上调20%,请查询出所有员工工作第一年的年薪所得(不考虑奖金部分,年薪为试用期6个月的月薪+转正后6个月的月薪)。
答案: select ename,sal
6+sal
1.2
6 from emp
77、通过子查询的方式创建一个表dept10,该表保存10号部门的员工数据。
答案: create table dept10 as select * from emp where deptno=10;
78、找员工姓名和直接上级的名字。
答案: select e.ename,m.ename from emp e,emp m where e.mgr=m.empno;
79、查询部门人数大于2的部门编号、部门名称、部门人数。
答案: select d.deptno,dname,count(empno) from emp e,dept d where e.deptno=d.deptno group by d.deptno,dname having count(empno)>2;
80、在员工表中添加一个性别列,列名为gender,类型为char(2),默认值为“男”。
答案: alter table emp add gender char(2) default ‘男’;
81、员工转正后,月薪上调20% ,请查询出所有员工转正后的月薪
答案: select ename,sal,1.2*sal from emp;
*82、列出一个雇员都没有的所有部门名称。
答案: select dname from dept d where not exists(select 1 from emp where deptno=d.deptno);
83、查询员工表中一共有几种岗位类型。
答案: select count(distinct job)from emp;
84、查询部门编号不在10部门的员工姓名、部门编号。
答案: select ename,deptno from emp where deptno<>10;

85、显示员工KING和FORD管理的员工姓名及其经理姓名。
答案: select e.ename,m.ename from emp e,emp m where e.mgr=m.empno and m.ename in(‘KING’,’FORD’);
86、写一个查询,显示所有工作在CHICAGO并且奖金不为空的员工姓名、工作地点、奖金。
答案: select ename,loc,comm from emp e,dept d where e.deptno=d.deptno and loc in(‘CHICAGO’)and comm is not null;
87、简答一下下述该SQL语句的执行过程。
答案:SELECT d.deptno,d.dname,count(ename),avg(sal) FROM emp e,dept d WHERE e.deptno=d.deptno GROUP BY d.deptno,d.dname ORDER BY count(ename);
88、写一个查询、显示所有员工姓名、部门编号、部门名称。
答案: select ename ,e.deptno,dname from emp e,dept d where e.deptno=d.deptno;
89、查询员工姓名倒数第2个字符为T的员工姓名。
答案: select ename from emp where ename like ‘%T_’;
90、查询员工姓名以W开头的员工姓名。
答案: select eanme from emp where ename like’W%’;
91、写一个查询,用首字母大写,其他字母小写显示雇员的ename,显示名字的长度,并给每列一个适当的标签,条件是满足所有雇员名字的开始字母是J、A或M的雇员,并对查询结果按雇员的ename升序排序。(提示:使用initcap 、length、substr)
答案: select initcap(ename),length(ename) from emp where substr(ename,1,1)in(‘J’,’A’,’K’);
92、列出至少有一个雇员的所有部门名称。
答案: select dname from dept d where exists (select 1 from emp where deptno=d.deptno);
93、写一个查询显示和员工SMITH工作在同一个部门的员工姓名、雇用日期,查询结果中排除SMITH。
答案: select ename,hiredate from emp where deptno=(select deptno from emp where ename=‘SMITH’) and ename!= ‘SMITH’;
94、使用子查询的方式查询哪些职员在NEW YORK工作。
答案: select ename from emp where deptno in (select deptno from dept where loc=‘NEW YORK’);
95、查询所有员工编号、姓名、部门名称,包括没有部门的员工也要显示出来。
答案: select empno,ename,dname from emp e,dept d where e.deptno=d.deptno(+);
96、创建一个视图,通过该视图可以查询到工作在NEW YORK和CHICAGO的员工编号、姓名、部门编号、入职日期。
答案: create view v1 as select empno,ename,e.deptno,hiredate from emp e,dept d where e.deptno=d.deptno and d.loc in(‘NEW YORK’,‘CHICAGO’);
97、修改奖金为NULL的员工,奖金设置为0.
答案: update emp set comm=0 where comm is null;
98、查询奖金为空的员工姓名,奖金。
答案: select ename,comm from emp where comm is null;
99、按照每页显示5条记录,分别查询第1页、第2页、第3页信息,要求显示员工姓名、入职日期、部门名称。
答案:
第一页:
select b.* from (select rownum rn,ename,hiredate,dname from emp e,dept d where e.deptno=d.deptno and rownum<=15) b where rn>(1-1)5;
第二页:
select b. from (select rownum rn,ename,hiredate,dname from emp e,dept d where e.deptno=d.deptno and rownum<=25) b where rn>(2-1)5;
第三页:
select b.
from (select rownum rn,ename,hiredate,dname from emp e,dept d where e.deptno=d.deptno and rownum<=3*5) b where rn>(3-1)*5;
100、查询入职日期在1982年1985年的员工姓名,入职日期。
答案: select ename,hiredate from emp where extract(year from hiredate) between 1982 and 1985;
书上:select ename, hiredate from emp where hiredate>='01-1月-82 ’ and hiredate<='31-12月-85 ';
101、查询部门人数在2人以上的部门名称、最低工资、最高工资,并对求得的工资进行四舍五入到整数位。
答案: select dname,round(min(sal)),round(max(sal)) from emp e,dept d where e.deptno=d.deptno group by dname having count(empno)>2;
102、查询部门编号为10或者20的员工姓名,部门编号。
答案: select ename,deptno from emp where deptno in (10,20);
103、创建表date_test,包含列d,类型为date类型。并试向date_test表中插入两条记录,一条当前系统日期记录,一条记录为“1998-08-18”。
答案: create table date_test (d date);
insert into date_test values (sysdate);
insert into date_test values (‘18-8月-1998’);
104、查询员工姓名中包含大写或小写字母A的员工姓名。
答案: select ename from emp where ename like ‘%A%’ or ename like ‘%a%’;
105、查询入职日期最早的前5名员工姓名。
答案: select ename,hiredate from emp e where (select count(empno) from emp where hiredate<e.hiredate)<5;
106、分别查看员工表、部门表、薪资等级表的表结构。
答案: DESC emp;
DESC dept;
DESC salgrade;
107、显示员工SMITH的姓名、部门名称、直接上级名称。
答案: select e.ename,dname,m.ename from emp e,dept d,emp m where e.deptno=d.deptno and e.mgr=m.empno and e.ename=‘SMITH’;
108、查询入职日期最早的员工姓名、入职日期。
答案: select ename,hiredate from emp where hiredate=(select min(hiredate) from emp);
109、查询不是经理的员工姓名。
答案: select ename from emp where empno not in(select mgr from emp where mgr is not null);
110、创建一个视图,通过该视图可以查询到工资在2000~5000内并且姓名中包含有A的员工编号,姓名,工资。
答案: create or replace view v1 as select empno,ename,sal from emp
where sal between 2000 and 5000 and ename like ‘%A%’;
111、列出入职日期早于其直接上级的所有雇员。
答案: select ename,hiredate from emp e where exists (select 1 from emp where e.mgr=empno and e.hiredate<hiredate);
112、查询职位及经理和10部门任意一个员工职位或经理相同的员工姓名、职位,不包括10部门员工。
答案: select ename,job from emp where (job,mgr)in(select job,mgr from emp where deptno=10) and deptno<>10;
113、查询服务器当前时间。
答案: select sysdate from dual;
114、写一个查询显示其上级领导是KING的员工姓名、工资。
答案: select ename,sal from emp where mgr=(select empno from emp where ename=‘KING’);
115、查询部门平均工资大于2000,且人数大于2的部门编号、部门名称、部门人数、部门平均工资,并按照部门人数升序排序。
答案: select d.deptno,d.dname,count(empno),avg(sal) from emp e,dept d where e.deptno=d.deptno group by d.deptno,d.dname having avg(sal)>2000 and count(empno)>2 order by count(empno);
116、查询部门编号为10或20,人职日期在1981年5月1日之后,并且姓名中包含大写字母A的员工姓名,员工姓名长度(提示,要求使用INSTR函数,不能使用like进行判断)。
答案: select ename,length(ename) from emp where deptno in(10,20) and hiredate >‘01-5月-1981’ and instr(ename,‘A’)>0;
117、查询工作在CHICAGO并且入职日期最早的前2名员工姓名、入职日期。
答案: select rownum,b.*from (select ename,hiredate from emp where deptno in(select deptno from dept where loc=‘CHICAGO’) order by hiredate) b where rownum<=2;
118、查询部门人数大于所有部门部门平均人数的部门编号、部门名称、部门人数。
答案: select d.deptno,d.dname,count(empno) from emp e,dept d where e.deptno=d.deptno group by d.deptno,d.dname
having count(empno)>(select avg(count(empno)) from emp group by deptno);

简答题

一、匿名块操作(10分)。以下两题任选其一回答,每题10分。
1.编写一个匿名块,使用记录型变量输出100号员工的姓名和部门名称。
答案:
declare
type emp_record_type is record
(ename employees.last_name%type,
dname departments.department_name%type
); ------------------------3分
emp_record emp_record_type; ------------------------1分
begin
select last_name,department_name into emp_record ---------------1分
from employees,departments
where employee_id=100 and employees.department_id=departments.department_id; ----------3分
dbms_output.put_line(emp_record.ename||’ '||emp_record .dname);-----2分
end;
2. 写一个块,实现向员工表入职一名新员工,员工编号为当前最大员工编号加1,员工姓名为JAMES,岗位为AC_ACCOUNT,入职日期为当前日期,工资为4000,部门编号同101号员工,上级为King,奖金为NULL,其余为说明项可视情况随机安排。
答案:
Declare
v_maxempno employees.employee_id%type; ---------------1分
v_mgr employees.employee_id%Type; ---------------1分
v_depno employees.department_id%type; ---------------1分
Begin
Select Max(employee_id) Into v_maxempno From employees ; ---------------1分
Select employee_id Into v_mgr From employees Where last_name = ‘King’; ---------------1分
select department_id into v_depno from employees where employee_id=101; ---------------1分
insert Into employees(employee_id,last_name,job_id,manager_id,hire_date,salary,commission_pct,department_id,email) Values(v_maxempno+1,‘JAMES’,‘AC_ACCOUNT’,v_mgr,Sysdate,4000,Null,v_depno,‘222’); ---------------4分
Commit;
End;
二、存储过程操作(10分)。以下两题任选其一回答,每题10分。

  1. 写一个存储过程getAllDept,遍历所有部门(输出部门编号、名称)。(8分)
    并执行过程显示结果。(2分)
    提示:1.定义游标
    2.使用游标for循环
    答案:
    CREATE OR REPLACE PROCEDURE getAllDept
    IS
    CURSOR dept_cursor IS
    SELECT department_id,department_name FROM departments; —3分
    BEGIN
    FOR dept_record IN dept_cursor LOOP --3分
    dbms_output.put_line(‘编号:’||dept_record.department_id ||’,名称为:’||dept_record.department_name); --2分
    END LOOP;
    END;
    执行,两种执行方式任选其一,回答正确得2分。

结果

2.创建存储过程Get_name,实现根据输入员工编号输出员工姓名的功能。(7分)
并调用过程输出100号员工姓名。(3分)
答案:
CREATE OR REPLACE PROCEDURE Get_name
(p_id IN employees.employee_id%type, ---------2分
p_name OUT varchar2) ---------2分
IS
BEGIN
SELECT last_name into p_name
FROM employees
WHERE employee_id = p_id; ----------------3分
END Get_name;
执行
declare
v_ename employees.last_name%type; ----------2分
begin
up_GetEmpName(100,v_ename); -------1分
dbms_output.put_line(‘100号雇员的名字是:’||v_ename);
end;
三、触发器操作。(10分)以下两题任选其一回答,每题10分。
1.编写一个触发器sal_up,每次工资调整后,在员工表中更新数据,并显示“触发器已出发,数据已更新”。(8分)验证触发器执行结果。(2分)
代码:
create or replace trigger sal_up
after update on employees -----3分
for each row -------2分
begin
dbms_output.put_line(‘触发器已触发,数据已更新’); -----3分
end;
测试数据:
update employees set salary=salary+1000 where employee_id=2018; -----2分
2. 编写一个触发器Job_up,在Employees表中做插入或更新操作时,如果职位列表值为“AD_VP”,提示“需要关注”。 (8分)验证触发器执行结果。(2分)
CREATE OR REPLACE TRIGGER Job_up
BEFORE INSERT OR UPDATE OF job_id ON employees -----------3分
FOR EACH ROW
WHEN (new.job_id = ‘AD_VP’) ----2分
BEGIN
raise_application_error(-20001,‘需要关注’); —3分
END;
测试数据:
INSERT INTO emp(empno,ename,job,mgr,hiredate,sal)
VALUES(1003,‘ljs’,‘AD_VP’,7902,sysdate,5500);----------------2分
四、函数操作。(10分)以下两题任选其一回答,每题10分。
1.编写函数up_GetDName,实现根据输入的部门编号查看部门名称的功能,设计合适的出异常处理。(8分)并调用执行。(2分)
答案:
CREATE OR REPLACE FUNCTION up_GetDName
(p_dno in employees.department_id%TYPE) -----2分
return departments.department_name%type
AS
p_dname departments.department_name%type; ----------2分
BEGIN
SELECT department_name INTO p_dname
FROM departments WHERE department_id = p_dno; ------2分
return p_dname;
EXCEPTION
WHEN NO_DATA_FOUND ---------2分
THEN return ‘error’;
END up_GetDName;
执行
SELECT up_GetDName(department_id )
FROM employees
WHERE department_id = 50; ------------2分
2. 编写一个函数count_num,根据输入的部门编号统计部门人数,人数大于等于5人时,提示“人数过多,请精简!”;人数小于2人时,提示“人数过少,请补充!”;其余提示“人数合适!”,设计合适的出异常处理。(8分)并执行调用。(2分)
答案:
CREATE OR REPLACE FUNCTION count_num(dep_no employees.department_id%TYPE)
RETURN NUMBER
IS
v_num NUMBER; ----------------2分
BEGIN
SELECT count(employee_id) INTO v_num from employees WHERE department_id = dep_no; -------------------2分
IF v_num >=5 THEN
dbms_output.put_line(‘人数过多,请精简!’);
ELSIF v_num<2 THEN
dbms_output.put_line(‘人数过少,请补充!’);
ELSE
dbms_output.put_line(‘人数合适!’); -----------2分
END IF;
RETURN v_num;
exception ------------------------2分
when no_data_found
then
return -1;
END;
执行
SELECT count_num (department_id )
FROM employees
WHERE department_id = 50; ------------2分

Oracle数据库SQL技术——习题1相关推荐

  1. 2场直播丨Oracle数据库SQL执行计划的取得和解析、一次特殊的 Oralce 硬解析性能问题的技术分享...

    1. Oracle数据库SQL执行计划的取得和解析- 2021.02.23 2月23日(周二)晚八点直播课,Oracle优化资深专家陈晓辉,以专业ORACLE数据库技术支持工程师的角度讲解SQL文的执 ...

  2. 资源放送丨《Oracle数据库SQL执行计划的取得和解析》PPT视频

    前段时间,墨天轮邀请Oracle优化方面的资深专家 陈晓辉 老师分享了<Oracle数据库SQL执行计划的取得和解析>,在这里我们共享一下PPT和视频,供大家参考学习. Oracle优化资 ...

  3. oracle数据库----SQL语句的实践(应用实例)

    oracle数据库----SQL语句的实践(应用实例) 创建表工资表salary,包括员工号emp_id,员工名emp_name,员工月基本工资monthsal,员工月总发工资totalsal. cr ...

  4. oracle数据库硬恢复,ORACLE数据库恢复技术

    一.恢复的意义 当我们使用一个数据库时,总希望数据库的内容是可靠的.正确的,但由于计算机系统的故障(硬件故障.网络故障.进程故障和系统故障)影响数据库系统的操作,影响数据库中数据的正确性,甚至破坏数据 ...

  5. oracle表结构修改回滚,87.Oracle数据库SQL开发之 修改表内存——数据库事务的提交和回滚...

    87.Oracle数据库SQL开发之 修改表内存--数据库事务的提交和回滚 数据库事务(transaction)就是一组SQL语句,这组SQL语句时一个逻辑工作单元. 要永久性的记录事务中SQL语句的 ...

  6. oracle数据库sql培训,Oracle数据库SQL语言实战培训教程(全面、详细、案例、精讲)套餐...

    风哥oracle数据库SQL语言实战培训教程(案例.精讲)套餐,DBA必备技能. 套餐介绍: 风哥Oracle数据库SQL语言实战培训教程(案例.精讲)套餐,DBA必备技能. 内容: SQL语言基础入 ...

  7. oracle数据库并行数限制,Oracle数据库并行处理技术详解(上)

    Oracle数据库并行处理技术是数据库的一项核心技术,它使组织能够高效地管理和访问TB级的数据.如果不能提供高效的Oracle数据库并行处理技术,这些大型数据库(通常用于数据仓库但也越来越多地出现在业 ...

  8. 线性回归中oracle性质,66.Oracle数据库SQL开发之 高级查询——使用线性回归函数...

    66.Oracle数据库SQL开发之 高级查询--使用线性回归函数 线性回归函数可以用普通最小平方回归曲线拟合一组数值对.线性回归函数可用于聚合.串口或报表函数. 如下图1: 例如: store@PD ...

  9. 29 Oracle数据库SQL开发之 SQLPlus使用——创建简单报表

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 29.O ...

  10. 19.Oracle数据库SQL开发之 笛卡尔积

    19.Oracle数据库SQL开发之 笛卡尔积 欢迎转载,转载请标明出处:http://blog.csdn.net/notbaron/article/details/49107213 如果在多表查询中 ...

最新文章

  1. 更新elementui图标不显示_超简单elementui主题及变量修改方案
  2. 面试题-两个数值交换
  3. mysql cross apply_SQL Server CROSS APPLY和OUTER APPLY的应用详解
  4. oc-14-对象方法调用类方法
  5. Tomcat 是怎么处理js file access request的
  6. JUnit与EasyMock合作
  7. oracle导数卡死,oracle-审计导数
  8. python二级简书_12月4日,总结发现杯,备战python二级
  9. Android studio每次启动卡在fetching Android sdk compoment information的解决方案
  10. 关于Oracle的提示详解(1)
  11. JavaScript:年月日時分秒設置
  12. 成为一名优秀架构师有标准吗?这12点或许能带给你一些启发
  13. 使用python的sklearn包实现CCA(典型相关分析)
  14. linux网络协议栈ip_rcv
  15. 蓝牙打印 设置打印样式_Android蓝牙打印机,带你真正了解各种打印格式
  16. abap 在屏幕如何调用选择屏幕以及如何获得选择屏幕的操作
  17. springbootadmin 客户端监控配置
  18. CentOS下安裝Nvidia docker 2.0:[Errno 256] No more mirrors to try錯誤及解決方式
  19. 劲爆!电波拉皮有什么危害,电波拉皮效果好吗能维持多久,人见人夸了
  20. 【学习笔记】min-height、max-height、line-height

热门文章

  1. 牛血清白蛋白包裹氧化锌纳米粒
  2. win7如何更改计算机管理员用户名和密码,Win7如何修改管理用户名
  3. 【深入理解RabbitMQ原理】RabbitMQ 相关问题总结--RabbitMQ 如何确保消息发送和消费?
  4. oracle查询某个时间段的数据
  5. python爬取固定酒店评论_爬取携程上酒店评论数据
  6. 四 国内IP核相关企业及其分析
  7. 我们是如何解决偶发性的 502 错误的
  8. 在微型计算机中 集成在微处理,在微型计算机中,微处理器的主要功能是进行什么...
  9. mysql逻辑删除案例_实现数据逻辑删除的一种方案
  10. 运算放大器参数的详细解释和分析-运放压摆率(SR)