What is trigger?
TRIGGER : --> SQL Trigger is an event-driven action or database callback function which is invoked automatically when an INSERT, UPDATE, and DELETE statement is performed on a specified table.
The main tasks of triggers are like enforcing business rules, validating input data, and keeping an audit trail.
Usage of Triggers:
1. Triggers are used for enforcing business rules.
2. Validating input data.
3.Generating a unique value for a newly-inserted row in a different file.
4.Write to other files for audit trail purposes.
5. Query from other files for cross-referencing purposes.
6. Used to access system functions.
7. Replicate data to different files to achieve data consistency.
Advantages of using triggers
Triggers make the application development faster. Because the database stores triggers, you do not have to code the trigger actions into each database application.Define a trigger once and you can reuse it for many applications that use the database.
Maintenance is easy, If the business policy changes, you have to change only the corresponding trigger program instead of each application program.
Types of triggers
there are two specification triggers1.) Application Triggers
2.) Database Triggers: there are three parts of database triggers
a.) DDL Triggers: Create,Drop,
b.) Instead of triggers
c.) DML Triggers: insert,delete,update
SYNTAX
: NEW:--> it represents the new value in to a column.
: OLD--> it represents the old value in to a column.
HOW TO CREATE A TRIGGER
create or replace trigger trigger_namebefore/after
insert/update/delete of column_name1,column_name2 on table_name
for each row :
declare
.
.
.
begin
.
.
.
.
.
.
.
end trigger_name;
/
Example: 1----->
create a trigger to insert or update vlaues in small letters but values show in caps.
create or replace trigger t1
before
insert or update of ename,job on emp
for each row
begin
:new.ename:=upper(:new.ename);
:new.job:=lower(:new.job);
end t1;
/
_________________________________________________________________________________
Example:2--->
Create a trigger which is showing 'record created' afetr insertion.
Create or replace trigger emp01
After insert on emp
begin
dbms_output.put_line("Record_inserted");
end
/
_________________________________________________________________________________
Example :3---->
Create a trigger to restriction for inserting the data according to time.
create or replace trigger t332
Before insert on emp
begin
if(to_char(Sysdate,'HH24-MI-SS') not between '09:00' and '18:00') then
Raise_application_error(-20056,'you can add employee only between 09:00 AM and 06:00 PM');
end if;
end;
/
_________________________________________________________________________________example Example :4---->
Create a trigger to update salary but the salary can't be greater then the current salary
create or replace trigger t332
Before update of sal on emp
for each row
begin
if :new.sal<:old.sal then
Raise_application_error(-20059,'updated salary can not be lesser then the current salary');
end if;
end;
/
_________________________________________________________________________________
Comments
Post a Comment