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 7: Advanced Use of ConstraintsMsg 547, Level 16, State 0, Line 1The INSERT statement conflicted with the FOREIGN KEY SAMETABLE constraint "FK_InventoryLog_Self". The conflictoccurred in database "test2", table "dbo.InventoryLog".The statement has been terminated.Listing 7-29: A row with incorrect current quantity does not save.Also, we cannot withdraw more of an item than is currently in stock.INSERT INTO dbo.InventoryLog( ItemID ,ChangeDate ,ChangeQuantity ,CurrentQuantity ,PreviousChangeDate ,PreviousQuantity)VALUES ( 2 ,'20100102' ,-6 ,-1 , -- CurrentQuantity cannot be negative'20100101' ,5);Msg 547, Level 16, State 0, Line 1The INSERT statement conflicted with the CHECK constraint"CHK_InventoryLog_NonnegativeCurrentQuantity". The conflictoccurred in database "test2", table "dbo.InventoryLog",column 'CurrentQuantity'.The statement has been terminated.Listing 7-30: We cannot withdraw more than the available amount.However, we can take out a valid quantity of stock.240

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

Saved successfully!

Ooh no, something went wrong!