Home > Ask the Oracle Experts > SQL Questions & Answers > Questions about primary keys
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Questions about primary keys

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 05 April 2002

I hope this isn't too basic of a question, but I am confused about Primary Keys. In a tutorial example I just read, they provided a list of fields in a BookProducts table: InventoryDate; InventoryQuantity; Title; LastName; FirstName; YearPublished; Publisher; UPC; ISBN; and RetailPrice. I selected the ISBN (International Standard Book Number) as the logical primary key, because it is unique to a book. The tutorial presented a Composite primary key that didn't include the ISBN (see below). Their explanation: Since each title and UPC can have more than one author and each author can have more than one title, you need a combination of LastName, FirstName, and UPC fields to create the unique primary key for the BookProducts Table. This totally confused me. Can you help? Why isn't the ISBN a good primary key by itself?

My second quick question is this: Are there any tables for which you don't need a primary key? I read that you need a primary key for each base table in a database. What other kinds of tables are there besides base tables, and do they need primary keys? Also, is there a good reference for understanding primary keys? My Beginning SQL Programming book is no help. Thanks for your time.


>
EXPERT RESPONSE

You are right, ISBN is unique for each book, but that would make it a good primary key only for the Book table, where presumably there is only one row per book. In the BookProducts table, there's obviously more than one row per book. Even LastName, FirstName, and UPC are not sufficiently unique to be the primary key, if I understand the design of the BookProducts table correctly. It seems to be possible to take inventory, i.e. count the number of copies of a book, on multiple dates. Therefore the same book will show up in the table on each inventory date that there was at least one copy in stock. So the composite primary key would have to contain the InventoryDate column as well.

The classic example of a missing primary key is the hypothetical table that holds the items on a grocery store cash register receipt. I learned of this from Joe Celko, who called it the "three cans of cat food" problem. Disregard for a moment that the three cans might have been rung up as a group (3 cans @ .98 = $2.97) and assume they are in the table each as a separate row. These three rows will have identical values in all columns that matter, unless you add something like a timestamp with precision down to a fraction of a second -- and a timestamp is an unnecessarily cumbersome field to use just so that you can declare a primary key.

I'm not sure how your "base tables" are defined, but in my mind there is no reason to declare a primary key unless you need another table to reference it via a foreign key. However, since most tables are related to other tables, primary keys become commonplace and thus perhaps they are assumed necessary. A table does not have to have a primary key. Uniqueness can be guaranteed by a constraint (often implemented by the database as a unique index).

A really great resource for relational database design is the Data Modeling documentation on the Web site of the University of Texas at Austin. Have a look at the section on Primary and Foreign Keys.

See also my previous answer What is a primary key? Composite PK? Foreign key? Tuple? 27 February 2002.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
SQL
Finding a column value inside a user-supplied string
Update a specific column in a field or row?
Using BETWEEN with DATETIMEs in SQL
Which normal form is used most?
IN list or series of OR conditions?
Connecting tables in a database
SQL query for co-authored books
Querying complex derived tables
SQL string functions
Changing a NULL column to NOT NULL

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts