Documente Academic
Documente Profesional
Documente Cultură
Triggers are stored procedures that are ran automatically by the database
whenever some event happens.
PL/SQL Triggers
Triggers are basically PL/SQL procedures that are associated with tables, and
they are called whenever a modification or event occurs. The modification
statements may include
INSERT
UPDATE and
DELETE.
Explanation:
Note:
By dinesh
Insert Triggers:
Update Triggers:
Delete Triggers:
Drop Triggers:
Drop a Trigger
Disable/Enable Triggers:
Disable a Trigger
Enable a Trigger
By dinesh
A BEFORE INSERT Trigger means that Oracle will fire this trigger before the INSERT
operation is executed.
Restrictions:
• You can not create a BEFORE trigger on a view.
• You can update the :NEW values.
• You can not update the :OLD values.
Table Used.
Output:
By dinesh
Output:
By dinesh
begin
dbms_output.put_line('Warning : before update trigger --> date
changes');
end;
Now creating a query to update all the date in the table usr.
Output
By dinesh
Before:
After:
create or replace trigger t5
before delete
on usr
begin
dbms_output.put_line('Warning : after delete trigger --> data
lost / deleted');
end;
Output:
By dinesh
Output:
By dinesh
Create a view:
By dinesh
Output:
Note:
When we try to insert a value into the view, it raises a trigger that we cannot insert
anything into the view.
By dinesh
For example, to prevent some action that improperly modifies the database.
CREATE OR REPLACE
TRIGGER t11
BEFORE UPDATE OF udate ON usr1
FOR EACH ROW
BEGIN
RAISE_APPLICATION_ERROR(-20000,’CANNOT CHANGE DATE’);
END;
Note:
The next thing you should notice is the procedure call RAISE APPLICATION ERROR,
which accepts an error code, and an explanation string. This effectively halts our trigger
execution, and raises an error, preventing our UDATE from being modified.
VIEWING TRIGGERS
Output:
By dinesh
See the above output. Now I am going to delete the trigger T_ALL.
Output:
By dinesh
If a trigger seems to be getting in the way, and you don’t want to drop it, just disable it
for a little while, you can alter it to disable it. Note that this is not the same as dropping a
trigger; after you drop a trigger, it is gone.
Consider Trigger
Disable a Trigger:
Output:
By dinesh
Enable a Trigger:
By dinesh