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 ConstraintsAssignedToDeveloperID ,Description ,TicketStatus ,DeveloperStatusFROM dbo.Tickets ;GOROLLBACK ;Listing 7-18: No rows in the Tickets table are modified if Justin goes on vacation.If Justin is on vacation we cannot reopen a ticket assigned to him, as verified byListing 7-19.BEGIN TRANSACTION ;GOUPDATE dbo.DevelopersSET DeveloperStatus = 'Vacation'WHERE DeveloperID = 1 ;-- attempt one: just change the ticket's statusUPDATE dbo.TicketsSET TicketStatus = 'Active'WHERE TicketID = 1 ;Msg 547, Level 16, State 0, Line 6The UPDATE statement conflicted with the CHECK constraint"CHK_Tickets_ValidStatuses"-- attempt two: change both Status and DeveloperStatusUPDATE dbo.TicketsSET TicketStatus = 'Active' ,DeveloperStatus = 'Active'WHERE TicketID = 1 ;Msg 547, Level 16, State 0, Line 11The UPDATE statement conflicted with the FOREIGN KEYconstraint "FK_Tickets_Developers_WithStatus".226

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

Saved successfully!

Ooh no, something went wrong!