day12_触发器

一、 什么是触发器
数据库触发器是一个存储的PL/SQL程序块,它与一个基表联系,当在表上执行特定的数据库维护(插入、删除、更新这三种操作)时,隐含地执行一个PL/SQL程序块。

二、触发器的作用:
              。防止非法的数据库操纵、维护数据库安全
              。对数据库的操作进行审计,存储历史数据
              。完成数据库初始化处理
              。控制数据库的数据完整性
              。进行相关数据的修改
              。完成数据复制
              。自动完成数据库统计计算
              。限制数据库操作的时间、权限等,控制实体的安全性



三、触发器的组成:
             1、触发时间:触发器事件的时间次序(before, afer)[2]
             2、触发事件:什么SQL语句会引起触发器触发(Insert, delete, update)[3]
             3、触发子体:触发器触发时要执行的操作(一个完整的PL/SQL程序)
             4、触发类型:触发器被执行的次数(语句级、行级)[2] //语句级只执行一次,行级会执行多次。
                  [*]一个表上最多可以创建12个不同类型的触发器:3*2*2 = 12


四、创建触发器注意事项:
              1、在触发器中可以调用存储过程、包;在存储过程中不得调用触发器。
              2、在触发器中不得使用commit, rollback, savepoint语句。
              3、在触发器中不得间接调用含有commit, rollback, savepoint的语句的存储过程及函数。

五,创建触发器


实现当操纵员工表时自动将员工人数统计到部门表中。
1  scott用户下
create table dept1(dno int,dname varchar(20),population int);
create table emp1(eid int,ename varchar2(20),dno int);

insert into dept1 values (11,'sale',0);
insert into dept1 values (12,'market',0);
insert into dept1 values (13,'study',0);
commit;


2.
SQL> create or replace trigger trigger01
   after delete or insert or update on emp1
    for each row
    begin
    if inserting then                                 
    update dept1 set population=population+1     
     where dept1.dno=:new.dno;
    elsif deleting then
    update dept1 set population=population-1
  where dept1.dno=:old.dno;
   elsif updating then
   update dept1 set population=population+1
   where dept1.dno=:new.dno;
  update dept1 set population=population-1 
  where dept1.dno=:old.dno;
   end if;
   end;
/



3.

SQL> select * from dept1;

       DNO DNAME                POPULATION
---------- -------------------- ----------
        11 sale                          0
        12 market                        0
        13 study                         0


insert into emp1 values (1001,'prajna',11);
insert into emp1 values (1002,'sommer',12);
insert into emp1 values (1003,'huihui',13);
commit;


SQL>  select * from dept1;

       DNO DNAME                POPULATION
---------- -------------------- ----------
        11 sale                          1
        12 market                        1
        13 study                         1
(发现里面population列的值跟着变了)



update emp1 set dno=12 where eid=1003; 
 commit;
 
================================================
公司库存表(kucun) 同类产品的总量
分销商订货(xiaoshou) 时,kucun表数量减少
create table kucun(cid int,shul int);
insert into kucun values(100,9000);

SQL> select * from kucun;

       CID       SHUL
---------- ----------
       100       9000


create table xiaoshou(cid int,shuliang int);
insert into xiaoshou values(100,1000);


SQL> select * from xiaoshou;

       CID   SHULIANG
---------- ----------
       100       1000


SQL> create or replace trigger t12
   after insert on xiaoshou
    for each row
     begin
    if inserting then 
    update kucun set shul=shul-:new.shuliang where kucun.cid=:new.cid;
   
   end if;
   end;
  /

((特殊变量:   :new    :old
   :new代表你所新输入的数据
   :old代表更改前的数据
  使用方式 :old.列名
  
  insert 操作只有 :new
  delete 操作只有 :old
  update 操作 :new  :old 都有))

查看错误
SQL> show error





posted @ 2016-03-21 21:24  暗夜小精灵~~  阅读(241)  评论(0编辑  收藏  举报