电子常识 电子基础知识常用元件介绍 经验总结 电子感悟 电子幽默 电子前沿 编程课堂
返回首页

触发器基础知识

时间:2009-10-27 10:19来源:未知 作者:admin 点击:

: 添加到QQ书签 Google书签 添加到百度搜藏 Yahoo书签 新浪ViVi 和讯网摘 提交新发现,Dig it 365Key网摘 天极网摘 我摘 POCO网摘 博拉网 将本文在板儿砖上开砖场 添加到抽屉 添加到 挖客

一、时序电路概述

同步时序电路的状态只在统一的信号脉冲控制下才同时变化一次,如果信号脉冲没有到来,即使输入信号发生变化,电路的状态仍不改变。
异步时序电路的状态变化不是同时发生的,它没有统一的信号脉冲(时钟脉冲用CP表示),输入信号的变化就能引起状态的变化。

 

:时序电路的表示形式

时序电路按输入变量的依从关系可分为米里型和莫尔型。米里型电路的输出是输入变量的现态函数;莫尔型电路的输出仅与电路的现态有关。

一般用Qnt)表示现态函数,用Qn+1t)表示次态函数。它们统称为状态函数,一个时序电路的主要特征是由状态函数给出的。


:时序电路的特征
时序电路中记忆功能是靠触发器来实现的,我们设计和分析时序电路的对象就是触发器。
描述时序电路时通常使用状态表和状态图,我们分析时序电路的方法通常是比较相邻的两种状态(即现态和次态)。

1列出下表所示时序电路的逻辑表达式、状态表和状态图   
  
逻辑表达式为:Qn+1=AQn+BQF=A B+AB
 
  
它的状态表为如下右图所示

  
状态图如下右图所示:

Qn     A       B

Qn+1

F

 

 

 

 

触发器是特定事件出现的时候,自动执行的代码块。类似于存储过程,但是用户不能直接调用他们。

功能:


1
允许/限制对表的修改


2
自动生成派生列,比如自增字段


3
强制数据一致性


4
提供审计和日志记录


5
防止无效的事务处理


6
启用复杂的业务逻辑

开始
create trigger biufer_employees_department_id
       before insert or update
             of department_id
             on employees
       referencing old as old_value
                   new as new_value
       for each row
       when (new_value.department_id<>80 )
begin
       :new_value.commission_pct :=0;
end;
/

 


触发器的组成部分:


1
触发器名称


2
触发语句


3
触发器限制


4
触发操作

 


1
触发器名称


create trigger biufer_employees_department_id


命名习惯:


biufer
before insert update for each row


employees
表名

 

 

department_id 列名

 

 


2
触发语句


比如:


表或视图上的DML语句


DDL
语句


数据库关闭或启动,startup shutdown 等等


before insert or update


              of department_id


              on employees


       referencing old as old_value


                       new as new_value


       for each row
说明:


1
无论是否规定了department_id ,对employees表进行insert的时候


2
employees表的department_id列进行update的时候


3
触发器限制


when (new_value.department_id<>80 )

 


限制不是必须的。此例表示如果列department_id不等于80的时候,触发器就会执行。


其中的new_value是代表跟新之后的值。

 


4
触发操作


是触发器的主体


begin


       :new_value.commission_pct :=0;

end;

 


主体很简单,就是将更新后的commission_pct列置为0


触发:


insert into employees(employee_id,


last_name,first_name,hire_date,job_id,email,department_id,salary,commission_pct )


values( 12345,’Chen’,’Donny’, sysdate, 12, ‘donny@hotmail.com’,60,10000,.25);

select commission_pct from employees where employee_id=12345;

触发器不会通知用户,便改变了用户的输入值。


触发器类型:


1
语句触发器


2
行触发器


3
INSTEAD OF 触发器


4
系统条件触发器


5
用户事件触发器


1
语句触发器


是在表上或者某些情况下的视图上执行的特定语句或者语句组上的触发器。能够与INSERTUPDATEDELETE或者组合上进行关联。但是无论使用什么样的组合,各个语句触发器都只会针对指定语句激活一次。比如,无论update多少行,也只会调用一次update语句触发器。

 


例子:


需要对在表上进行DML操作的用户进行安全检查,看是否具有合适的特权。


Create table foo(a number);

Create trigger biud_foo
       Before insert or update or delete On foo

begin
       If user not in (‘DONNY’) then
             Raise_application_error(-20001, ‘You don’t have access to modify this table.’);
      End if;
End;


/
即使SYSSYSTEM用户也不能修改foo

[试验]


对修改表的时间、人物进行日志记录。


1
建立试验表


create table employees_copy as select *from hr.employees

2 建立日志表


create table employees_log(who varchar2(30),when date);

3 employees_copy表上建立语句触发器,在触发器中填充employees_log 表。


Create or replace trigger biud_employee_copy
            Before insert or update or delete On employees_copy
       Begin
              Insert into employees_log(Who,when) Values( user, sysdate);
       End;
       /

4 测试

update employees_copy set salary= salary*1.1;
select *from employess_log;

5 确定是哪个语句起作用?


即是INSERT/UPDATE/DELETE中的哪一个触发了触发器?


可以在触发器中使用INSERTING / UPDATING / DELETING 条件谓词,作判断:


begin
       if inserting then
              -----
        elsif updating then
              -----
        elsif deleting then
              ------
        end if;
end;

if updating(‘COL1’) or updating(‘COL2’) then
       ------
end if;

[试验]


1
修改日志表


alter table employees_log

       add (action varchar2(20));


2
修改触发器,以便记录语句类型。


Create or replace trigger biud_employee_copy
              Before insert or update or delete On employees_copy
       Declare
              L_action employees_log.action%type;
       Begin
        if inserting then
               l_action:=’Insert’;
        elsif updating then
               l_action:=’Update’;
        elsif deleting then
               l_action:=’Delete’;
        else
              raise_application_error(-20001,’You should never ever get this error.’);
              Insert into employees_log(Who,action,when) Values( user, l_action,sysdate);
       End;
       /
3
测试
      insert into employees_copy( employee_id, last_name, email, hire_date, job_id)
      values(12345,’Chen’,’Donny@hotmail’,sysdate,12);
select *from employees_log
update employees_copy set salary=50000 where employee_id = 12345;

2 行触发器

是指为受到影响的各个行激活的触发器,定义与语句触发器类似,有以下两个例外:

1 定义语句中包含FOR EACH ROW子句

2 BEFORE……FOR EACH ROW触发器中,用户可以引用受到影响的行值。

比如:
定义:

create trigger biufer_employees_department_id
       before insert or update of department_id on employees_copy
       referencing old as old_value new as new_value
       for each row
       when (new_value.department_id<>80 )
begin
       :new_value.commission_pct :=0;
end;
/
Referencing
子句:


执行DML语句之前的值的默认名称是 :old ,之后的值是 :new


insert
操作只有:new


delete
操作只有 :old


update
操作两者都有

本站相关资讯链接QQ电子网


找不到想要的,那就在这里Google一下!

(责任编辑:admin)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名:密码: 验证码:点击我更换图片
推荐内容
站长推荐