|
|
The primary key contains a value that is unique across all the rows in that table. If you know the primary-key value, you can single out one row in a table, no matter how many rows the table has. A single column that contains an ID number is the best kind of primary key to retrieve data quickly.
Sometimes there is a second way to identify a unique row in a table called an alternate key. This is a column or set of columns, just like the primary key.
A foreign key resides in one table, but is the key to a different table. The primary key of the connected table is kept in the foreign-key column in the other table. Got it? No? Ask the pooch for the details.
|
|
You should name the foreign-key column and the corresponding primary-key columns the same names. This clearly defines the relationship between the two. There are many software tools that generate SQL match tables reports by looking for identical column names.
|
|
so there!
|
|
Primary keys are carried in multiple tables -- people copy primary keys into other tables as alternate keys. Obviously, if the primary key column holds a four-digit number, that will take up a lot less space than a column that holds thirty-six digits.
Now, what do I mean about keeping your primary keys stupid? I mean this: intelligent keys store meaningful data in addition to being the primary key. A nonintelligent (or stupid) key has only one purpose: being the primary key for the table. Why is it smart to use a stupid key? Because if you have meaningful data in the primary key column, when that data changes, you have to change all the foreign keys that uses that primary key as a match. |
|
Or maybe the key block?
|