Database Trigger : SQL Server 2005 - Part 1

No.of Views2078
Bookmarked0 times
Downloads 
Votes0
By  Jana   On  15 Feb 2010 23:02:44
Tag : Sql Servers , How to
Database Trigger : SQL Server 2005 - Part 1
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

What is Trigger ?

A Trigger is a special type ofstored procedure which execute based on some event fired.  Triggers are attached with some Tables, Database or Server and registered with some events, when ever that event fire, trigger will execute automatically and do the predefine job. As I have already mention execution of trigger is based on some events, so we cannot run trigger manually.

Trigger can execute on data modification using Insert, Delete, Update  command or it can execute some data definition change like Table creation etc. We can also create trigger on server level which will execute based on some event perform on server. Some times trigger can cause execution of other trigger- know as "Nested Trigger"

General Syntax of Creating Trigger is

To summarize the whole things

  • Triggers are special type of Stored procedure which executed based on some events.
  • We cannot execute trigger manually.
  • We can have trigger on Table Level, Database Level and Server Level
  • We can not use parameter with trigger
  • we cannot use any return value from Trigger.

Now lets have a look on the advantages and disadvantages of Trigger.

Advantages of Trigger

Trigger has the following advantages

  • Trigger invoked based on some events.
  • Trigger can be fired on database and also server event
  • It is very helpful to check some complex condition where Constraints failed.
  • It is used for database table auditing
  • Triggers can be used to enforce constraints on Table.

Disadvantages of Trigger

  • Triggers that are created on table  are invisible, so its very difficult to maintain them.
  • Triggers run every time when the database fields are updated/Insert/delete and it is overhead on system.
  • we cannot use parameter with trigger
  • Trigger can not return any value

Different Types of Trigger

As I have already discuss, trigger can be applicable in Table, Database or in Database Server. Based on that, we can categorize trigger in following way.

Triggers type

SQL Sever command divided into two major category DML ( Data Manipulation Language ) and DDL (Data Definition Language) . DML statement are usually work with Database Table like insert update and Delete. DML trigger are those trigger which are execute when some DML statement fired on a table. DDL statements are used to build and modify the structure of your tables and other objects in the database. In SQL Server 2005 we have another Trigger called CLR Trigger. Now coming back to the details,

DML Trigger : This is based on Table. DML trigger are execute when any of the  following DML statement fired on that particular Table. Those are

  1. Insert

  2. Delete

  3. Update

we can attach a DML Trigger with any table against of those (Insert, delete, update ) SQL Command. we can have the DML Trigger for View also.  We can create multiple trigger on a single event . In that case we have to specify the sequence of execution. If have discussed it in later.

There are Two types of DML Trigger

  • AFTER Trigger

  • INSTEAD OF Trigger

Note : This is the end if Part 1, In the net article i will example all the DML and DDL trigger with example.

Hope this will give you a very good basic idea of Data Base Trigger.

Thank you

Jana

 
Sign Up to vote for this article
 
About Author
 
Jana
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-Not Provided
Joined date-19 Jun 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top