Trigger TRIGGER
Always poison not invade I unexpectedly in this joint eye on the cold, the head is also dizzy, a confused don't know what to think, thief uncomfortable, cough cough cough.
Today, another basic object in the Oralce database-triggers, if you understand the stored functions and stored procedures, it is particularly easy to understand triggers, because the trigger is also a piece of Pl/sql program block, there is a definition, there is a declaration, there is execution, etc. However, unlike a stored function or procedure, a procedure or function requires an outside call or an unsolicited call to execute, and a trigger is a trigger that has an event to start and run, that is, when an event occurs, an implicit trigger is executed, and the trigger cannot accept parameters.
Oracle events include insert update deletes for tables and corresponding actions on the view. This article is mainly about DML triggers, other alternative triggers and system triggers that need to be understood.
triggers 4 basic components:
(1) Trigger event: That is, when the event occurs, common events such as INSERT, update,delete;
(2) Trigger time: The time here is relative to the occurrence of the incident, can be defined before and after the event occurred before | After
(3) The trigger itself: this is the key, which is why we want to create this trigger, what to do with it, the body of the trigger, a PL/SQL program block
(4) Trigger frequency: Description within triggers, the frequency at which the event occurs, a statement-level trigger and a row-level trigger are common, and the statement level is executed once by the trigger, and the row-level trigger executes the trigger once per action line, and is defined with the need to add (for each row)
Basic format:
CREATE [OR REPLACE] TRIGGER trigger_name
{before | after}
{Insert | update | delete}
On table_name
[For each row]
[Where Condition]
Begin
Executive Body
End
Take a look at two examples directly.
Copy the data that will be deleted when one record of the employee table is deleted to the employee Delete table,
create table delemp as
select * from Emplooyees where 1=2;
------------------------------------------------
Create or replace trigger Copy_tri
before Trigger Delete on
employees for
each row//delete every line of data triggers a
begin
insert Into Delemp (employee_id,last_name,salary) VALUES (: old.employee_id,:old.last_name,:old.salary);
End;
Here's a mention of: old, and: New
: The value of the modifier access operation before it is completed;
: New represents the value after the modifier access operation completes.
When you delete tables and views, the corresponding triggers for that table are also deleted, and we can also use the statement
Drop Trigger trigger_name;
Follow up, have a bad headache ...