Trigger :
Triggers in Oracle are blocks of code which Oracle automatically executes based on some action or event on a table.
They are mainly used for following tasks -
Restrictions On Trigger :
Although Triggers are very powerful objects but still they suffers from few restrictions -
A "Constraining table" on the other hand is one which is serving as 'parent-table' for the 'mutating table'.
Considering the above tables, the restrictions are -
Example :
Whenever we try to access the column of the mutating-table form trigger's body, Oracle have encountered :
ORA-04091 (table xxx is mutating. Trigger/function might not see it)
Also, please keep in mind following points :
Triggers in Oracle are blocks of code which Oracle automatically executes based on some action or event on a table.
They are mainly used for following tasks -
- Applying complex-integrity-constraints which is not possible through complex approach of applying constraint.
- Performing audit-trail i.e. recording changes made by other users on a table.
- Generating Primary-key values automatically.
Restrictions On Trigger :
Although Triggers are very powerful objects but still they suffers from few restrictions -
- A Trigger can't issue any TCL-command (commit, rollback, savepoint). This is because trigger is never executed on its own rather it is always fired due to execution of a DML-statement (Insert, Update, Delete). Now at the server-end both, the trigger & the triggering-event are merged as a single block i.e. Server sees both of them as a single unit of transaction.Thus trigger alone can't use COMMIT or ROLLBACK. So when at the SQL prompt, the triggering statement is committed or rolled-back automatically.
- If the trigger from its body is calling any stored procedure or function then that function or procedure also can't use commit or rollback.
- There are restrictions on the table which a trigger can access. But to understand these restrictions, we will have to understand two terms, called Mutating Table and Constraining Table.
A "Constraining table" on the other hand is one which is serving as 'parent-table' for the 'mutating table'.
Considering the above tables, the restrictions are -
- A trigger can't access any column of the mutating-table from its body. In other words,
- A row-level trigger cannot query or modify a mutating table. (Of course, NEW and OLD still can be accessed by the trigger) .
- A statement-level trigger must not query or modify a mutating table if the trigger is fired as the result of a CASCADE delete. (A foreign key with a cascade delete means that if a record in the parent table is deleted, then the corresponding records in the child table with automatically be deleted. This is called a cascade delete.)
- The statements of a trigger cannot change the PRIMARY, FOREIGN, or UNIQUE KEY columns of a constraining table of the triggering statement. However, staring with Oracle 8i, there is no such restriction.
Example :
Whenever we try to access the column of the mutating-table form trigger's body, Oracle have encountered :
ORA-04091 (table xxx is mutating. Trigger/function might not see it)
Also, please keep in mind following points :
- The trigger body cannot declare any LONG or LONG RAW variables. Also, :new and :old cannot refer to a LONG or LONG RAW column in the table for which the trigger is defined.
- Code in a trigger-body may reference and use LOB (Large Object) columns, but it may not modify the values of the columns.
Question: How many triggers can be applied to a table?
Answer: A maximum of 12 triggers can be applied to one table.
Question: Explain the uses of database trigger.
Answer: A PL/SQL program unit associated with a particular database table is called a database trigger. It is used for :
1)Audit data modifications.
2)Log events transparently.
3)Enforce complex business rules.
4)Maintain replica tables
5)Derive column values
6)Implement Complex security authorizations