Pay attention to the development of the trigger point
- Trigger does not receive parameters
- The more the trigger, the lower the operating performance of DML
- Trigger a maximum of 32k, if pl / sql statement too much, you can write stored procedures, triggers call
- In the operative part of the trigger can only DML statements (insert, select, update, delete), you can not use DDL statements (create, alter, drop)
- Trigger can not contain transaction control statements (commit, rollback, savepoint), as part of a statement trigger is triggered, triggering statement submitted a rollback, the trigger is also committed, rolled back; procedure call trigger body The function can not use transaction control statements
- Trigger body can not declare any long and blog variables,: new and: old table can not be any long column or blog
- A table can have a maximum of 12 triggers, and at the same time, the same event, the same type of trigger can only have one, nor contradictions between the various flip-flops
1 --insert事件触发器 2 before insert/before insert for each row 3 after insert/after insert for each row 4 --update事件触发器 5 before update/before update for each row 6 after update/after update for each row 7 --delete事件触发器 8 before delete/before delete for each row 9 after delete/after delete for each row
Trigger enabled and disabled
. 1 - Enable / disable individual flip-flop 2 ALTER Trigger trigger_name {enable / disable}; . 3 - Enable / disable all triggers associated with a particular table . 4 ALTER Table table_name {enable / disable} All Triggers;
Trigger Syntax
. 1 Create [ or Replace ] Trigger trigger_name 2 {before / After} - time to trigger . 3 { Delete / INSERT / Update [ of column ] } - triggering event . 4 ON Table . 5 [ for each Row [WHERE (condition) ] ] 6 DECLARE . 7 - local Variables here Wallpaper . 8 the begin . 9 PL / SQL statement 10 End ;
Case
1, insert a print data word
1 create or replace trigger trg_test1 2 after insert on table 3 begin 4 dbms_output.put_line('insert successful!') 5 end [trg_test1];
2, the data can not be inserted in the weekend
. 1 Create or Replace Trigger trg_test2 2 before INSERT ON Table . 3 DECLARE . 4 t_day VARCHAR2 ( 10 ); . 5 the begin . 6 SELECT TO_CHAR (SYSDATE, ' Day ' ) INTO t_day from Dual; . 7 IF t_day in ( ' Saturday ' , ' Sunday ' ) the then 8 - throw an error 9 RAISE_APPLICATION_ERROR ( - 20001 , ' can not be inserted in the data weekend! ' ); 10 End IF ; . 11 End [ trg_test2 ] ;
3, use: old and: new
Triggering statement | :old | :new |
insert | All fields are null | The data to be inserted |
update | Value before the update | The updated value |
delete | Value before deleting | All fields are null |
. 1 - is determined: updating of the original value must be greater than 2 Create or Replace Trigger trg_test3 . 3 before Update of COL ON Table . 4 for each Row . 5 the begin . 6 IF : old.col > : new.col the then . 7 RAISE_APPLICATION_ERROR ( - 20002 , ' updated values less than the original value can not be updated! ' ); . 8 End IF ; . 9 End [ trg_test3 ] ;