Datasheet

7
Chapter 1: Introducing T-SQL and Data Management Systems
With the EmployeeKey column, you have an efficient, easy - to - manage primary key.
Each table can have only one primary key, which means that this key column is the primary method for
uniquely identifying individual rows. It doesn t have to be the only mechanism for uniquely identifying
individual rows; it is just the primary mechanism for doing so. Primary keys can never be null, and
they must be unique. Primary keys can also be combinations of columns (though I ll explain later why
I am a firm believer that primary keys should typically be single - column keys). If you have a table where
two columns in combination are unique, while either single column is not, you can combine the two
columns as a single primary key, as illustrated in Figure 1 - 4 .
Figure 1-3
Figure 1-4
In this example, the LibraryBook table is used to maintain a record of every book in the library. Because
multiple copies of each book can exist, the ISBN column is not useful for uniquely identifying each book.
To enable the identification of each individual book, the table designer decided to combine the ISBN
column with the copy number of each book. Personally, I avoid the practice of using multiple column
keys. I prefer to create a separate column that can uniquely identify the row. This makes it much easier to
write join queries (covered in detail in Chapter 8 ). The resulting code is cleaner and the queries are
generally more efficient. For the library book example, a more efficient mechanism might be to assign
each book its own number. The resulting table would look like that shown in Figure 1 - 5 .
Figure 1-5
CH001.indd 7CH001.indd 7 3/26/10 11:35:35 AM3/26/10 11:35:35 AM