Hi friends,
I want to know What is Trigger ? and types of trigger with example insert, update, delete.
thank you.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
MuthuMari MPosted May 10, 2012, 1:40 AM
A trigger is a special kind of a store procedure that executes in response to certain action on the table like insertion, deletion or updation of data. It is a database object which is bound to a table and is executed automatically. You can't explicitly invoke triggers. The only way to do this is by performing the required action no the table that they are assigned to.
Types Of Triggers
There are three action query types that you use in SQL which are INSERT, UPDATE and DELETE. So, there are three types of triggers and hybrids that come from mixing and matching the events and timings that fire them.Basically, triggers are classified into two main types:-
(i)After Triggers (For Triggers)
(ii)Instead Of Triggers
pls refer this url
http://www.codeproject.com/Articles/25600/Triggers-Sql-Server
http://docs.oracle.com/cd/B19306_01/server.102/b14220/triggers.htm#i2013
thanks.
If this post is useful then mark it as "Accepted Answer"
Jignesh TrivediPosted May 9, 2012, 11:53 PM
A trigger is a database object that is attached to a table. In many aspects it is similar to a stored procedure.
In other word special kind of stored procedure that run automatically on table when DML is fire.
Trigger is Data base object so it limited by number of objects in a database.
please refer
http://www.sqlteam.com/article/an-introduction-to-triggers-part-i
hope this help.
Satyapriya NayakPosted May 9, 2012, 10:39 PM
Triggers are special kind of stored procedures that get executed automatically when an INSERT, UPDATE or DELETE operation takes place on a table.
What is a Trigger
A trigger is a special kind of a store procedure that executes in response to certain action on the table like insertion, deletion or updation of data. It is a database object which is bound to a table and is executed automatically. You can't explicitly invoke triggers. The only way to do this is by performing the required action no the table that they are assigned to.Types Of Triggers
There are three action query types that you use in SQL which are INSERT, UPDATE and DELETE. So, there are three types of triggers and hybrids that come from mixing and matching the events and timings that fire them.Basically, triggers are classified into two main types:-
(i)After Triggers (For Triggers)
(ii)Instead Of Triggers
(i) After Triggers
These triggers run after an insert, update or delete on a table. They are not supported for views.AFTER TRIGGERS can be classified further into three types as:
(a)AFTER INSERT Trigger.
(b)AFTER UPDATE Trigger.
(c)AFTER DELETE Trigger.
Let's create After triggers. First of all, let's create a table and insert some sample data. Then, on this table, I will be attaching several triggers.
Now, create the audit table as:-
(a) AFTRE INSERT Trigger
This trigger is fired after an INSERT on the table. Let's create the trigger as:-In the trigger body, table named inserted has been used. This table is a logical table and contains the row that has been inserted. I have selected the fields from the logical inserted table from the row that has been inserted into different variables, and finally inserted those values into the Audit table.
To see the newly created trigger in action, lets insert a row into the main table as :
Now, a record has been inserted into the Employee_Test table. The AFTER INSERT trigger attached to this table has inserted the record into the Employee_Test_Audit as:-
(b) AFTER UPDATE Trigger
This trigger is fired after an update on the table. Let's create the trigger as:-Let's update a record column and see what happens.
(c) AFTER DELETE Trigger
This trigger is fired after a delete on the table. Let's create the trigger as:-Let's fire a delete on the main table.
A record has been inserted into the audit table as:-
ALTER TABLE Employee_Test {ENABLE|DISBALE} TRIGGER ALLSpecific Triggers can be enabled or disabled as :-ALTER TABLE Employee_Test DISABLE TRIGGER trgAfterDeleteThis disables the After Delete Trigger named trgAfterDelete on the specified table.
(ii) Instead Of Triggers
These can be used as an interceptor for anything that anyonr tried to do on our table or view. If you define an Instead Of trigger on a table for the Delete operation, they try to delete rows, and they will not actually get deleted (unless you issue another delete instruction from within the trigger)INSTEAD OF TRIGGERS can be classified further into three types as:-
(a)INSTEAD OF INSERT Trigger.
(b)INSTEAD OF UPDATE Trigger.
(c)INSTEAD OF DELETE Trigger.
(a) Let's create an Instead Of Delete Trigger as:-
Now, let's try to delete a record with the Emp_Sal >1200 as:-
delete from Employee_Test where Emp_ID=4This will print an error message as defined in the RAISE ERROR statement as:-And this record will not be deleted.
In a similar way, you can code Instead of Insert and Instead Of Update triggers on your tables.
Please refer the below links
http://www.c-sharpcorner.com/uploadfile/shashikantray/create-delete-and-update-triggers-in-a-database/default.aspx
http://www.codeproject.com/Articles/25600/Triggers-Sql-Server
Thanks