Beruflich Dokumente
Kultur Dokumente
e frontend):
---------------------------------------------------------------------------------Database Trigger is Event Based database Object From PL/SQL. This Object is used
to apply business rules on database. Business rules are user specific validatio
n.
Database Trigger execution is decided by Oracle server based on the event define
d for trigger. These events are (i.e)
Database Events are categorised into four groups:
------------------------------------------------------------------1)DML Events:(Used On Table)
-INSERT
-UPDATE
-DELETE
2)Instead Of Event (Used On view)
3)DDL Events:(Used On Any Database Object)
-CREATE
-ALTER
-DROP
4)Database Events:
-After Startup
-Before Shutdown
-After Login
-Before Logout
-ON Server Error.
DML Triggers:
==========
*)Are Used to protect Data Manipulation on tables.
*)DML Triggers have:
-->Two Execution Time --> BEFORE /AFTER EVENT
--> Three Events (INSERT or DELETE or UPDATE)
( INSERT or DELETE)
( INSERT or UPDATE)
(UPDATE or DELETE)
-->Two Types (Statement Trigger(table level trigger) /
Row Trigger)
--> Trigger Condition:(is not allowed for Table
Trigger)
If the trigger is row trigger it is executed. If trigger
condition is True,then only trigger body
nonymous Block) is processed.
(A
*) For DML Trigger PL/SQL provides Three variables to know the Current Event :Th
ey are
-->INSERTING
-->DELETING
-->UPDATING
all the above are boolean variables and any one
will be true.
*)For Row Trigger Server
ues. These variables are
--> OLD(%ROWTYPE
--> NEW(%ROWTYPE
Trigger dropped.
Trigger Created
SQL> update emp set sal=4000;
update emp set sal=4000
*
ERROR at line 1:
ORA-20003: No Update for salary
ORA-06512: at "SIVA1.CHECK_EMP", line 12
ORA-04088: error during execution of trigger 'SIVA1.CHECK_EMP'
SQL> CREATE TABLE deptback
2 as
3 SELECT * FROM dept WHERE 1=2;
Table created.
Note: For deleted records we have to dump in a separate table.
For that we need another trigger i.e given below.
CREATE OR REPLACE TRIGGER Backup_Dept
AFTER DELETE ON dept
FOR EACH ROW
BEGIN
INSERT INTO deptback(deptno,dname,loc)
VALUES(:OLD.deptno,:OLD.dname,:old.loc);
END;
/
Note:
-----Maximum Number of Triggers on a table:
-----------------------------------------------------BEFORE INSERT FOR STATEMENT
AFTER INSERT FOR STATEMENT
BEFORE DELETE FOR STATEMENT
AFTER DELETE FOR STATEMENT
BEFORE UPDATE FOR STATEMENT
AFTER UPDATE FOR STATEMENT
BEFORE
AFTER
BEFORE
AFTER
BEFORE
AFTER
Instead Of Trigger:
==============
*)This Trigger is used on view Object to allow data manipulation on the base ta
ble(s).
*)Within the trigger we have to write DML commands to manipulate the base table(
s)
*)This trigger always fire BEFORE the event and it is always
ROW TRIGGER.
WHERE deptno=pDeptno;
RETURN (vTemp);
END Count_emp_for_deptno;
checking:
-----------SQL> SELECT Count_emp_for_deptno(10) FROM Dual;
COUNT_EMP_FOR_DEPTNO(10)
-----------------------4