ABHIRAMI T Triggers In PL/SQL A stored program that is fired by default or by some events is called a trigger. A trigger is executed due to the following circumstances listed below:
• By a DDL (Data Definition Language) statement like DROP, ALTER, or
CREATE. • By a DML (Data Manipulation Language) statement like UPDATE, INSERT, or DELETE. • By a database action like SHUTDOWN, STARTUP, LOGOFF, and LOGON. A trigger can be set on a schema, view, or database that has an event attached. Advantages Of Triggers
• Ability to enforce referential integrity.
• The ability of monitoring. • Ability to log and hold data on accessing tables. • Ability to stop transactions that are not valid. • Ability to enforce security features. • Ability to produce derived column values by default. Usages Of Triggers
• Prevents improper transactions.
• Accumulates information on table usage. • Monitor critical information. Types Of Triggers In PL/SQL
#1) Categorization on the trigger level.
• ROW Level trigger: It gets executed for each record that
got updated by a DML statement.
• STATEMENT Level trigger: It gets executed only once by
the event statement. #2) Categorization of the trigger timing.
• BEFORE trigger: It gets executed prior to the specific
event that has taken place. • AFTER trigger: It gets executed post the specific event that has taken place. • INSTEAD OF trigger: It is a special type of trigger and it gets executed for each record that got updated by a DML statement #3) Categorization of the trigger event.
• DML trigger: It gets executed if a DML event like an
UPDATE, INSERT, or DELETE is performed. • DDL trigger: It gets executed if a DDL event like a DROP, ALTER, or CREATE is performed. • DATABASE trigger: It gets executed if a database event like SHUTDOWN, STARTUP, LOGOFF, and LOGON has taken place. CREATE [OR REPLACE ] TRIGGER trigger_n {BEFORE | AFTER | INSTEAD OF } {INSERT [OR] | UPDATE [OR] | DELETE} [OF column_n] ON table_n [REFERENCING OLD AS o NEW AS n] [FOR EACH ROW] WHEN (condition) DECLARE << Declaration statement >> BEGIN << Block of executable code>> EXCEPTION << Exception handling if any >> END; • CREATE [OR REPLACE] TRIGGER trigger_n – This is for creating, replacing, or updating a trigger having a name as trigger_n. • {BEFORE | AFTER | INSTEAD OF} – This is for determining the time when the trigger will be fired. The INSTEAD OF is for creating a trigger that has a view. • {INSERT [OR] | UPDATE [OR] | DELETE} – This is for executing the DML actions. • [OF column_n] – This is for mentioning the column name that shall be modified. • [ON table_n] – This is for mentioning the table name that is attached to the trigger. • [REFERENCING OLD AS o NEW AS n] – This is for referring to the old and new values by the DML statement like UPDATE, INSERT or DELETE. • [FOR EACH ROW] – This determines a row-level trigger, i.e., the trigger will be fired for each row that is modified, else the trigger will fire just once when the SQL statement is executed, which is known as a table-level trigger. • WHEN (condition) – This gives a condition for rows for which the trigger would be executed. This is applicable only row-level triggers. • ALTER TRIGGERtrigger_n[ENABLE|DISABLE];