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 ConstraintsFirstName VARCHAR(30) NOT NULL ,Lastname VARCHAR(30) NOT NULL ,DeveloperStatus VARCHAR(8) NOT NULL ,CONSTRAINT PK_Developers PRIMARY KEY (DeveloperID) ,CONSTRAINT CHK_Developers_StatusCHECK (DeveloperStatusIN ( 'Active', 'Vacation' ) )) ;CREATE TABLE dbo.Tickets(TicketID INT NOT NULL ,AssignedToDeveloperID INT NULL ,Description VARCHAR(50) NOT NULL ,TicketStatus VARCHAR(6) NOT NULL ,DeveloperStatus VARCHAR(8) NOT NULL ,CONSTRAINT PK_Tickets PRIMARY KEY ( TicketID ) ,CONSTRAINT FK_Tickets_DevelopersFOREIGN KEY ( AssignedToDeveloperID )REFERENCES dbo.Developers ( DeveloperID ) ,CONSTRAINT CHK_Tickets_StatusCHECK ( TicketStatus IN ( 'Active', 'Closed' ) )) ;Listing 7-1: Recreating the Developers and Tickets tables.Enforcing business rules using constraints onlyThe Developers table is identical to one we used in Chapter 4, but the Tickets tableis different; it has an extra column, DeveloperStatus, which we will use in a CHECKconstraint that attempts to enforce both our business rules, as shown in Listing 7-2.211

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

Saved successfully!

Ooh no, something went wrong!