Triggers: Very Good, or Very Bad?
secretGeek .:dot Nuts about dot Net:.
home .: about .: sign up .: sitemap .: secretGeek RSS

Triggers: Very Good, or Very Bad?

A colleague said this morning "I try to steer clear of triggers"

Part of me immediately thought: "That's stupid! Triggers are powerful and they should be used wherever they're the best tool."

But then a calm inner-voice rose up and reminded me of the dangers of triggers:

"With great power comes great responsibility."

(more on trigger-thinking...)

Testing and maintaining a trigger-happy database is comparable to waltzing through a jungle full of trip-wires, booby-traps and landmines.

Hence, as a general rule of thumb we should try to steer clear of triggers.

Before implementing a trigger, ask yourself this much, at least:

  1. "Is there an alternative technique?" (i.e. a technique that doesn't require a trigger)
  2. "Is the alternative technique no more than twice* as complex as the trigger technique?"

[* 'twice' may not be the figure you prefer -- determine a suitable factor for your own methods.]

If the answer is "no" to either of those questions then it's Trigger Time baby!

I do love a good trigger!

One little tip: you might want to have some nice strongly-typed methods in your DAL for turning the triggers on and off at will. [Note to self: this is particularly important when working on a HR system with a trigger that sends email notifications for application-level events (such as termination of employees) to board-level executives. Forgetting to disarm such a trigger during testing might cause undue stress to your boss, their boss, their boss's boss, and in turn, to yourself.]





'Jon Galloway' on Tue, 06 Jul 2004 05:46:10 GMT, sez:

I worked on an application which ran against a SQL Server 6.5 database which made heavy use of triggers. They'd actually run into the 8K limit on triggers on the Orders table, and had to change variable names to one or two characters to make necessary additions. Inserts often took 30 seconds, and no one could really explain what was going on for those 30 seconds.

So I guess I'm with your friend in trying to avoid triggers when I can. I think your rule sounds good, but the trigger difficulty should include the additional maintenance / debugging headaches you'll be passing on to those who inherit your code.



'Matthew Martin' on Tue, 06 Jul 2004 12:50:40 GMT, sez:

We use them to replace the buggy updates executed by our COTS software with better code.



'secretGeek' on Tue, 06 Jul 2004 20:43:28 GMT, sez:

I had to google for 'COTS', and now know: 'COTS' = 'Commercial Off the Shelf'

Thanks Matthew. I know the feeling.

Yep Jon, you've hit the nail on the head -- the factor used for evaluating trigger-suitability, should relate to how much maintenance is expected, what other sources will cause updates etc.




'Scott' on Wed, 07 Jul 2004 05:28:19 GMT, sez:

We now use SPs for all our CRUD actions; no new app is allowed to access tables directly. This allows some level of logic to be performed during all CRUD actions. In the insert SP you can implement any logic that you might otherwise have implemented as a trigger.
We do use triggers to populate/update the created and modified columns in tables where they exist. No logic checks, just simple, mundane, tasks.



'secretGeek' on Wed, 07 Jul 2004 21:15:56 GMT, sez:

hiya Scott, sounds good.

nice to hear from you - hope the UK is going swell!

And I agree, the less logic in your triggers/sprocs the better.

i particularly like using triggers for creating archived versions of records - because it's so much quicker to develop than any other archiving scheme, but it does make other maintenance rather hellish, and makes your db grow very quick.

some people love triggers for generating email via xp_sendmail -- i'm not a big fan of this and would rather the app controlled things like email generation -- but it's still a nice trick to have in your toolkit.

the main case for triggers is where you can't control the input mechanisms - eg COTS, or mutliple inputs, e.g. web service, web apps, legacy desktop apps and biztalk server all update the same database. triggers can provide a uniform response to the stimulus, regardless of its source. it's hard to use anything other than triggers in such a case.

we use a code generator to write all our CRUD sprocs, and it also generates a combined DAL/BLL which calls the sprocs and allows us to code against strongly typed objects. it allows us to basic maintenance apps together pretty fast -- and get on with worrying about the real issues outside of CRUD.

best of luck!
lb




name


website (optional)


enter the word:
 

comment (HTML not allowed)


All viewpoints welcome. But the right to delete any post for any reason is reserved. Don't make me do it. Aim for constructiveness. Comments may be republished, emailed to your loved ones or printed and used as toilet paper. Also, I get particularly nasty on comment spam. It's not worth even trying to post comment spam here -- your html is escaped, and your links are given a rel='nofollow'. By attempting to post a comment, you understand that if the comment is considered spam, at my absolute discretion, your IP address may be used as the target of a prolonged distributed denial of service attack. Your electricity might suddenly stop working. Your car tyres will go mysteriously flat. You will suffer permanent hairloss. Your dreams will be filled with terrifying monsters. And in any case I reserve the right to record and publish your IP address.

 

TimeSnapper is a life analysis system that stores and plays-back your computer use. It makes timesheet recording a breeze, helps you recover lost work and shows you how to sharpen your act.

 

NimbleText - FREE text manipulation and data extraction

NimbleText is a Powerful FREE Tool

Use it for:

  • extracting data from text
  • manipulating text
  • generating code

It makes you look awesome. Use it right now! Go on! Hurry! Don't walk, run!

 

Articles

Just Wally Just Wally
The Correct Order for a First Time Viewing of The Lord Of The Rings The Correct Order for a First Time Viewing of The Lord Of The Rings
A new era for Android. A new era for Android.
Mind-boggling Demo of New Gaming Genre, aka Folder-Based Hangman, aka Fun with Recursion Mind-boggling Demo of New Gaming Genre, aka Folder-Based Hangman, aka Fun with Recursion
Got CSV in your javascript? Use agnes. Got CSV in your javascript? Use agnes.

Archives Complete secretGeek Archives

TimeSnapper -- Automated Screenshot Journal TimeSnapper: automatic screenshot journal
NimbleText -- World's Simplest Code Generator NimbleText: Code Generator, Text Manipulator, Data Extractor

25 steps for building a Micro-ISV 25 steps for building a Micro-ISV
3 minute guides -- babysteps in new technologies: powershell, JSON, watir, F# 3 Minute Guide Series
Universal Troubleshooting checklist Universal Troubleshooting Checklist
Top 10 SecretGeek articles Top 10 SecretGeek articles
ShinyPower (help with Powershell) ShinyPower
Now at CodePlex

Realtime CSS Editor, in a browser RealTime Online CSS Editor
Gradient Maker -- a tool for making background images that blend from one colour to another. Forget photoshop, this is the bomb. Gradient Maker


[powered by Google] 


How to be depressed How to be depressed
You are not inadequate.



Recommended Reading


the little schemer


The Best Software Writing I
The Business Of Software (Eric Sink)

Recommended blogs

Jeff Atwood
Joseph Cooney
Phil Haack
Scott Hanselman
Julia Lerman
Rhys Parry
Joel Pobar
Thomas White
OJ Reeves
Eric Sink

Aggregated Links

proggit
dzone
hacker news
dot net kicks

Human Link Machines

interesting finds
a continuous learner's weblog
arjan's world
weekly link post

LinkedIn profile
LogEnvy - event logs made sexy
Computer, Unlocked. A rapid computer customization resource
PhysioTec, Brisbane Specialist Physiotherapy & Pilates
 
home .: about .: sign up .: sitemap .: secretGeek RSS .: © Leon Bambrick 2003 .: privacy

home .: about .: sign up .: sitemap .: RSS .: © Leon Bambrick 2003 .: privacy