Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
108 views

Triggers - DBMS

Triggers are stored PL/SQL blocks that are automatically executed or "triggered" in response to certain events on the database tables, such as when a row is inserted, updated or deleted. Triggers can be defined to execute before or after these events and on a statement level or row level. The main types of triggers are before and after triggers, which specify if the trigger should execute before or after the triggering event, as well as statement and row level triggers, which determine if the trigger should execute once per statement or row event. Triggers allow programmatic control and validation of DML statements on database tables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

Triggers - DBMS

Triggers are stored PL/SQL blocks that are automatically executed or "triggered" in response to certain events on the database tables, such as when a row is inserted, updated or deleted. Triggers can be defined to execute before or after these events and on a statement level or row level. The main types of triggers are before and after triggers, which specify if the trigger should execute before or after the triggering event, as well as statement and row level triggers, which determine if the trigger should execute once per statement or row event. Triggers allow programmatic control and validation of DML statements on database tables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

11/5/2018 Triggers | DBMS | Tutorialink.

com

Triggers
A trigger is a pl/sql block structure which is fired when a DML statements like Insert, Delete, Update is executed on a database
table. A trigger is triggered automa cally when an associated DML statement is executed.

Syntax:
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
BEGIN
--- sql statements
END;

Each statements are described below :

Statement Descrip on

CREATE [OR This clause creates a trigger with the given name or
REPLACE ] overwrites an exis ng trigger with the same name.
TRIGGER
trigger_name

{BEFORE | This clause indicates at what me the trigger should get


AFTER | fired. i.e for example: before or a er upda ng a table.
INSTEAD OF } INSTEAD OF is used to create a trigger on a view. before
and a er cannot be used to create a trigger on a view.

{INSERT [OR] | This clause determines the triggering event. More than
UPDATE [OR] | one triggering events can be used together separated by
DELETE} OR keyword. The trigger gets fired at all the specified
triggering event.

[OF col_name] This clause is used with update triggers. This clause is used
when you want to trigger an event only when a specific
column is updated.

CREATE [OR This clause creates a trigger with the given name or
REPLACE ] overwrites an exis ng trigger with the same name.
TRIGGER
trigger_name

[ON This clause iden fies the name of the table or view to
table_name] which the trigger is associated.

https://tutorialink.com/dbms/triggers.dbms 1/4
11/5/2018 Triggers | DBMS | Tutorialink.com

[REFERENCING This clause is used to reference the old and new values of
OLD AS o NEW the data being changed. By default, you reference the
AS n] values as :old.column_name or :new.column_name. The
reference names can also be changed from old (or new) to
any other user-defined name. You cannot reference old
values when inser ng a record, or new values when
dele ng a record, because they do not exist.

[FOR EACH This clause is used to determine whether a trigger must


ROW] fire when each row gets affected ( i.e. a Row Level Trigger)
or just once when the en re sql statement is
executed(i.e.statement level Trigger).

WHEN This clause is valid only for row level triggers. The trigger is
(condi on) fired only for rows that sa sfy the condi on specified.

Example:
Create or replace Trigger np before insert on account for each row
Begin
IF :NEW.bal < 0 THEN
DBMS_OUTPUT.PUT_LINE('BALANCE IS NAGATIVE..');
END IF;
End;
/

Output:
Run SQL Command Line

SQL>start D://t.sql
Trigger created.

SQL>insert into account values(1,-100);


BALANCE IS NAGATIVE..
1 row created.

Types of Triggers
A trigger's type is defined by the type of triggering transac on and by the level at which the trigger is executed. Oracle has the
following types of triggers depending on the different applica ons.

Row Level Triggers


Statement Level Triggers
Before Triggers
A er Triggers

Row Level Triggers :

https://tutorialink.com/dbms/triggers.dbms 2/4
11/5/2018 Triggers | DBMS | Tutorialink.com

Row level triggers execute once for each row in a transac on.
The commands of row level triggers are executed on all rows that are affected by the command that enables the trigger.
For example, if an UPDATE statement updates mul ple rows of a table, a row trigger is fired once for each row affected by the
UPDATE statement.
If the triggering statement affects no rows, the trigger is not executed at all.
Row level triggers are created using the FOR EACH ROW clause in the CREATE TRIGGER command.

Applica on:
Consider a case where our requirement is to prevent upda on of empno 100 record cannot be
updated, then whenever UPDATE statement update records, there must be PL/SQL block that will be fired automa cally by
UPDATE statement to check that it must not be 100, so we have to use Row level Triggers for that type of applica ons.

Statement Level Triggers :


Statement level triggers are triggered only once for each transac on.
For example when an UPDATE command update 15 rows, the commands contained in the trigger are executed only once,
and not with every processed row.
Statement level trigger are the default types of trigger created via the CREATE TRIGGER command.

Applica on:
Consider a case where our requirement is to prevent the DELETE opera on during Sunday. For this whenever
DELETE statement deletes records, there must be PL/SQL block that will be fired only once by DELETE statement to check that
day must not be Sunday by referencing system date, so we have to use Statement level Trigger for which fires only once for
above applica on.

Before Trigger :
Since triggers are executed by events, they may be set to occur immediately before or a er those events.
When a trigger is defined, you can specify whether the trigger must occur before or a er the triggering event i.e. INSERT,
UPDATE, or DELETE commands.
BEFORE trigger execute the trigger ac on before the triggering statement.
These types of triggers are commonly used in the following situa on:
1. BEFORE triggers are used when the trigger ac on should determine whether or not the triggering statement should be
allowed to complete.
2. By using a BEFORE trigger, you can eliminate unnecessary processing of the triggering statement.
3. For example: To prevent dele on on Sunday, for this we have to use Statement level before trigger on DELETE statement.
4. BEFORE triggers are used to derive specific column values before comple ng a triggering INSERT or UPDATE statement.

A er Trigger :
AFTER trigger executes the trigger ac on a er the triggering statement is executed.
AFTER triggers are used when you want the triggering statement to complete before execu ng the trigger ac on.
For example: To perform cascade delete opera on, it means that user delete the record fro one table, but the corresponding
records in other tables are delete automa cally by a trigger which fired a er the execu on of delete statement issued by the
user.
When combining the different types of triggering ac ons, there are mainly 4 possible Valid trigger types available to us.

The possible configura ons are:


BEFORE statement Trigger
BEFORE row Trigger
AFTER statement Trigger
AFTER row Trigger

Dropping Triggers :
Triggers may be dropped via the drop trigger command. In order to drop a trigger, you must either own the trigger or have the
DROP ANY TRIGGER system privilege.

Syntax:
https://tutorialink.com/dbms/triggers.dbms 3/4
11/5/2018 Triggers | DBMS | Tutorialink.com

DROP TRIGGER trigger_name;

Example:
Run SQL Command Line

SQL>DROP TRIGGER emp;

Trigger dropped.

https://tutorialink.com/dbms/triggers.dbms 4/4

You might also like