17.07.2015 Views

Defensive Database Programming - Red Gate Software

Defensive Database Programming - Red Gate Software

Defensive Database Programming - Red Gate Software

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6: Common Problems with Data IntegrityMultiple triggers for the same operation, on the same table, not only introduce thechances of conflicting changes, they also introduce the chance of redundant actions, orof unwanted and maybe unknown dependencies on undocumented execution orders.If we want to make sure that there is not more than one trigger for one operation onone table, we can wrap the query in Listing 6-57 in a unit test and have our test harnessverify that it never returns anything.Before moving on to other examples, let us get rid of the triggers we no longer need, asshown in Listing 6-58.DROP TRIGGER dbo.Items_EraseBarcodeChangeLog ;GODROP TRIGGER dbo.Items_LogBarcodeChange ;Listing 6-58: Dropping the triggers.Problems with triggers under snapshot isolation levelsIn Chapter 4, we discussed in detail how triggers can fail when running under snapshotisolation. As we demonstrated there, when our triggers are selecting from other rowsor other tables, in some cases we are implicitly assuming that they will not run undersnapshot isolation level. In such cases, it is necessary to use the READCOMMITTEDLOCKhint to eliminate this assumption.SummaryWe have discussed several cases where incorrect assumptions may lead to compromiseddata integrity, and we have investigated ways to improve the integrity of data by properuse of constraints and triggers.Data integrity logic in the application layer is too easily bypassed. The only completelyrobust way to ensure data integrity is to use a trusted constraint. Even then, we need totest our constraints, and make sure that they handle nullable columns properly.207

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!