实验四--触发器的使用_第1页
实验四--触发器的使用_第2页
实验四--触发器的使用_第3页
实验四--触发器的使用_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上实验四 触发器的使用 4实验目的1、理解触发器基本概念。2、掌握触发器定义及其触发。3、了解触发器调试。4、理解触发器作用。实验内容1、定义一个触发器,完成及时计算所有供应商供应零件总数量。create or replace trigger t1_spjafter insert or delete or update on spjdeclarecursor cur_1 is select sno ,sum(qty) as sumqty from spj group by sno; begin for cur in cur_1 loop update s set sqt

2、y=cur.sumqty where s.sno=cur.sno; end loop; end t1_spj;insert into spj values(S5,P6,J4,700); 2、定义触发器,实现实体完整性(以s表供应商代码sno为例)。create or replace trigger t2_spjafter insert on sdeclarev_sno s.sno%type;v_count number;cursor cur_sno is select sno from s group by sno having count(*)1;begin select count(*)

3、into v_count from s where sno is null; if v_count0 then raise_application_error (-20008,主码sno不能取空值); end if; open cur_sno; fetch cur_sno into v_sno; if cur_sno%found then raise_application_error (-20012,主码sno不能重复); end if; end t2_spj;insert into S(SNO,SNAME,STATUS,CITY) values(S1,竟仪,20,天津); 3、定义触发器,

4、实现参照完整性(以spj表供应商代码sno参照s表供应商代码sno为例)。-当在SPJ表插入数据时,如果S,P,J表不存在相应的记录时,则插入失败create or replace trigger tr1_spjbefore insert or update of sno,pno,jnoon spjfor each row declare v_count1 number; v_count2 number; v_count3 number; begin select count(*) into v_count1 from s where sno=:new.sno; if v_count11 th

5、en raise_application_error(-20001,供应商编号为|to_char(:new.sno)|不存在); end if; select count(*) into v_count2 from p where pno=:new.pno; if v_count21 then raise_application_error(-20002,供应商编号为|to_char(:new.pno)|不存在); end if; select count(*) into v_count3 from j where jno=:new.jno; if v_count31 then raise_a

6、pplication_error (-20005,供应商编号为|to_char(:new.sno)|在spj表中有引用); end if;end tr2_spj; delete from s where s.sno=S1;-级联删除,删除S表中的记录时,同时删除SPJ表中的记录create or replace trigger tr3_spjafter delete on sfor each row declare begin delete from spj where spj.sno=:old.sno; end tr3_spj; delete from s where s.sno=S1; -级联更新,更新S表中的SNO时,同时更新SPJ表中的SNO记录create or replace trigger tr4_spjafter update of sno on sfor each row declare begin u

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论