一些SQL知识

为了找工作,找一些sql相关的笔试题来做做

题目:考试总分为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分)
create sequence seq_employee
increment by 1
start with 1
nomaxvalue
nocycle;


2.写一个PL/SQL块,插入表user.employee中100条数据。插入该表中字段id用序列seq_employee实现,薪水和姓名字段可以任意填写。(15分)
declare i number;
begin
for i in 1..100
loop
insert into employee
values (seq_employee.nextval, 1950+i, 'Jack' ||to_char(i));
commit;
end loop;
end;



3.写一个语句块,在语句块中定义一个显式游标,按id升序排列,打印表employee中前十条数据。(15分)
declare 
cursor c is select id, salary, name from (select * from employee order by id asc) where rownum < 11;
v_record c%rowtype;
begin
open c;
loop
fetch c into v_record;
exit when c%notfound;
dbms_output.putline(to_char(v_record.id) || ',' || to_char(v_record.salary) || ',' || v_record.name);
end loop;
close c;
end;


4.创建存储过程p_employee,输入员工薪水范围,返回员工工号、姓名、薪水结果集,结果集按员工薪水升序排列。(15分)
create or replace procedure p_employee
(iminsalary in number, imaxsalary in number)
is
begin
for x in (select id, salary, name from (select * from employee where salary between iminsalary and imaxslalary) order by salary)
loop
dbms_output.put_line(to_char(x.id) || to_char(x.salary) || x.name);
end loop;
end;


5.创建函数f_employee实现更新员工薪水的功能,将薪水低于2000且姓wang的员工薪水加5%,其他不变,更新成功则返回0,否则返回1。(15分)
create or replace function f_employee return number
is
begin
update employee set salary = salary + salary*0.05 where salary<2000 and name like 'wang%';
commit;
if sql%rowcount=0 then
return 1;
else
return 0;
end if;
end;



Implicit cursors: SQL%ROWCOUNT returns number of rows affected by SQL statement



6.写一个匿名语句块,用于执行函数f_employee,并打印执行该函数的结果。(8分)
declare a number;
begin
a:=f_employee();
dbms_output.put_line(to_char(a));
end;



7.创建存储过程p_create_emp,用于判断表employee是否存在,如果存在则删除该表。(15分)
create or replace procedure p_create_emp
is
v_count number;
begin
select count(*) into v_count from user_tables where table_name = 'EMPLOYEE';
if v_count = 0 then
return;
else
execute immediate 'drop table employee';
end if;
end;



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



摘自: http://space.itpub.net/8645409/viewspace-175844
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值