设为首页 收藏本站
查看: 755|回复: 0

[经验分享] 一道Oracle笔试题 附网友答案

[复制链接]

尚未签到

发表于 2016-8-9 07:13:59 | 显示全部楼层 |阅读模式
考试总分为100分,共8题,时间为1小时。
表结构说明:
create table employee(
id number(10) not null, — 员工工号
salary number(10,2) default 0 not null, — 薪水
name varchar2(24) not null — 姓名
);
1.创建序列seq_employee,该序列每次取的时候它会自动增加,从1开始计数,不设最大值,并且一直累加,不循环。(10分)
2.写一个PL/SQL块,插入表user.employee中100条数据。插入该表中字段id用序列seq_employee实现,薪水和姓名字段可以任意填写。(15分)
 
6.写一个匿名语句块,用于执行函数f_employee,并打印执行该函数的结果。(8分)
7.创建存储过程p_create_emp,用于判断表employee是否存在,如果存在则删除该表。(15分)
8.写一个匿名语句块,用于执行存储过程p_create_emp。(7分)
答案如下:
SQL> create table employee(
2 id number(10) not null, — 员工工号
3 salary number(10,2) default 0 not null, — 薪水
4 name varchar2(24) not null — 姓名
5 );
表已创建。
—第一题答案:
SQL> Create sequence seq_employee increment by 1 start with 1 nomaxvalue nocycle;
序列已创建。
—第二题答案:
SQL> declare i number;
2 begin
3 for i in 1 .. 100
4 loop
5 insert into employee
6 values(seq_employee.nextval,1950+i,'王明'||to_char(i));
7 commit;
8 end loop;
9 end;
10 /
PL/SQL 过程已成功完成。
SQL> select * from employee where rownum<11;
ID SALARY NAME
———- ———- ————————
1 1951 王明1
2 1952 王明2
3 1953 王明3
4 1954 王明4
5 1955 王明5
6 1956 王明6
7 1957 王明7
8 1958 王明8
9 1959 王明9
10 1960 王明10
已选择10行。
3.写一个语句块,在语句块中定义一个显式游标,按id升序排列,打印表employee中前十条数据。(15分)
——-第三题答案:
SQL> declare
2 cursor c is select id,salary,name from(select * from employee order by id) where rownum<11;
3 v_record c%rowtype;
4 begin
5 open c;
6 loop
7 fetch c into v_record;
8 exit when c%notfound;
9 dbms_output.put_line(to_char(v_record.id)||','||to_char(v_record.salary)||','||v_record.name);
10 end loop;
11 close c;
12 end;
13 /
1,1951,王明1
2,1952,王明2
3,1953,王明3
4,1954,王明4
5,1955,王明5
6,1956,王明6
7,1957,王明7
8,1958,王明8
9,1959,王明9
10,1960,王明10
PL/SQL 过程已成功完成。
4.创建存储过程p_employee,输入员工薪水范围,返回员工工号、姓名、薪水结果集,结果集按员工薪水升序排列。(15分)
——-第四题答案
SQL> create or replace procedure p_employee
2 (iminsalary in number,
3 imaxsalary in number)
4 is
5 begin
6 for x in(select id,salary,name from(select * from employee where salary between iminsalary and imaxsalary) order by salary)
7 loop
8 dbms_output.put_line(to_char(x.id)||to_char(x.salary)||x.name);
9 end loop;
10 end;
11 /
过程已创建。
SQL> exec p_employee(2000,2007);
502000王明50
512001王明51
522002王明52
532003王明53
542004王明54
552005王明55
562006王明56
572007王明57
PL/SQL 过程已成功完成。
5.创建函数f_employee实现更新员工薪水的功能,将薪水低于2000且姓wang的员工薪水加5%,其他不变,更新成功则返回0,否则返回1。(15分)
———第五题答案
SQL> create or replace function f_employee return number
is
begin
update employee set salary=salary+salary*0.05 where salary<2000 and name like ‘王%';
commit;
if sql%rowcount=0 then
return 1;
else
return 0;
end if;
end;
/
函数已创建。
—-第六题答案
SQL> declare a number;
2 begin
3 a:=f_employee();
4 dbms_output.put_line(to_char(a));
5 end;
6 /
0
PL/SQL 过程已成功完成。
SQL> select * from employee where salary<2000 and name like ‘王%';
未选定行
SQL> select * from employee where rownum<50;
ID SALARY NAME
———- ———- ————————
1 2048.55 王明1
2 2049.6 王明2
3 2050.65 王明3
4 2051.7 王明4
5 2052.75 王明5
6 2053.8 王明6
7 2054.85 王明7
8 2055.9 王明8
9 2056.95 王明9
10 2058 王明10
11 2059.05 王明11
ID SALARY NAME
———- ———- ————————
12 2060.1 王明12
13 2061.15 王明13
14 2062.2 王明14
15 2063.25 王明15
16 2064.3 王明16
17 2065.35 王明17
18 2066.4 王明18
19 2067.45 王明19
20 2068.5 王明20
21 2069.55 王明21
22 2070.6 王明22
ID SALARY NAME
———- ———- ————————
23 2071.65 王明23
24 2072.7 王明24
25 2073.75 王明25
26 2074.8 王明26
27 2075.85 王明27
28 2076.9 王明28
29 2077.95 王明29
30 2079 王明30
31 2080.05 王明31
32 2081.1 王明32
33 2082.15 王明33
ID SALARY NAME
———- ———- ————————
34 2083.2 王明34
35 2084.25 王明35
36 2085.3 王明36
37 2086.35 王明37
38 2087.4 王明38
39 2088.45 王明39
40 2089.5 王明40
41 2090.55 王明41
42 2091.6 王明42
43 2092.65 王明43
44 2093.7 王明44
ID SALARY NAME
———- ———- ————————
45 2094.75 王明45
46 2095.8 王明46
47 2096.85 王明47
48 2097.9 王明48
49 2098.95 王明49
已选择49行。
—-第七题答案
SQL> create or replace procedure p_create_emp
2 is
3 v_count number;
4 begin
5 select count(*) into v_count from user_tables where table_name='EMPLOYEE';
6 if v_count=0 then
7 return;
8 else
9 execute immediate ‘drop table employee';
10 end if;
11 end;
12 /
过程已创建。
———第八题答案
SQL> exec p_create_emp;
PL/SQL 过程已成功完成。
SQL> select * from employee;
select * from employee
*
ERROR 位于第 1 行:
ORA-00942: 表或视图不存在

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-255090-1-1.html 上篇帖子: oracle sql 合并 分组 聚合函数 下篇帖子: oracle 中translate()方法部分用法理解
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表