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 ConstraintsThe Ticket-Tracking SystemIn Chapter 4, we implemented a trigger-based solution to the problem of assigningtickets in a ticket-tracking system. Our AFTER UPDATE trigger, dbo.Developers_Upd,enforced the following two business rules:• developers cannot go on vacation if they have active tickets assigned to them• inactive tickets cannot be changed to active status if they are assigned to developersthat are on vacation.Here, we'll re-implement these business rules using only constraints and, in addition,we'll be able to enforce the rule that "newly added active tickets cannot be assigned toa developer on vacation," which would have required an additional AFTER INSERTtrigger. To ensure that we are all on the same page, we'll drop the original tables fromChapter 4 and start again from scratch.IF EXISTS ( SELECT *FROM sys.objectsWHERE OBJECT_ID= OBJECT_ID(N'dbo.Tickets') )BEGIN ;DROP TABLE dbo.Tickets ;END ;GOIF EXISTS ( SELECT *FROM sys.objectsWHERE OBJECT_ID= OBJECT_ID(N'dbo.Developers') )BEGIN ;DROP TABLE dbo.Developers ;END ;GOCREATE TABLE dbo.Developers(DeveloperID INT NOT NULL ,210

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

Saved successfully!

Ooh no, something went wrong!