数据库面试题之PL/SQL面试题

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开始计数,不设最大值,并且一直累加,不循环。

Create sequence seq_employee start with  1 increment  by 1 minvalue 1
nomaxvalue nocycle nocache order;

2.写一个PL/SQL块,插入表user.employee中100条数据。插入该表中字段id用序列seq_employee实现,薪水和姓名字段可以任意填写。

Begin
for i in 1..100 loop
insert into employee
values(seq_employee.nextval,'10000','son');
end loop;
end;

----这里私自加上自己的复杂点的想发,尽量用上条件--

 

 

3.写一个语句块,在语句块中定义一个显式游标,按id升序排列,打印表employee中前十条数据。

declare 
cursor cur_emp is select * from employee where id<112 order by id asc;
row_emp cur_emp%rowtype;
begin
open cur_emp;
fetch cur_emp into row_emp;
while cur_emp%found
loop
dbms_output.put_line(row_emp.id||row_emp.salary||row_emp.name);
fetch cur_emp into row_emp;
end loop;
close cur_emp;
end;
/

 


4.创建存储过程p_employee,输入员工薪水范围,返回员工工号、姓名、薪水结果集,结果集按员工薪水升序排列。

参照http://blog.csdn.net/szstephenzhou/article/details/7766626

 ----首先创建包,oracle中的包中可以包含函数,存储过程和游标的定义。

CREATE OR REPLACE package emp_package
IS
type emp_cursor
IS ref CURSOR;
  PROCEDURE emp_procedure(
      hisal employee.salary%type,
      losal employee.salary%type,
      Re_cursor OUT emp_cursor);
  END;
  /

----再创建包体

CREATE OR REPLACE PROCEDURE EMP_PROCEDURE(
LOSAL IN NUMBER,HISAL IN NUMBER,SAL_CURSOR out EMP_PACKAGE.EMP_CURSOR)
IS
BEGIN
OPEN SAL_CURSOR FOR
SELECT * FROM EMPLOYEE WHERE SALARY BETWEEN LOSAL AND HISAL order by salary desc;
END EMP_PROCEDURE;
/

----执行过程结果

var sal_cursor refcursor
exec emp_procedure(100,100000,:sal_cursor);

 

5.创建函数f_employee实现更新员工薪水的功能,将薪水低于2000且姓wang的员工薪水加5%,其他不变,更新成功则返回0,否则返回1。

CREATE OR REPLACE FUNCTION EMP_FUNCTION(
SAL NUMBER,FIRSTNAME CHAR)
RETURN BOOLEAN
AS
BEGIN 
UPDATE EMPLOYEE SET SALARY=1.05*SALARY WHERE SALARY<SAL AND NAME=FIRSTNAME;
RETURN 1;
else
return 0;
end;
/

 

6.写一个匿名语句块,用于执行函数f_employee,并打印执行该函数的结果。

7.创建存储过程p_create_emp,用于判断表employee是否存在,如果存在则删除该表。

8.写一个匿名语句块,用于执行存储过程p_create_emp。

第二题: Wages 表 Emp_id | 基本工资| 工龄工资|

------------------------------------------- 1 | 1.00 | 1.00 |

------------------------------------------- 2 | 1.00 | 2.00 |

------------------------------------------- 3 | 1.00 | 3.00 |

------------------------------------------- 4 | 1.00 | 4.00 |

------------------------------------------- 得到如下结果: Emp_id | 基本工资| 工龄工资 | 合计 | 名次

------------------------------------------------------------------ 1 | 1.00 | 1.00 |2.00 | x

------------------------------------------------------------------ 2 | 1.00 | 2.00 |3.00 | y

------------------------------------------------------------------ 3 | 1.00 | 3.00 |4.00 | ..

------------------------------------------------------------------ 4 | 1.00 | 4.00 |5.00 | ..

SELECT EMP_ID,FOUNDSAL,AGESAL,SUMSAL ,
(case sumsal when 2 then 'X'
                  when 3 then 'Y'
                 ELSE  'Z'
    end) as grade
from
(select emp_id,foundsal,agesal,foundsal+agesal as sumsal from
WAGES);

 

------------------------------------------------------------------

第三题: 3、有如下信息: 起始地 目的地 距离(公里) A B 1000

                                                                       A C 1100

                                                                       A D 900

                                                                       A E 400

                                                                       B D 300

                                                                       D F 600

                                                                      E A 400

                                                                      F G 1000

                                                                      C B 600

请用SQL语句或一段代码写出从A出发,可达到的目的地(包括间接方式)以及公里数。

posted @ 2013-08-17 22:08  残阳飞雪  阅读(1335)  评论(0编辑  收藏  举报