Unit 6 Database Triggers
Unit 6 Database Triggers
Triggers are stored programs that are fired automatically when some events occur. The code to
be fired can be defined as per the requirement.
Oracle has also provided the facility to mention the event upon which the trigger needs to be fire
and the timing of the execution.
A trigger can include SQL and PL/SQL statements to execute as a unit and can invoke
stored procedures. However, procedures and triggers differ in the way that they are
invoked. While a procedure is explicitly executed by a user, application, or trigger,
one or more triggers are implicitly fired (executed) by Oracle when a triggering
INSERT, UPDATE, or DELETE statement is issued, no matter which user is
connected or which application is being used.
For example, Figure shows a database application with some SQL statements that
implicitly fire several triggers stored in the database.
Notice that triggers are stored in the database separately from their associated tables.
Triggers can be defined only on tables, not on views. However, triggers on the base table(s) of a
view are fired if an INSERT, UPDATE, or DELETE statement is issued against a view.
Benefits of Triggers
Following are the benefits of triggers.
Generating some derived column values automatically
Enforcing referential integrity
Event logging and storing information on table access
Auditing
Synchronous replication of tables
Imposing security authorizations
Preventing invalid transactions
6.1 Use of Database Trigger
In many cases, triggers supplement the standard capabilities of Oracle to provide a highly
customized database management system. For example, a trigger can permit DML operations
against a table only if they are issued during regular business hours. The standard security
features of Oracle, roles and privileges, govern which users can submit DML statements against
the table. In addition, the trigger further restricts DML operations to occur only at certain times
during weekdays. This is just one way that you can use triggers to customize information
management in an Oracle database.
In addition, triggers are commonly used to
automatically generate derived column values
prevent invalid transactions
enforce complex security authorizations
enforce referential integrity across nodes in a distributed database
enforce complex business rules
provide transparent event logging
provide sophisticated auditing
maintain synchronous table replicates
gather statistics on table access
6.2 Database Triggers Vs Procedure
1. We can execute a stored procedure whenever we want with the help of
the exec command, but a trigger can only be executed whenever an event (insert, delete,
and update) is fired on the table on which the trigger is defined.
2. We can call a stored procedure from inside another stored procedure but we can't directly
call another trigger within a trigger. We can only achieve nesting of triggers in which the
action (insert, delete, and update) defined within a trigger can initiate execution of
another trigger defined on the same table or a different table.
3. Stored procedures can be scheduled through a job to execute on a predefined time, but we
can't schedule a trigger.
4. Stored procedure can take input parameters, but we can't pass parameters as input to a
trigger.
5. Stored procedures can return values but a trigger cannot return a value.
6. We can use Print commands inside a stored procedure for debugging purposes but we
can't use print commands inside a trigger.
7. We can use transaction statements like begin transaction, commit transaction, and
rollback inside a stored procedure but we can't use transaction statements inside a trigger.
8. We can call a stored procedure from the front end (.asp files, .aspx files, .ascx files, etc.)
but we can't call a trigger from these files.
9. Stored procedures are used for performing tasks. Stored procedures are normally used for
performing user specified tasks. They can have parameters and return multiple results
sets.
10. The Triggers for auditing work: Triggers normally are used for auditing work. They can
be used to trace the activities of table events.
6.3 Triggers vs. Declarative Integrity Constraints
Triggers and declarative integrity constraints can both be used to constrain data input. However,
triggers and integrity constraints have significant differences.
A declarative integrity constraint is a statement about the database that is never false while the
constraint is enabled. A constraint applies to existing data in the table and any statement that
manipulates the table.
Triggers constrain what transactions can do. A trigger does not apply to data loaded before the
definition of the trigger. Therefore, it does not guarantee all data in a table conforms to its rules.
A trigger enforces transitional constraints; that is, a trigger only enforces a constraint at the time
that the data changes. Therefore, a constraint such as "make sure that the delivery date is at least
seven days from today" should be enforced by a trigger, not a declarative integrity constraint.
Parts of a Trigger
A trigger has three basic parts:
a triggering event or statement
a trigger restriction
a trigger action
Figure 15 - 3 represents each of these parts of a trigger and is not meant to show exact syntax.
Each part of a trigger is explained in greater detail in the following sections.
Disable a Trigger
Syntax
The syntax for a disabling a Trigger in Oracle/PLSQL is:
ALTER TRIGGER trigger_name DISABLE;
For example:
ALTER TRIGGER orders_before_insert DISABLE;
Disable all Triggers on a table
Syntax
The syntax for a disabling all Triggers on a table in Oracle/PLSQL is:
ALTER TABLE table_name DISABLE ALL TRIGGERS;
For example:
ALTER TABLE orders DISABLE ALL TRIGGERS;
Enable a Trigger
Syntax
The syntax for a enabling a Trigger in Oracle/PLSQL is:
ALTER TRIGGER trigger_name ENABLE;
For example:
ALTER TRIGGER orders_before_insert ENABLE;