Hi,
Where do we need to use triggers,how do we decide on that in case in asp.net application. Please give examples in order to understand it better.
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.
Andrew FensterPosted Apr 11, 2011, 3:01 PM
A database trigger is sometimes handy. The exact method varies from database to database. The basic idea is that when a particular table gets updated, the trigger fires and executes a SQL call, stored procedure or function.
Triggers can be very useful, but there are two potential problems (at least):
(1) If you use a lot of them, your business logic will get very complicated and hard to debug.
(2) Triggers can be very inefficient. The trigger code runs EVERY time the database table gets updated. So suppose you added a trigger so that someone gets notified each time an order over $1,000,000 gets entered. Suppose you only have an order that large every several months. So you now have a trigger that fires each time an order gets entered. Each time the trigger will evaluate the order size and determine whether to send a notice. That's a lot of extra work for a situation that only arises every few months. So that would not be a good situation to use a trigger.
Andrew FensterPosted Apr 12, 2011, 9:10 AM
Personally, I don't like to use them for business logic. Then you have part of your business logic in the code and part in the database, and you can't easily debug. It's more error prone.
Of the four reasons Ankit listed, the first and the third seem like sensible places for triggers. Use them sparingly.
Shirsendu NandiPosted Apr 12, 2011, 8:59 AM
Ankit NandekarPosted Apr 12, 2011, 7:36 AM
Because a trigger resides in the database and anyone who has the required privilege can use it, a trigger lets you write a set of SQL statements that multiple applications can use. It lets you avoid redundant code when multiple programs need to perform the same database operation.
You can use triggers to perform the following actions, as well as others that are not found in this list:
- Create an audit trail of activity in the database. For example, you can track updates to the orders table by updating corroborating information to an audit table.
- Implement a business rule. For example, you can determine when an order exceeds a customer's credit limit and display a message to that effect.
- Derive additional data that is not available within a table or within the database. For example, when an update occurs to the quantity column of the items table, you can calculate the corresponding adjustment to the total_price column.
- Enforce referential integrity. When you delete a customer, for example, you can use a trigger to delete corresponding rows (that is, rows that have the same customer number) in the orders table.
For more information visit this below linkhttp://publib.boulder.ibm.com/infocenter/idshelp/v10/index.jsp?topic=/com.ibm.sqlt.doc/sqltmst316.htm>
Suthish NairPosted Apr 12, 2011, 7:27 AM
Savita JoshiPosted Apr 11, 2011, 9:56 PM
Please can you tell me where is it absolutely necessary to use triggers.. How do you decide whether it should be INSTEAD OF TRIGGER OR AFTER TRIGGER..