数据库实验报告(5)
设有基于图书馆数据库的 4 个基本表:
图书(书号,书名,作者,出版社,单价)
读者(读者号,姓名,性别,办公电话,部门,读者类别编号)
借阅(读者号,书号,借出日期,归还日期)
读者类别(读者类别编号,读者类别,允许借阅册数,允许借阅天数)
编写存储过程实现:
1)根据读者号查询该读者的图书借阅的册数
// 创建存储过程:
create or replace procedure rentnumber(rid1 in varchar2 )
is
rnumber number;
begin
select count(*) into rnumber
from borrow
where rid = rid1;
dbms_output.put_line(rnumber);
end;
// 调用该存储过程:
set serveroutput on
begin
rentnumber('120521');
end;
2)根据书名和作者查询相应的出版社与单价
// 创建存储过程:
create or replace procedure dpr(bn in varchar2,au in varchar2 )
is
dp book %rowtype;
begin
select * into dp
from book
where bname = bn and author = au;
dbms_output.put_line('出版社:'||dp.press||'价格'||dp.price);
end;
// 调用该存储过程:
set serveroutput on
begin
dpr('数据库','贾元春');
end;
3)读者借阅图书时,需要查询是否超出允许借阅册数,如果超出则不能继续借阅图书,试创建一存储过程完成此查询功能。(提示:输入参数:读者号;输出:能否借阅的结论)
// 创建存储过程:
create or replace procedure can(bi in varchar2 )
is
bo number;
cb number;
rc varchar2(50 byte);
begin
select rcid into rc
from reader
where rid = bi;
select numbers into cb
from readercategory
where rcid = rc;
select count(*) into bo
from borrow
where rid = bi;
cb := cb - bo;
if cb = 0 then
dbms_output.put_line('不能借阅');
else
dbms_output.put_line('可以借阅');
end if;
end;
// 调用该存储过程:
set serveroutput on
begin
can('1205011');
end;
4)根据读者号查询该读者的图书借阅的册数
// 创建函数:
create or replace function rn (ri varchar2)
return number
is
re number;
begin
select count(*) into re from borrow
where rid = ri;
return re;
end;
// 调用该函数:
declare
re number(10,0);
begin
re:=rn('1205021');
dbms_output.put_line(re);
end;
遇到的一点问题:
问题1:预计的输出结果没有出现,只有匿名块已完成
解决方法:在运行程序前加‘set serveroutput on;’
问题2:‘set serveroutput on;’出现报错
解决方法:改用‘set serveroutput on size unlimited;’语句