ORACLE database triggers [Reserved]

Original link: http://www.cnblogs.com/happylyyer/p/4329885.html

ORACLE actually trigger PL / SQL block, which is similar to stored procedures and functions, but one thing different is that the trigger is implicitly called, and can not receive parameters ORACLE triggers There are three types, namely:. DML triggers an alternative trigger and trigger system. The following describes these three types one by one

1.DML trigger

  As the name suggests, a DML trigger is triggered DML statements. Databases such as INSERT / UPDATE / DELETE operations can trigger this type of trigger. They can be triggered before or after these statements, or trigger level in the row (that is to say for each affected row trigger once)

For example, we have a table TABLE1, a total of three fields, ID, name, age, when we want ID can be generated automatically when inserted, you can set up a trigger

 

CREATE OR REPLACE TRIGGER TR_INSERT_ID BEFORE INSERT ON  TABLE1 BEGIN SELECT  T.NEXTVAL INTO :NEW.ID FROM DUAL; END;

 

So when we insert the table when, ID fields are automatically populated.

2. Alternative trigger

Alternatively the trigger can only be used in view, the DML is different, DML trigger is run outside of the DML, but instead its trigger is a DML statement in place of the excitation run Alternatively trigger is a row-level, for example , there is a view:

- Examples of sources insteadOf.sql

 

CREATE OR REPLACE VIEW classes_rooms AS SELECT department, course, building, room_numbe FROM rooms, classes WHERE rooms.room_id = classes.room_id;

 

Direct view of insertion, illegal, because the insertion operation requires both tables to be modified, e.g.

- Examples of sources insteadOf.sql

 

If you use alternative

INSERT INTO classes_rooms (department, course, building, room_number) 2    VALUES ('MUS', 100, 'Music Building', 200); INSERT INTO classes_rooms (department, course, building, room_number) * ERROR at line 1: ORA-01732: data manipulation operation not legal on this view

Flip-flop, then we can solve this problem:

 

CREATE TRIGGER ClassesRoomsInsert INSTEAD OF INSERT ON classes_rooms DECLARE v_roomID rooms.room_id%TYPE; BEGIN -- First determine the room ID SELECT room_id INTO v_roomID FROM rooms WHERE building = :new.building AND room_number = :new.room_number; -- And now update the class UPDATE CLASSES SET room_id = v_roomID WHERE department = :new.department AND course = :new.course; END ClassesRoomsInsert;

The trigger can work properly

3. System triggers

This occurs when the trigger is turned on or off as database systems such events did not occur in the implementation of DML statement, of course, can also be triggered when DDL.

 

No matter which one of these three, created the syntax is the same.

 

CREATE [OR REPLACE] TRIGGER trigger_name ...{BEFORE | AFTER | INSTEAD OF} triggering_event referencing_clause [WHEN trigger_condition] [FOR EACH ROW] trigger_body;

Which, trigger _ name is the name of a trigger, triggering _ event describes the event that fired the trigger (may also include special table or view), trigger _ body is the trigger code. referencing _ clause is used to reference the data in the row in the modified state, if the trigger specified for condition Condition _ WHEN clause, then the first condition is evaluated. Trigger body run only if the condition is true value.

DML triggers for us because developers are the most common, so first of all in detail here DML triggers explain in detail:

DML trigger is a database INSERT / UPDATE / DELETE operation triggers. Such triggers can be performed before or after the above statement, the row may be performed for each affected time, the combination of these conditions, it can be a combination of 12 kinds type of DML triggers. here is a table gives the DML trigger

             Table 6-1 DML trigger type Type Value Description statement INSE RT, DELETE, the definition of what kind of DML statement prior to the excitation trigger UPD AT E timed or after the trigger is defined excitation level before and after the statement runs or run lines or statement if trigger it is a row-level trigger that change on each row triggered by the statement once excited. If the trigger is a statement-level trigger, the trigger will excite once before or after the statement. Row-level triggers are defined by the flip-flop in the FOR EACH ROW clause indicates that the

One thing to note is: A trigger is a trigger that type of statement together as a mistake to run.

Excitation trigger is a DML statement run time, if a plurality of triggers, the order of execution is as follows: 1) If there is, then before the statement level trigger, the trigger 2 first run) for each row affected by the statement: a. If there before row-level trigger, then run the trigger. b. Execute the statement itself. c. If there is after row-level trigger, then run the trigger. 3) If there is after statement-level triggers, then run the trigger

And for row-level triggers, by: accessing a row of data being processed for DML trigger OLD flag, meaning that two identifiers as follows: NEW and:

Triggering statement identifier: old identifier: new INSE RT undefined - all the fields blank NULL original value of the original value before the statement at the end of the updated value of the DELETE to delete the row that will be inserted before the end of this statement is to update row value UPDAE no definition - all the fields blank NULL

Note Identifier: old no definition of INSE RT statements and identifiers: new definition of no DELETE statement. PL / SQL compiler will not be used in INSE RT statement: old and used in the DELETE statement: new identifier error, compile the results will enable the field is empty. At the same time, then we can not row-level triggers change: new, the reason is that the statement has been processed. In general, to: modify before new changes only at the row level triggers. : Old read-only attribute can only be read.

and new: old just inside the row level trigger legal. If you attempt to reference in the statement-level trigger of the: new or: old, then the compiler generates an error. Since the statement-level triggers run only once, even if there are many statements to be treated OK, then, new and: old are also not defined. The compiler does not know that reference the line

 

WHEN clause WHEN clause applies only to row-level triggers. If this clause, then the trigger member will only satisfy the condition described by the WHEN clause rows executed. WHEN clause syntax is: WHEN trigger_condition which, trigger _ condition is a logical expression. The expression is evaluated for each row. : New and: old record can be referenced within the trigger _ condition, but without the use of a colon. The trigger is valid only in the colon in vivo. For example, the flip-flop C heck C redits body only when 0 exceeds 2 credits run current students obtained: CREATE OR REPLACE TRIGGER CheckCredits BEFORE INSERT OR UPDATE OF current_credits ON students FOR EACH ROW WHEN (new.current_credits> 20) BEGIN . / * Trigger body goes here * / END;

Trigger predicate: INSE RT ING, UPD AT ING DELETING and internal triggers (different excitation trigger DML statement) has three logic expressions which can be used to confirm the operation to be performed. These predicate expression is INSE RT ING, UPD AT ING, DELETING. The following explains the meaning of each predicate. INSE RT ING predicate expression state if the triggering statement is INSE RT, then, for the true value (TRUE), otherwise FA LSEUPD AT ING if the triggering statement is UPD AT E, then, for the true value (TRUE), otherwise FA LSEDELETING If the trigger is a DELETE statement, then, for the true value (tRUE), otherwise FA LSE

Reproduced in: https: //www.cnblogs.com/happylyyer/p/4329885.html

Guess you like

Origin blog.csdn.net/weixin_30627341/article/details/94783581