觸發器簡介

2021-08-26 13:58:49 字數 3210 閱讀 3553

觸發器(trigger)是個特殊的儲存過程,它的執行不是由程式呼叫,也不是手工啟動,而是由事件來觸發,不能帶引數,比如當對乙個表進行操作( insert,delete, update)時就會啟用它執行。觸發器經常用於加強資料的完整性約束和業務規則等。 觸發器可以從 dba_triggers ,user_triggers 資料字典中查到。

oracle觸發器語法

觸發器是特定事件出現的時候,自動執行的**塊。類似於儲存過程,觸發器與儲存過程的區別在於:儲存過程是由使用者或應用程式顯式呼叫的,而觸發器是不能被直接呼叫的。

功能:1、 允許/限制對錶的修改

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

3、 強制資料一致性

4、 提供審計和日誌記錄

5、 防止無效的事務處理

6、 啟用複雜的業務邏輯

觸發器觸發時間有兩種:after和before。

1、觸發器的語法:

create [or replace] tigger觸發器名 觸發時間 觸發事件

on表名

[for each row]

begin

pl/sql語句

end其中:

觸發器名:觸發器物件的名稱。

before---表示在資料庫動作之前觸發器執行;

after---表示在資料庫動作之後出發器執行。

觸發事件:指明哪些資料庫動作會觸發此觸發器:

insert:資料庫插入會觸發此觸發器;

update:資料庫修改會觸發此觸發器;

delete:資料庫刪除會觸發此觸發器。

表 名:資料庫觸發器所在的表。

for each row:對錶的每一行觸發器執行一次。如果沒有這一選項,則只對整個表執行一次。

事務的特性:

事務具有acid四大特性:

某種意義上說,原子性是手段,一致性是目的,通過原子性的手段達到一致性的目的。

a:原子性: 做都做 不做都不做-------手段

c:一致性: 做事務操作的前後,資料保持一致性-----目的

i: 隔離性: 隔離性越強,併發性越弱

---------用for update 來實現事務的隔離性 通過鎖的特性:

|是乙個加鎖過程

d:永久性:同乙個事務在commit 之後不能rollback

oracle中 dml語言 發生時 oracle 只操作記憶體,並不能自動提交

行觸發器的工作原理是 倆個重要的記憶體表

old new

insert ---- ok

update ok ok

delete ok ----

:new --為乙個引用最新的列值;

:old --為乙個引用以前的列值; 這兩個變數只有在使用了關鍵字 "for each row"時才存在.且update語句兩個都有,而insert只有:new ,delect 只有:old;

例項--drop trigger trig_account_spittor

create or replace trigger trig_account_spittor

after insert or update

of spittor_id,amount,status

on spittor_account

for each row

begin

if :new.status=1 then

if inserting then

update spittor set balance=(balance+:new.amount) where id=:new.spittor_id;

end if;

end if;

commit;

end;

--drop trigger trig_spittor_order

create or replace trigger trig_spittor_order

after insert or update

of id,spittor_id,name,total_price,type,status

on spittor_order

for each row

begin

if :new.status=1 then

if inserting then

if :new.type=1 then

insert into spittor_account(id,order_id,spittor_id,amount,fiinsh_date,status)values(seq_spittoraccount.nextval,:new.id,:new.spittor_id,:new.total_price,sysdate,:new.status);

elsif :new.type=2 then

insert into spittor_account(id,order_id,spittor_id,amount,fiinsh_date,status)values(seq_spittoraccount.nextval,:new.id,:new.spittor_id,-:new.total_price,sysdate,:new.status);

end if;

elsif updating then

if :old.status=1 then

dbms_output.put_line('warning:this order is also done it....');

else

if :new.type=1 then

insert into spittor_account(id,order_id,spittor_id,amount,fiinsh_date,status)values(seq_spittoraccount.nextval,:new.id,:new.spittor_id,:new.total_price,sysdate,:new.status);

elsif :new.type=2 then

insert into spittor_account(id,order_id,spittor_id,amount,fiinsh_date,status)values(seq_spittoraccount.nextval,:new.id,:new.spittor_id,-:new.total_price,sysdate,:new.status);

end if;

end if;

end if;

end if;

end;

前觸發器和後觸發器簡介

觸發器是一種特殊的儲存過程。當 insert update 或者delete 語句修改表中乙個或者多個行時執行觸發器。因為 sql server 對特定表上的每乙個指定操作呼叫乙個觸發器,所以可以使用觸發器擴充套件 sql sever 的內建完整性和資料操縱功能.注意 不像delete 語句,tra...

Oracle觸發器簡介 建立 使用觸發器

觸發器類似與儲存過程,都是為了實現特殊功能而執行的 塊。觸發器不允許使用者顯示傳遞引數,不能夠返回引數值,不允許使用者呼叫觸發器。觸發器只是在oracle合適的時間自動呼叫,非常類似於面向程式設計中的 觸發器按照觸發事件型別 物件不同分為 語句觸發器,行觸發器,instead of觸發器,系統事件觸...

Oracle觸發器簡介

oracle觸發器型別 1 語句觸發器 對乙個sql語句執行一次,分為before和after 例項 create trigger name before insert or update or delete 也可以是after of column name 可選,限定列名 on tablename...