views:

123

answers:

8

I have a table which needs 2 fields. One will be a foreign key, the other is not necessarily unique. There really isn't a reason that I can find to have a primary key other than having read that "every single tabel ever needs needs needs a primary key".

Edit:

Some good thoughts in here.

For clarity's sake, I will give you an example that is similar to my database needs.

Let's say have a table with product type, quantity, cost, and manufacturer.

Product type will not always be unique (say, MP3 Player), but manufacturer/product type will be unique (say, Apple MP3 Player). Forget about the various models the manufacturers make for this example. For ease, this table has a autoincrementing primary key.

I am giving a point value and logging how often these products are searched for, added to a cart, and bought for display on a list of hot items.

The way I have it layed out currently is in a second table with a FK pointing to the main table, and a second column for the total number of "popularity points" this item has gained.

The answers have seen here have made me think that perhaps I should just add a "points" column to my primary products table so that I could just track there... but that seems like I'm not normalizing my database enough.

My problem is I'm currently mostly just a hobbyist doing this for learning, and don't have the luxury of a DBA to tell me how to set up my tables, so I have to learn both the coding side and the database side.

+1  A: 

i am not sure how the other field looks like .. but i am guessing that it would be to ok to have a composite primary key , which is based on the FK and the other field .. but then again i dont know your exact scenario.

Sabeen Malik
+2  A: 

For the sake of having something unique and as identifier, please please please please have a primary key in every table :)

It also helps forward compaitability in case there are future schema changes and 2 values are no long unique. Plus, memory are much cheaper now, feel free to use them as investments. ;)

rockacola
A: 

This is a technique related to normalization and a pretty good practice. A key made up of an auto incrementing number has many benefits:

  • You have a PK that does not pertain to the data.
  • You never have to change the PK value
  • Every row will automatically have a unique identifier
Raj More
All of the above is true but doesn't answer OP's question.
ChssPly76
You must be a manager...
PostMan
Having an auto incrementing number does not pertain to nomalization. http://en.wikipedia.org/wiki/Database_normalization
JeremyDWill
A: 

I would say that it's absolutely necessary to have some sort of primary key in every table.

Interestingly enough, one of the DBA's for a Viacom property once told me that there was really no discernible difference in using an INT UNSIGNED or a VARCHAR(n) as a primary key in MySQL. This was in reference to a user table with more than 64 million rows. I believe n can be decently large (<=100), but I forget the what they limited to. Unfortunately, I don't have any empirical data to back that up.

Justin Johnson
A: 

You don't HAVE to have a primary key on every table, but it is considered best practice to have them as they are almost always necessary on a normalized relational database design. If you're finding a bunch of tables you don't think need PKs, then you should revisit the design/layout of your tables. To read more on normalization see here.

A couple scenarios that I can think of where you may not need or want a PK on a table would be a table strictly for logging. (to limit performance degradation of writing the log and maintaining a unique index) and in the scenario where your just storing data used to pump through an application for test purposes.

RC
A: 

I'll be contrary and say you shouldn't add the key if you don't have a reason for it. It is very easy to add this column later if needed.

Keith Randall
+3  A: 

You have to distinguish between primary key and surrogate key. Auto-incremented column would be a particular case of the latter. Your question, therefore, is twofold:

  1. Does every table need to have a primary key?
  2. Does every table need to have a surrogate primary key?

The answer to first question is YES except in some special cases (association table for many-to-many relationship arguably being an example of such a special case). The reason for this is that you usually need to be able (if not right now then in the future) to consistently address individual rows of that table - for updates / deletion, for example.

The answer to the second question is NO. If your table represents a core business entity then OR it can be referenced from many-to-one association, having a surrogate key is probably a good idea; but it's not absolutely necessary.

It's somewhat unclear what your table's function is; from your description it sounds like it has "collection of values" semantics (FK to "main" table + value). Certain ORMs don't support surrogate keys in such circumstances; if that's what has prompted your question it's OK to leave the surrogate (or even primary in case of bag) key off.

ChssPly76
A: 

Strictly speaking, a surrogate key is not necessary, but a primary key is.

Many people use the term "primary key" to mean a single column that is an auto-incrementing integer. But this is not an accurate definition of a primary key.

A primary key is a constraint on one or more columns that serve to identify each row uniquely. Yes, you need some way of addressing individual rows. This is a crucial characteristic of a relation (aka a table).

You say you have a foreign key and another column that is not unique. But are these two columns taken together unique? If so, you can declare a primary key constraint over these two columns.

Defining another surrogate key (also called a pseudokey -- the auto-incrementing type) is a convenience because some people don't like to have to reference two columns when selecting a single row. Or they want the freedom to change values in the other columns easily, without changing the value of the primary key by which one addresses the individual row.

Bill Karwin