Oracle数据库基本操作五——存储过程与触发器
4. 存储过程与触发器:
例7-1: (存储过程) 创建一个显示学生总人数的存储过程。
set serveroutput on create or replace procedure student_count as p1 number(3); begin select count(*) into p1 from student; dbms_output.put_line('学生总人数是:'||p1); end; / execute student_count();
例7-2: (存储过程) 创建显示学生信息的存储过程STUDENT_LIST,并引用STU_COUNT存储过程。
set serveroutput on; create or replace procedure student_list ascursor select_hand is/*定义游标方便使用*/ select sno,rtrim(sname) as sname,ssex,sage,sdept,sclass from student; begin for i in select_hand loop dbms_output.put_line(i.sno||' '||i.sname||' '||i.ssex||' '||i.sage||' '
||i.sdept||' '||i.sclass); end loop; STUDENT_COUNT(); end; / execute student_list();
例7-3: (存储过程) 创建一个显示学生平均成绩的存储过程。
set serveroutput on; create or replace procedure student_avgs (no in student.sno%type) as avgs1 number(3,1); begin select avg(score) into avgs1 from score group by sno having sno = no ; dbms_output.put_line('学号为:'||no||' 的平均成绩是:'|| avgs1); end; / execute student_avgs('96002');
例7-4: (存储过程) 创建显示所有学生平均成绩的存储过程。
set serveroutput on create or replace procedure student_avgs as cursor calcu_avgs is select sno,avg(sno) as avgs from score group by sno; begin for i in calcu_avgs loop dbms_output.put_line('学号为:'||i.sno||' 的平均成绩是:'|| i.avgs); end loop; end; / execute student_avgs();
例7-5: (修改数据库表) 在Student表中增加SAVG(N,6,2) 字段。
alter table student add savg number(6,2);
例7-6: (存储过程) 创建存储过程,计算每个学生的平均成绩保存到学生表SAVG字段中。
set serveroutput on create or replace procedure save_savg as cursor calcu_avgs is select sno, avg(sno) as ag from score group by sno; begin for i in calcu_avgs loop update student set savg = i.ag where sno = i.sno; end loop; end; / execute save_savg();
例7-7: (触发器) 当更新学生成绩表SCORE中的学生成绩时,自动计算该学生的平均成绩保存到学生表SAVG字段中。
create or replace trigger TSTU_UPA after update or delete or insert on score referencing new as new old as old for each row begin if updating or inserting then update student set savg=-1 where sno=:new.sno; end if; if deleting then update student set savg=-1 where sno=:old.sno; end if; end; / create or replace trigger TSTU_UPS after update or delete or insert on student declare ssno char(5); ssavg number(6,1); begin select sno into ssno from student where savg=-1; select avg(score) into ssavg from score group by sno having sno=ssno; if updating or inserting then update student set savg=ssavg where sno=ssno; end if; if deleting then update student set savg=ssavg where sno=ssno; end if; end;
例7-8: (触发器) 创建包含插入、删除、修改多种触发事件的触发器DML_LOG,对SCORE表的操作进行记录。用INSERTING、DELETING、UPDATING谓词来区别不同的DML操作。
先创建事件记录表LOGS,该表用来对操作进行记录。该表的字段含义解释如下:
LOG_ID:操作记录的编号,数值型,它是该表的主键,自动增1,可由序列自动生成。
LOG_TABLE:进行操作的表名,字符型,非空,该表设计成可以由多个触发器共享使用。比如我们可以为Student表创建类似的触发器,同样将操作记录到该表。
LOG_DML:操作的动作,即INSERT、DELETE或UPDATE三种之一。
LOG_KEY_ID:操作时表的主键值,数值型。之所以记录表的主键,是因为主键是表的记录的惟一标识,可以识别是对哪一条记录进行了操作。对于Score表,主键是由SNO_CNO构成。
LOG_DATE:操作的日期,日期型,取当前的系统时间。
LOG_USER:操作者,字符型,取当时的操作者账户名。比如登录SCOTT账户进行操作,在该字段中,记录账户名为SCOTT。
set serveroutput on; create table logs( log_id number primary key, log_table varchar2(30), log_dml char(10), log_key_id varchar2(30), log_date date, log_user char(30) ); create sequence log_id; create or replace trigger sc_logs after insert or delete or update on score referencing new as new old as old for each row declare tlog_id logs.log_id%type; tlog_table logs.log_table%type; tlog_dml logs.log_dml%type; tlog_key_id logs.log_key_id%type; tlog_date logs.log_date%type; tlog_user logs.log_user%type; begin select max(logs.log_id) into tlog_id from logs; tlog_table := 'score'; tlog_date := sysdate; tlog_user := user; if inserting then tlog_dml := 'insert'; tlog_key_id:=:old.rowid; insert into logs values(log_id.nextval, tlog_table, tlog_dml, tlog_key_id, tlog_date, tlog_user); end if;
end;