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.

that my users can’t sell a product at a loss, or perhaps something as simple as CKCustomerPhoneNo on<br />

a column that ensures that phone numbers are formatted properly.<br />

As with the naming of anything that we’ll use in this book, how exactly you name things is really not all<br />

that important. What is important is that you:<br />

❑ Be consistent.<br />

❑ Make it something that everyone can understand.<br />

❑ Keep it as short as you can while still meeting the preceding rules.<br />

❑ Did I mention to be consistent?<br />

Key Constraints<br />

There are four different types of common keys that you may hear about in your database endeavors.<br />

These are primary keys, foreign keys, alternate keys, and inversion keys. For this chapter, we’ll only take<br />

a look at the first three of these, as they provide constraints on a database.<br />

An inversion key is basically just any index (we cover indexes in Chapter 9) that does not apply some<br />

form of constraint to the table (primary key, foreign key, unique). Inversion keys, rather than enforcing<br />

data integrity, are merely an alternative way of sorting the data.<br />

Keys are one of the cornerstone concepts of database design and management, so fasten your seatbelt<br />

and hold on tight. This will be one of the most important concepts you’ll read about in this book, and<br />

will become absolutely critical as we move on to normalization in Chapter 8.<br />

PRIMARY KEY Constraints<br />

Chapter 6: Constraints<br />

Before we define what a primary key actually is, let’s digress slightly into a brief discussion of relational<br />

databases. Relational databases are constructed on the idea of being able to “relate” data. Therefore, it<br />

becomes critical in relational databases for most tables (there are exceptions, but they are very rare) to<br />

have a unique identifier for each row. A unique identifier allows you to accurately reference a record<br />

from another table in the database, thereby forming a relation between those two tables.<br />

This is a wildly different concept from what we had with our old mainframe environment or the ISAM<br />

databases (dBase, FoxPro, and so on) of the ’80s and early ’90s. In those environments, we dealt with<br />

one record at a time. We would generally open the entire table, and go one record at a time until we<br />

found what we were looking for. If we needed data from a second table we would then open that table<br />

separately and fetch that table’s data, then mix the data programmatically ourselves.<br />

Primary keys are the unique identifiers for each row. They must contain unique values (and hence cannot<br />

be NULL). Because of their importance in relational databases, primary keys are the most fundamental of<br />

all keys and constraints.<br />

157

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

Saved successfully!

Ooh no, something went wrong!