views:

177

answers:

4

Possible Duplicate:
Are database triggers evil?

There is lot of negative information on database triggers, just want to get the community's take on when is it good vs bad.

+1  A: 

Because they're "magic". They aren't very visible and they can do lots of work.

I've seen good developers take a lot of time trying to track down issues that are trigger related because they just didn't think to look for them. Also, they are very rarely needed.

Mike M.
I spent most of my day today looking for a mysterious update statement from a stored proc that doesn't issue an update. Long story short it the trigger on the table issuing the update.
CTKeane
That doesn't mean the trigger is a bad thing, it means the devs are not properly trained.
HLGEM
+3  A: 

I think they're OK when they are used to populate a separate, one-off set of tables for things like logging, aggregation etc. for security or creating metadata for example.

When you start altering your "live" data or "looping back" into your biz info tables, that's when they become evil and unwieldy. They are also utterly unnecessary for this. There is nothing that a trigger does that a stored proc cannot do.

I feel like they are SQL's evil equivalent to GOTOs in programming languages. Legal, but to be avoided unless absolutely necessary, and they are NEVER absolutely necessary.

Paul Sasik
Thank u Paul, But u said that they're absolutely NEVER necessary !!.Is there any better way to make Logging for updating, Deleting from table ??
Wahid Bitar
+3  A: 

Database triggers are bad when they are used when other features are more appropriate.

Features which should be considered before attempting to use triggers:

Check constraints

Foreign key constraints

Unique indexes/constraints

(Persisted) computed columns

(Indexed) Views (if a trigger is attempting something like updating an aggregate, say)

Stored procedures (if direct table access can be prohibited)

After that point, triggers can be appropriately used without being "bad". Triggers should always be designed to have a small footprint.

Cade Roux
+1  A: 

Here are a few articles that should help you figure out by yourself for your needs.

  1. Pros and Cons of Triggers vs. Stored Procedures for Denormalization (SO question) ;
  2. Choice Between Stored Procedures, Functions, Views, Triggers, Inline SQL.

In short, Triggers are useful for the processing of massive amount of data, when complex DML has to be conducted. To answer your question, if this is not the case, you got your answer as per when a trigger is bad.

Will Marcouiller