0% found this document useful (0 votes)
22 views5 pages

Unit - 5_ADBMS

A trigger in Oracle is a set of SQL and PL/SQL statements that execute automatically based on database events such as insert, delete, or update. It consists of three sections: the triggering event, trigger restriction, and trigger action, and can be categorized into types such as row triggers and statement triggers. Triggers are beneficial for preventing database misuse, implementing automatic backups, tracking operations, enforcing business rules, and enhancing security.

Uploaded by

vrajpatelmyindia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Unit - 5_ADBMS

A trigger in Oracle is a set of SQL and PL/SQL statements that execute automatically based on database events such as insert, delete, or update. It consists of three sections: the triggering event, trigger restriction, and trigger action, and can be categorized into types such as row triggers and statement triggers. Triggers are beneficial for preventing database misuse, implementing automatic backups, tracking operations, enforcing business rules, and enhancing security.

Uploaded by

vrajpatelmyindia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Unit - 5

Trigger

� Trigger is a group or set of SQL and PL/SQL statements that are executed
by oracle itself.
� Trigger is fired automatically based on database event.

� Triggers are like procedures and functions , although not exactly same.

Syntax:

Create or replace trigger trigger_name

Before / after insert, delete, update on table_name Old / New for each row

Declare

---------

Begin
------

Exception

------

End;

Structure of Trigger
� A trigger contains three basic sections:
1) Triggering event or statement
2) Trigger restriction
3) Trigger action

Triggering event or statement


� It is an SQL statement that causes a trigger to be fired.

� It can be insert, delete or update statement.

Trigger restriction

� It specifies the condition that must be true for a trigger to fire.

� It is specified using when clause.

Trigger action

� It is a PL/SQL block that gets executed when trigger is fired.

� It contains SQL and PL/SQL statements.

Types of Trigger
Following are the four types of trigger:
� Row trigger
- Fire each time a row is affected by the triggering statement.
- For example, if an update operation updates three rows in a table, trigger
will be executed three times.
- If no rows are affected by the triggering statements, the trigger will not
be executed.
For example:
create or replace trigger trig1
before update on account for each row
begin
dbms_output.put_line('Welcome for Update');

end;

� Statement trigger
- It fires only once.
- For example, if an update operation updates three rows in a table, trigger
will be executed only once.
- If no rows are affected by the triggering statements, the trigger will
execute only once.

For example:
create or replace trigger trig1
before insert on data
begin
dbms_output.put_line('Welcome');

end;

� Before trigger
- Trigger is executed before the triggering statements.
For example:
create or replace trigger trig1
before insert on data
begin
dbms_output.put_line('Welcome');

end;

� After trigger
- Trigger is executed after the triggering statements.
For example:
create or replace trigger trig1
after insert on data
begin

dbms_output.put_line('Welcome');

end;

Audit and Trail


� Auditing and trail provides the ability to trace the information flow inside a
database, including connection attempts, data updates, deletes, inserts and
selects, execute functionality.
� Auditing and trail is the monitoring and recording of selected user database
actions
� Audit and trail manages the history and keeps the back up of the table;

For example,

create or replace trigger tr1


before update or delete on data for each row
begin
insert into info values(:old.no,:old.name,:old.sal);

end;

� In this example, audit and trail will manage the history of data(origional
table) and will store in info(new table) as log file or backup.

Advantages of Trigger:

� To prevent misuse of database.

� To implement automatic backup of the database.

� To track the operations being performed on specific tables.

� To implement business rule constraints.

� To enforce complex security authorization.

Examples:

create or replace trigger trig1


before update on account for each row

begin

dbms_output.put_line('Welcome for Update');

end;

You might also like