



全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
在PL/SQL中编写触发器-触发器-创建测试表-create table t1( t1_id number primary key, t1_name varchar2(20)-创建序列-drop sequence seq_t1_idcreate sequence seq_t1_id-测试-insert into t1 (t1_id,t1_name) values(seq_t1_id.nextval,a);insert into t1 (t1_id,t1_name) values(seq_t1_id.nextval,b);insert into t1 (t1_id,t1_name) values(seq_t1_id.nextval,c);commit;-编写触发器,实现id的自动增长-create or replace trigger tr_t1_autoIdbefore inserton t1for each rowbegin select seq_t1_id.nextval into :new.t1_id from dual;end;insert into t1 (t1_name) values(d);insert into t1 (t1_name) values(e);insert into t1 (t1_name) values(f);commit;-实现工资只能增加,不能减少-create or replace trigger tr_emp_salbefore updateon empfor each rowdeclarebegin if (:old.sal:new.sal) then raise_application_error(-20009,老板,我每天都努力工作,怎么能减我的工资呢); end if;end;update emp set sal=1000 where empno=7002;-使用条件谓词-更新员工工资,只能增加,不能减少;删除员工,不能删除项目经理-create or replace trigger tr_emp_salbefore update or deleteon empfor each rowbegin if updating then if (:old.sal:new.sal) then raise_application_error(-20009,老板,我每天都努力工作,怎么能减我的工资呢); end if; end if; if deleting then if(:old.job=MANAGER) then raise_application_error(-20009,不能删除项目经理); end if; end if;end; select * from empdelete from emp where empno=7566-添加员工,超过3人不能添加-create or replace trigger tr_emp_addbefore inserton empfor each rowdeclare v_count number;begin select count(*) into v_count from emp where deptno=:new.deptno; if v_count=3 then raise_application_error(-20010,此部门人数太多,不能再添加); end if;end;select deptno,count(*) as counts from emp group by deptnoinsert into emp (empno,ename,sal,deptno) values(6001,大傻,3000,50)-修改部门编号,该部门的员工部门编号也随之变化-create or replace trigger tr_dept_update_deptnoafter update of deptnoon deptfor each rowbegin update emp set deptno=:new.deptno where deptno=:old.deptno;end;-测试-select * from deptselect * from empupdate dept set deptno=21 where deptno=20-看新记录的id是几?-create or replace trigger tr_t1before inserton t1for each rowdeclare v_max t1.t1_id%type;begin select max(t1_id) into v_max from t1; dbms_output.put_line(v_max); :new.t1_id :=nvl(v_max,0)+1;end;-测试-select * from t1delete from t1insert into t1(t1_id,t1_name) values(2,d);-before和after的区别-任务一、t1用最大编号+1 实现自动编号,名称转化为大写。-由于修改了:new变量,所以只能用Before。create or replace trigger tr_t1before insert on t1for each rowdeclare id_max t1.t1_id%type;begin select nvl(max(t1_id),0)+1 into id_max from t1; :new.t1_id :=id_max; :new.t1_name :=upper(:new.t1_name);end;-添加t1的时候,把记录同时添加到t2-sql中复制表select * into t2 from t1 create table t2 as select * from t1-复制表-由于只是使用:new变量,并没有修改,所以-before和after都可以create or replace trigger tr_t1_insertafter insert on t1for each rowbegin insert into
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年高级保育员技能证书考试笔试试题附答案
- 2025年爆破的试题及答案
- 2025年安全生产月试题题库(含参考答案)
- 大学生个人职业生涯规划书
- 在线授课经验分享与教学质量反思
- 1.1细胞中的元素和无机化合物课件-高一上学期生物苏教版必修1
- 受邀安全培训课件
- 晋中安全员b证考试及答案
- 宜昌安全员c证考试及答案
- 乡土文化课程教学设计及实施方案
- 2025年烟草专卖局公开遴选面试高分策略及模拟题答案
- 乳制品行业智能化奶源管理与追溯方案
- 医务人员职业道德准则(2025年版)全文培训课件
- 恒瑞医药2023ESG社会责任报告:关注员工成长共建美好家园
- 医院网络信息安全培训
- 《构成设计基础》全套教学课件
- 项目初步验收汇报
- 2025年山东省济宁市电工等级低压电工作业(应急管理厅)真题(含答案)
- otc药品管理办法
- 康复医学科病历书写规范与质量控制
- 商用厨房设计汇报
评论
0/150
提交评论