Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are database triggers bad? [duplicate]

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.

like image 299
Srikar Doddi Avatar asked Jun 15 '10 19:06

Srikar Doddi


2 Answers

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.

like image 176
Paul Sasik Avatar answered Oct 08 '22 01:10

Paul Sasik


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.

like image 37
Cade Roux Avatar answered Oct 07 '22 23:10

Cade Roux