11303 dbms chap_02_triggers (2)

25
TRIGGERS

Transcript of 11303 dbms chap_02_triggers (2)

Page 1: 11303 dbms chap_02_triggers (2)

TRIGGERS

Page 2: 11303 dbms chap_02_triggers (2)

Definition• A Trigger is a statement that the system

executes automatically as a side effect of a modification to the database.

Page 3: 11303 dbms chap_02_triggers (2)

To Design a trigger mechanism, we must meet two requirements:

1. Specify when a trigger is to be executed. This a broken up into an event that causes the trigger to be checked and a condition that must be satisfied for trigger execution to proceed.

2. Specify the actions to be taken when the trigger executes.

The above model of triggers is referred to as the event-condition-action-model for triggers.

Page 4: 11303 dbms chap_02_triggers (2)

• The database stores triggers just as if they were regular data, so that they are persistent and are accessible to all database operations.

• Once we enter a trigger into the database, the database system takes on the responsibility to executing it whenever the specified event occurs and the corresponding condition is satisfied.

Page 5: 11303 dbms chap_02_triggers (2)

Database Triggers Vs. Procedures• The oracle engine allows the user to define procedures that

are implicitly executed, when an INSERT,UPDATE od DELETE is issued against a table from SQL*Plus or through an application. These procedures are called database Triggers.

• There are very few differences between these database triggers and procedures

1. Triggers do not accept parameters whereas Procedures can.

2. A Trigger is executed implicitly by the Oracle engine itself upon modification of an associated table or its data. To execute a Procedure, it has to be explicitly called by the user.

Page 6: 11303 dbms chap_02_triggers (2)

How to Apply Database Triggers

• A Trigger has three basic parts:

1.A triggering event or statement.2.A Trigger restriction.3.A Trigger action.

Page 7: 11303 dbms chap_02_triggers (2)

A triggering event or statement.

• It is a SQL statement that causes a trigger to be fired. It can be INSERT, UPDATE or DELETE statement for a specific table.

Page 8: 11303 dbms chap_02_triggers (2)

Trigger Restriction

• A Trigger restriction specifies a Boolean (Logical) expression that must be TRUE for trigger to fire. It is an option available for triggers that are fired for each row.

• Its function is to conditionally control the execution of a trigger.

• A trigger restriction is specified using a WHEN clause.

Page 9: 11303 dbms chap_02_triggers (2)

Trigger Actions

• A trigger action is the PL/SQL code to be executed when a triggering statement is encountered and any trigger restriction evaluates to TRUE.

Page 10: 11303 dbms chap_02_triggers (2)

Types of Triggers

• While defining a trigger, the number of times the trigger action is to be executed can be specified.

1.Row Triggers2.Statement Triggers3.Before Vs. After Trigger.4.Combinations Trigger.

Page 11: 11303 dbms chap_02_triggers (2)

Row Triggers

• A row trigger is fired each time a row in the table is affected by the triggering statement.

• For e.g., if an UPDATE statement updates multiple rows of a table, a row trigger is fired once for each row effected by the UPDATE statement. If the triggering statement affects no rows, the trigger is not executed at all.

• Row triggers should be used when some processing is required whenever a triggering statement affects a single row in a table.

Page 12: 11303 dbms chap_02_triggers (2)

Statement Triggers

• A statement trigger is fired once on behalf of the triggering statement, independent of the number of rows the triggering statement affects(even if no rows are affected).

• Statement triggers should be used when a triggering statement affects rows in a table but the processing required is completely independent of the number of rows affected.

Page 13: 11303 dbms chap_02_triggers (2)

Before Vs. After Triggers

• When defining a trigger it is necessary to specify the trigger timing, i.e. specifying when the triggering action is to be executed in relation to the triggering statement.

• BEFORE and AFTER apply to both row and the statement triggers.

Page 14: 11303 dbms chap_02_triggers (2)

Before Trigger

• BEFORE triggers execute the trigger action before the triggering statement. These types of triggers are commonly used in the following situations:

1. BEFORE triggers are used when the trigger action should determine whether or not the triggering statement should be allowed to complete. By using a BEFORE trigger, you can eliminate unnecessary processing of the triggering statement.

2. BEFORE triggers are used to derive specific column values before completing a triggering INSERT or UPDATE statement.

Page 15: 11303 dbms chap_02_triggers (2)

After Triggers

• After triggers executes the trigger action after the triggering statement is executed. These types of triggers are commonly used in the following situations:

1. AFTER triggers are used when you want the triggering statement to complete before executing the trigger action.

2. If a BEFORE trigger is already present , an AFTER trigger can perform different actions on the same triggering statement.

Page 16: 11303 dbms chap_02_triggers (2)

Combinations Trigger.• Using the above options, four types of triggers could be created. BEFORE Statement Trigger: Before executing the triggering

statement, the trigger action is executed BEFORE Row Trigger: Before modifying each row affected by the

triggering statement and before appropriate integrity constraints, the trigger is execute if the trigger restriction either evaluates to TRUE or was not included.

AFTER Statement Trigger: After executing the triggering statement and applying any integrity constraint, the trigger action is executed.

AFTER Row Trigger: After modifying each row affected by the triggering statement and possibly applying appropriate integrity constraints, the trigger action is executed for the current row, if the trigger restriction either evaluates to TRUE or was not included. Unlike BEFORE row trigger, AFTER row trigger have rows locked.

Page 17: 11303 dbms chap_02_triggers (2)

The Generic PL/SQL Block• The minimum sections of a PL/SQL blocks are:• The Declare section• The Master Begin and END section that contains the Exception

section.1. The Declare Section: Code block starts with a declaration section, in

which memory variable & other oracle objects can be declared, & if required intialized. Once declared they can be used in the SQL statements for data manipulation.

2. The Begin Section: It consist of a set of SQL and PL/SQL statements, which describe processes that have to be applied to the data. Actual data manipulation, retrieval, looping & branching constructs are specified in this section.

3. The Exception Section: This section deals with handling of errors that arise during execution of the data manipulation statements. Errors can arise due to syntax, logic and/or validation rule violation.

4. The End Section: This marks the end of a PL/SQL block.

Page 18: 11303 dbms chap_02_triggers (2)

PL/SQL Block Structure

Page 19: 11303 dbms chap_02_triggers (2)

How To Create A Trigger?• Syntax for Creating a Trigger:CREATE OR REPLACE TIRGGER triggername{BEFORE, AFTER}{DELETE,INSERT,UPDATE [OF column,…]}ON tablename[REFERENCING {OLD AS old NEW AS new}][FOR EACH ROW [WHEN condition]]DECLAREVariable declarations;Constant declarations;BEGINPL/SQL subprogram body;EXCEPTIONException PL/SQL block;END;

Page 20: 11303 dbms chap_02_triggers (2)

Deleting A Trigger

• SyntaxDROP TRIGGER triggername;

Page 21: 11303 dbms chap_02_triggers (2)

Example

• Create a transparent audit system for a table Client_master. The system must keep track of the records that are being deleted or updated. The functionality being when a record is deleted or modified the original record details and the date of operation are stored in the audit table, then the delete or update is allowed to go through

Page 22: 11303 dbms chap_02_triggers (2)

Table: Client_masterColumn Name Data Type Size Attributes

Client_no Varchar2 6 Primary Key/First letter must start with ‘C’

Name Varchar2 20 Not Null

Address Varchar2 30

City Varchar2 30

State Varchar2 15

Pincode Number 6

Bal_Due Number 10,2

Page 23: 11303 dbms chap_02_triggers (2)

Table: AuditclientColumn Name Data Type Size Attributes

Client_no Varchar2 6

Name Varchar2 20

Bal_Due Number 10,2

Operation Varchar2 8

Userid Varchar2 20

Odate Date

Where:Operation : The operation performed on the client_master table.Odate : The date when the operation was performed.Userid : Then name of the user performing the operation.

Page 24: 11303 dbms chap_02_triggers (2)

CREATE TRIGGER audit_trailAFTER UPDATE OR DELETE ON client_masterFOR EACH ROWDECLARE

Oper varchar2(8);Client_no varchar2(6);Name varchar2(20);Bal_due number (10,2);

BeginIF updating THEN

Oper:=‘update’;END IF;IF deleting THEN

Oper:=‘delete’;END IF;

Clent_no :=:old.client_no;Name :=: old.name;bal_due :=: old.bal_due;INSERT INTO auditclientVALUES (client_no, name, bal_due, oper, user, sysdate);

END;

Page 25: 11303 dbms chap_02_triggers (2)