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 IntegrityWidthInInches DECIMAL(4, 2) NULL ,HeightInInches DECIMAL(4, 2) NULL) ;Listing 6-1: Creating the Boxes table, which is populated by our application.Our application has already loaded some data into our table, as represented by the scriptshown in Listing 6-2.INSERT INTO dbo.Boxes(Label,LengthInInches,WidthInInches,HeightInInches)VALUES ('School memorabilia',3,4,5) ;Listing 6-2: Loading some existing data into the Boxes table.However, suppose that we then develop a new version of our application, in which wehave started to enforce the following rule when inserting rows into the Boxes table:The height of a box must be less than, or equal to, the width; and the width must be lessthan, or equal to, the length.At some later point, we are asked to develop a query that returns all the boxes with atleast one dimension that is greater than 4 inches. With our new business rule in place weknow (or at least we think we know) that the longest dimension of any box is the length,so all we have to do in our query is check for boxes with a length of more than 4 inches.Listing 6-3 meets these requirements.164

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

Saved successfully!

Ooh no, something went wrong!