1 / 34

Triggers: Born Evil or Misunderstood?

Triggers: Born Evil or Misunderstood?. Louis Davidson. Who am I?. Been in IT for over 18 years Microsoft MVP For 9 Years Corporate Data Architect Written five books on database design Ok, so they were all versions of the same book. They at least had slightly different titles each time.

reese
Download Presentation

Triggers: Born Evil or Misunderstood?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Triggers: Born Evil or Misunderstood? Louis Davidson

  2. Who am I? • Been in IT for over 18 years • Microsoft MVP For 9 Years • Corporate Data Architect • Written five books on database design • Ok, so they were all versions of the same book. They at least had slightly different titles each time

  3. Brief Introduction • Triggers are coded modules that are very similar to stored procedures • Not called directly, but are “triggered” by certain events • With “special” tools to access event data • Triggers existed in Microsoft SQL Server 1.0 (far before check constraints!) • Types: • DML –Table/View level, fire on INSERT, UPDATE and/or DELETE to a single object • Once per statement, regardless of number of rows • DDL – Server or Database level, fire whenever a DDL statement is executed • Login – Fire whenever a user logs into the server AD-100 - Triggers, Born Evil or Misunderstood?

  4. Introduction - Continued • Triggers execute as part of the operation statement • ROLLBACK in the trigger will stop the operation (and anything else that is part of the current transaction) • Can use EXECUTE AS to elevate the permissions of the trigger code (in extreme circumstances!) • Never return any results from triggers • Ability to do so will be removed in an upcoming SQL Server version • Currently controlled with “disallow results from triggers” server setting • Should operate silently AD-100 - Triggers, Born Evil or Misunderstood?

  5. The Setup… In the following session we will put triggers on trial I will play the part of Judge, Prosecutor, and Defense Attorney I will provide many exhibits (aka Code Demos) to demonstrate my points You will play the part of Jury Member and possibly Witness AD-100 - Triggers, Born Evil or Misunderstood?

  6. Your Part of the Process - Jury • Judge triggers… You will be given the following choices, majority rules • You can find them • 5 - Evil • 4 • 3 • 2 • 1 – Misunderstood (aka Awesome!) • Then we discuss sentencing… AD-100 - Triggers, Born Evil or Misunderstood?

  7. Come to order AD-100 - Triggers, Born Evil or Misunderstood?

  8. Opening Statements - Prosecution AD-100 - Triggers, Born Evil or Misunderstood?

  9. Triggers are dangerous • Sneaky…can do weird stuff to data that isn’t obvious • Performance could be affected • Difficult to get right unless you are really careful • Multi-row operations for DML triggers are commonly screwed up • Very often the source of problems that aren’t diagnosed because they execute silently • Error handling can be messy AD-100 - Triggers, Born Evil or Misunderstood?

  10. Opening Statement - Defense AD-100 - Triggers, Born Evil or Misunderstood?

  11. What is the overall data problem? • Top Issue with Database Implementations • #1 Data Quality • #2 Does any other issue matter if the data quality is unsatisfactory? • Obviously performance and usability is important, but still quality is the most important thing • Anything we can do to manage our servers and keep the data clean the better • Triggers are a VERY small part of the picture! • But still a part of the picture… AD-100 - Triggers, Born Evil or Misunderstood?

  12. Data Protection.1/3 – Start right! • Start by getting the structure correct • Normalization -Normalized structures are far less susceptible to data integrity issues • Datatypes • Match datatypes to the needs of the user • Data stored in the right datatype works better for the Query Processor • Make sure only the right people are modifying structures AD-100 - Triggers, Born Evil or Misunderstood?

  13. Data Protection.2/3 - Constraints NULL: Determines if a column will accept NULL for its value. NULL constraints aren’t technically constraint objects, they behave like them. PRIMARY KEY and UNIQUE: Used to make sure your rows contain only unique combinations of values over a given set of key columns. FOREIGN KEY: Used to make sure that any migrated keys have only valid values that match the key columns they reference. DEFAULT: Used to set an acceptable default value for a column when the user doesn’t provide one. (Some people don’t count defaults as constraints, because they don’t constrain updates.) CHECK: Used to limit the values that can be entered into a single column or an entire row. AD-100 - Triggers, Born Evil or Misunderstood?

  14. Data Protection.3/3 • Determine what can be reliably done using client code • Stored procedures • Compiled, procedural code • The key word in previous statement: “reliably” • Client code is notoriously untrustworthy • It gets worse when it a rule has to be enforced in multiple places • Often multiple layers implementing the same rules can be useful • Then come triggers…filling in gaps that can not be handled reliably in any other manner AD-100 - Triggers, Born Evil or Misunderstood?

  15. Prosecution – Evidence AD-100 - Triggers, Born Evil or Misunderstood?

  16. Exhibit A – Multi-row Operations • DML triggers must be coded using Multi-Row operations • Not getting this right can cause data to be allowed in that is actually incorrect AD-100 - Triggers, Born Evil or Misunderstood?

  17. Exhibit B –Settings • Triggers are subject to settings at the server and the database level that can change how the code works AT RUNTIME! • Including: • sp_serveroption— nested triggers (default ON)– Determines if a DML statement from one trigger causes other DML triggers to be executed • Database option—RECURSIVE_TRIGGERS (default OFF)– Determines if an update on the table where the trigger fired causes the same triggers to fire again • sp_serveroption–disallow results from triggers (default OFF): Turn this setting on will ensure that any trigger that tries to return data to the client will get an error • sp_serveroption-server trigger recursion (default ON) – Determines if DDL in a server DDL trigger causes it to fire again AD-100 - Triggers, Born Evil or Misunderstood?

  18. Exhibit C –Messy Error Handling • Errors that occur in triggers can result in multiple different outcomes, depending on how they are coded AD-100 - Triggers, Born Evil or Misunderstood?

  19. Exhibit D –Weird Things Happen • DML Triggers can make the action you want to take not actually occur • This can go unnoticed for long periods of time, causing data loss • Even worse, sometimes triggers return results…by accident… AD-100 - Triggers, Born Evil or Misunderstood?

  20. Prosecution – Last Minute Witnesses? AD-100 - Triggers, Born Evil or Misunderstood?

  21. Defense – Evidence AD-100 - Triggers, Born Evil or Misunderstood?

  22. Exhibit A –Skill and Templates… • It is true, triggers are not “simple” • However, the prosecution would have you believe they are impossible • With caution and process, they are very possible • Start from a template that sets you up for success AD-100 - Triggers, Born Evil or Misunderstood?

  23. Exhibit B –Automatic Protection… • When coded properly, prevents data that does not meet minimum standards to be stored, even if the rules are complex.. AD-100 - Triggers, Born Evil or Misunderstood?

  24. Exhibit C – Makes Magic Happen • Triggers can, when needed do validations that other types of automatic code cannot: • Access data in multiple rows • Access data in a different table • Introduce side effects • Stop DML operations • Work on DDL operations • Work on Login operations AD-100 - Triggers, Born Evil or Misunderstood?

  25. Exhibit C – Example Magic • Magical operations can occur with minimal coding where necessary and zero visible system impact • Examples • Clandestinely adding to third party systems • Row metadata (last modified time) • Logging changes to data AD-100 - Triggers, Born Evil or Misunderstood?

  26. Exhibit D – Metadata Makes Life Easier • There are many bits of metadata in the catalog objects that make it easier to support triggers • sys.configurations: check the settings for nested triggers and results allowed • sys.databases: see if recursive triggers set to on • sys.triggers: list triggers and properties • sys.trigger_events: list events that cause triggers to fire • sys.dm_sql_referenced_entities: see objects and columns referenced in triggers AD-100 - Triggers, Born Evil or Misunderstood?

  27. Exhibit E – Works on Any Edition AD-100 - Triggers, Born Evil or Misunderstood?

  28. Defense – Last Minute Witness? AD-100 - Triggers, Born Evil or Misunderstood?

  29. Summation - Prosecution AD-100 - Triggers, Born Evil or Misunderstood?

  30. Summation – Prosecution Points • Triggers are sneaky, devious objects • Triggers are the most complex code that has to be maintained • DBAs need to be cognizant of their existence of they will drive you NUTS • Any positives are always prefixed with: As long as you know what you are doing AD-100 - Triggers, Born Evil or Misunderstood?

  31. Summation - Defense AD-100 - Triggers, Born Evil or Misunderstood?

  32. Summation – Defense Points • Triggers fill a gap of data validation that cannot be done easily otherwise • Multi-row validations • Multi-table validations • Auditing (certainly where Version < Enterprise) • Complex Cascading operations • Triggers allow you to make silent changes to system (the prosecution called this sneaky, I call it useful) • As long as programmers/users are aware of their existence and purpose, they are helpful and useful tools AD-100 - Triggers, Born Evil or Misunderstood?

  33. Ladies and Gentlemen of the Jury • It is time, what says you? • You can find them • 5 - Evil • 4 • 3 • 2 • 1 – Misunderstood (aka Awesome!) AD-100 - Triggers, Born Evil or Misunderstood?

  34. Sentencing Quit your job before being forced to use a trigger Never use them in any case, unless your boss forces you to Only use them when absolutely necessary and you can’t come up with any other method that would work Use them in any case where they seem needed and you can’t think of any other solution Use them for everything AD-100 - Triggers, Born Evil or Misunderstood?

More Related