Presentasjon lastes. Vennligst vent

Presentasjon lastes. Vennligst vent

Triggere Mutasjoner i basen. Triggers Triggers are stored procedures that execute automatically when something (event) happens in the database: : data.

Liknende presentasjoner


Presentasjon om: "Triggere Mutasjoner i basen. Triggers Triggers are stored procedures that execute automatically when something (event) happens in the database: : data."— Utskrift av presentasjonen:

1 Triggere Mutasjoner i basen

2 Triggers Triggers are stored procedures that execute automatically when something (event) happens in the database: : data modification (INSERT, UPDATE or DELETE) : schema modification : system event (user logon/logoff) Types of triggers : row-level triggers : statement-level triggers : BEFORE and AFTER triggers : INSTEAD OF triggers (used for views) : schema triggers : database-level triggers

3 The optional FOR EACH ROW clause is known as a row-level trigger (i.e., the rule is triggered separately for each tuple).The optional FOR EACH ROW clause is known as a row-level trigger (i.e., the rule is triggered separately for each tuple). If FOR EACH ROW clause was left out, the trigger would be known as a statement-level trigger (i.e., the rule would be triggered once for each triggering statement).If FOR EACH ROW clause was left out, the trigger would be known as a statement-level trigger (i.e., the rule would be triggered once for each triggering statement). The keywords NEW and OLD can only be used with row-level triggers.The keywords NEW and OLD can only be used with row-level triggers.

4 Rekkefølge Firing order: 1. Before Statement (once only) 2. Before Row (once per each affected row) 3. The actual DML statement 4. After Row (once per each affected row) 5. After Statement (once only)

5 Trigger Syntax CREATE OR REPLACE TRIGGER trigger-name BEFORE | AFTER DELETE | INSERT | UPDATE [OF column(s)] ON table-name [FOR EACH ROW [WHEN SQL-predicate] ] {PL/SQL block (SQL statements and PL/SQL extensions) };

6 Trigger Example -- this is an example of an update, after, row trigger CREATE TRIGGER give_bonus AFTER UPDATE OF sales ON salespeople FOR EACH ROW WHEN sales > BEGIN UPDATE commission SET bonus = bonus ; UPDATE commission SET bonus = bonus ; END; END;

7 Navnekonvensjon I FS har vi bare triggere for insert/update/delete. Vi gir dem disse prefixene:I FS har vi bare triggere for insert/update/delete. Vi gir dem disse prefixene: DAR – Delete After RowDAR – Delete After Row DAS – Delete After StatementDAS – Delete After Statement DBR – Delete Before RowDBR – Delete Before Row DBS – Delete Before StatementDBS – Delete Before Statement IAR – Insert After RowIAR – Insert After Row IAS – Insert After StatementIAS – Insert After Statement IBR – Insert Before RowIBR – Insert Before Row IBS – Insert before StatementIBS – Insert before Statement UAR – Update After RowUAR – Update After Row UAS – Update After StatementUAS – Update After Statement UBR – Update Before RowUBR – Update Before Row UBS – Update Before StatementUBS – Update Before Statement

8 Mutasjoner ORA-04091: table is mutating, trigger/function may not see itORA-04091: table is mutating, trigger/function may not see it The basic reason for this error is the way Oracle manages a read consistent view of data. The error is encountered when a row- level trigger accesses the same table on which it is based, while executing. The table is said to be mutating. Mutation will not occur if a single record is inserted in the table (using VALUES clause). If bulk insertion is done or data is inserted from another table mutation will occur.The basic reason for this error is the way Oracle manages a read consistent view of data. The error is encountered when a row- level trigger accesses the same table on which it is based, while executing. The table is said to be mutating. Mutation will not occur if a single record is inserted in the table (using VALUES clause). If bulk insertion is done or data is inserted from another table mutation will occur. The mutating error is not only encountered during queries, but also for insert, updates and deletes present in the trigger.The mutating error is not only encountered during queries, but also for insert, updates and deletes present in the trigger.

9 FS har en mutasjonspakke Bruker en plsqlpakke for hver tabell. Oppretter to lister: newListe og oldListe for gamle og nye rader. Disse listene blir populert i DBR/IBR/UBR-triggerene. I DAS/IAS/UAS-triggerene blir en prosedyre i pakken, for eksempel P_Vedlikehold_Insert, kallet. Disse løper da igjennom de to listene og gjør det triggeren skal gjøre

10 Eksempel på p_vedlikehold --| PROCEDURE P_Vedlikehold_Insert --| PROCEDURE P_Vedlikehold_Insert --| Vedlikeholder restriksjoner, kontroller på nye forekomster --| Vedlikeholder restriksjoner, kontroller på nye forekomster --| --| Procedure P_Vedlikehold_Insert Is Procedure P_Vedlikehold_Insert Is Begin Begin While newCount# > 0 Loop While newCount# > 0 Loop P_SjekkAktivitetFinnes(newCount#); P_SjekkAktivitetFinnes(newCount#); newCount#:=newCount# - 1; newCount#:=newCount# - 1; End Loop; End Loop; End P_Vedlikehold_Insert; End P_Vedlikehold_Insert;

11 Takk for oppmerksomheten!


Laste ned ppt "Triggere Mutasjoner i basen. Triggers Triggers are stored procedures that execute automatically when something (event) happens in the database: : data."

Liknende presentasjoner


Annonser fra Google