v_girlnumber(3); v_zsrsnumber(3); v_lqrsnumber(3); v_lqyxnumber(4); v_yxmcvarchar2(20);
cursorstu_cursor is select 录取院校 ,avg( 总分 ),max( 总分 ), min( 总分 ) from student group by 录取院校 order by avg( 总分 ) desc; begin
dbms_output.put_line(' 院校编号 院校名称 招生人数 录取人数 男生人数 女生人数 最高分数 最低分数 平均分数 '); openstu_cursor; loop
fetch stu_cursor into v_yxbh,v_avg,v_max,v_min; exit when stu_cursor%notfound; ifv_yxbh is not null then
select 院校名称 , 招生人数 , 录取人数 into v_yxmc,v_zsrs,v_lqrs from collegewhere 院校编号 =v_yxbh;
selectcount(*) into v_boy from student where 录取院校 =v_lqyx and 性别 =1; select count(*) into v_girl from student where 录取院校 =v_lqyx and 性别 =2;
dbms_output.put_line(rpad(v_yxbh,8,' ')||rpad(v_yxmc,18,'')||rpad(v_zsrs,9,' ')||rpad(v_lqrs,9,' ')||rpad(v_boy,9,' ')||rpad(v_girl,9,' ')||rpad(v_max,9,'')||rpad(v_min,9,' ')||rpad(v_avg,9,' ')); endif; endloop; closestu_cursor;
end;
说明:统计工作主要是通过一个按录取院校分组的查询游标来完成的。在分组查询中统计院校的平均分数、最高分和最低分,并按照平均分排序。其他信息在游标循环中根据院校编号通过查询语句得到。 3. 触发器的设计
通过触发器可以为数据提供进一步的保护。下面设计两种常见类型的触发器。 1 .分数修改触发器
如果要自动记录对数据库的数据进行的某些操作,可以通过创建触发器来实现。在考生数据库中,高考的分数字段的内容十分重要,是录取的最重要依据,应该正确设置对其进行操作的权限,并做好操作的记录。权限可以通过设定特定权限的账户进行控制,记录操作可以通过触发器来实现。 通过触发器来记录对考生表高考分数字段的插入、删除和修改操作,记录的内容可以包括:操作时间、操作人账户、执行的操作、考生编号、原分数和修改后的分数。以上内容记录到表 operation_log 。
表 6 operation_log 表的结构
步骤 1 :创建如下的记录表 operation_log : SQL> create table operation_log( 序号 number(10) primary key, 账户 varchar2(15) not null, 时间 date, 操作 varchar2(10), 考生编号 number(5), 原分数 number(3), 新分数 number(3) );
步骤 2 :创建一个主键序列 operation_id :
SQL> create sequence operation_id incrementby 1 start with 1 maxvalue 9999999 nocycle nocache;
步骤 3 :创建和编译以下触发器: SQL> create or replace trigger operation before-- 触发时间为操作前
insertor delete or update of 总分 on student for eachrow -- 行级触发器 begin
ifinserting then
insertinto operation_log values(operation_id.nextval, user,sysdate,' 插入 ',:new. 编号 ,null,:new. 总分 ); elsifdeleting then
insertinto operation_log values(operation_id.nextval, user,sysdate,' 删除 ',:old. 编号 ,:old. 总分 ,null); else
insertinto operation_log values(operation_id.nextval, user,sysdate,' 修改 ',:old. 编号 ,:old. 总分 ,:new. 总分 ); end if; end;
2 .级联修改触发器
我们还可以创建级联修改触发器 update_college_yxbh ,以实现如下的功能:当修改院校的编号时,自动修改学生表中与院校编号关联的字段内容。学生表共有 3 个字段与院校编号关联,即一志愿,二志愿和录取院校。 创建级联修改触发器:
SQL> create or replace triggerupdate_college_yxbh
after update of 院校编号 oncollege for each row begin
updatestudent set 一志愿 =:new. 院校编号 where 一志愿 =:old. 院校编号 ; updatestudent set 二志愿 =:new. 院校编号 where 二志愿 =:old. 院校编号 ; updatestudent set 录取院校 =:new. 院校编号 where 录取院校 =:old. 院校编号 ; end;
4 系统的测试和运行
直接使用查询是进行测试的一种很好的方法,在这里也列出了一些可能用到的查询。 在投档前可以进行以下的查询。 (1) .按姓名进行模糊查询 查找姓王的考生:
SQL>select 编号 , 姓名 , 性别 , 总分 from student where 姓名 like ' 王 %';
(2) .按分数或分数段进行查询
查询分数在 600 ~ 650 分之间的考生:
SQL> select 编号 , 姓名 , 性别 , 总分 from student where 总分 >600 and 总分 <650;
(3) .查询分数最高的考生报考的院校 查询分数最高的考生一志愿报考的院校:
SQL> select 编号 , 姓名 , 性别 , 总分 , 院校名称 from student s, college c where s. 一志愿 =c. 院校编号 and s. 总分 =(select max( 总分 ) from student);