17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

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 6: Constraints<br />

We can then build one or more tables that reference the CreditCardID column of our domain table. With<br />

referential integrity, any table (such as our Orders table) that is defined as referencing our CreditCard<br />

table will have to have a column that matches up to the CreditCardID column. For each row that we<br />

insert into the referencing table, it will have to have a value that is in our domain list (it will have to have<br />

a corresponding row in the CreditCard table).<br />

We’ll see more of this as we learn about FOREIGN KEY constraints later in this chapter.<br />

Constraint Naming<br />

156<br />

Before we get down to the nitty-gritty of constraints, we’ll digress for a moment and address the issue of<br />

naming constraints.<br />

For each of the different types of constraints that we will be dealing with in this<br />

chapter, you can elect not to supply a name — that is, you can have <strong>SQL</strong> <strong>Server</strong> provide<br />

a name for you. Resist the temptation to do this. You’ll quickly find that when<br />

<strong>SQL</strong> <strong>Server</strong> creates its own name, it isn’t particularly useful.<br />

An example of a system-generated name might be something like PK__Employees__145C0A3F. This is<br />

an <strong>SQL</strong> <strong>Server</strong>–generated name for a primary key on the Employees table of the Accounting database,<br />

which we will create later in the chapter. The “PK” is for primary key (which is the most useful naming<br />

element), the Employees is for the Employees table that it is on, and the rest is a randomly generated<br />

value to ensure uniqueness. You only get this type of naming if you create the primary key through<br />

script. If you created this table through Management Studio, it would have a name of PK_Employees.<br />

That one isn’t too bad, but you get less help on other constraints; for example, a CHECK constraint used<br />

later in the chapter might generate something like CK__Customers__22AA2996. From this, we know<br />

that it’s a CHECK constraint, but we know nothing of what the nature of the CHECK is.<br />

Since we can have multiple CHECK constraints on a table, you could wind up with all of these as names<br />

of constraints on the same table:<br />

❑ CK__Customers__22AA2996<br />

❑ CK__Customers__25869641<br />

❑ CK__Customers__267ABA7A<br />

Needless to say, if you needed to edit one of these constraints, it would be a pain to figure out which was<br />

which.<br />

Personally, I either use a combination of constraint types with a phrase to indicate what it does or the name(s)<br />

of the column(s) it affects. For example, I might use CKPriceExceedsCost if I have a constraint to ensure

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

Saved successfully!

Ooh no, something went wrong!