11

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.

Community
  • 1
  • 1
Srikar Doddi
  • 15,499
  • 15
  • 65
  • 106
  • 1
    It's because neophytes use them without proper knowledge to know that alternatives exist that are better suited. IE: "Hey I want to do X. I know, I'll use a trigger..." when a check or default constraint is better, or worse - a foreign key constraint. – OMG Ponies Jun 15 '10 at 19:28
  • (Concerning my close vote for this question: I know you've formulated your question a little differently than the one I suggested, but I feel that the answers to the other question already discuss both positive and negative things about DB triggers... won't that answer your question also?) – stakx - no longer contributing Jun 15 '10 at 19:30
  • 3
    @stakx agreed. I did not search for the word evil. – Srikar Doddi Jun 15 '10 at 19:35
  • 2
    +1 for reopen: The title of the question needs to be redone because it does sound duplicated but in contrast to the dublicate "Evil" triggers question this one does ask (albeit only in the question body) about when triggers are good. – Paul Sasik Jun 15 '10 at 21:03

4 Answers4

11

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
  • 79,492
  • 20
  • 149
  • 189
  • 2
    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 Jun 30 '10 at 14:01
7

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
  • 88,164
  • 40
  • 182
  • 265
5

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.
  • 12,343
  • 1
  • 24
  • 28
  • 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 Jun 15 '10 at 20:18
  • 3
    That doesn't mean the trigger is a bad thing, it means the devs are not properly trained. – HLGEM Jun 15 '10 at 21:33
  • 3
    @HLGEM, devs don't (and can't) know everything! The point is putting code in a trigger can hide it away unnecessarily. – CrazyTim Aug 10 '12 at 05:26
  • 1
    No the point in putting it in a trigger is to safeguard the data which is FAR FAR nmore important than protecting the egos of incomptent devs. – HLGEM Aug 10 '12 at 13:50
  • 1
    @HLGEM I disagree. Put constraints on the table for what you can, and if there are business rules that need to be hit to ensure that your data retains its integrity those should be done either in SPs, or a business logic layer. It's really a matter of "best tool for the job", I'd argue that enforcing business rules with triggers is *usually* not the best way. There are always exceptions! – Mike M. Aug 10 '12 at 19:41
2

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.

Community
  • 1
  • 1
Will Marcouiller
  • 23,773
  • 22
  • 96
  • 162