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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7: Advanced Use of ConstraintsSQL Server UNIQUE constraints and the ANSI StandardIn SQL Server, a UNIQUE constraint disallows duplicate NULLs. However, accordingto the ANSI standard it should allow them. Therefore, the UNIQUE constraint inListing 7-35 will work on SQL Server but will not port to a RDBMS that implementsANSI-compliant UNIQUE constraints.Updating existing rowsIn all previous examples, we have used INSERT commands to add new inventoryrows. Now we need to consider how our system will behave if we allow UPDATEs ofexisting rows in the inventory trail for a given item. First of all, let's add another historyof changes for item #2.INSERT INTO dbo.InventoryLog( ItemID ,ChangeDate ,ChangeQuantity ,CurrentQuantity ,PreviousChangeDate ,PreviousQuantity)VALUES ( 2 ,'20100105' ,-3 ,1 ,'20100102' ,4);Listing 7-36: More history for item #2.Suppose it is discovered that the initial inventory for item #2, entered on January 1st,2010, should have reflected a quantity of 3, rather than 5, so we need to decrease theamount added for that item.245

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

Saved successfully!

Ooh no, something went wrong!