tags:

views:

128

answers:

9

Can anyone help me understand if a table in a relational database (such as MySQL) can be without a primary key?

For example, I could have table day_temperature, where I register temperature and time. I don't see the reason to have a primary key for such a table.

Thoughts?

Thanks, Boda Cydo.

+2  A: 

If the possibility of having duplicate entries (for example for the same time) is not a problem, and you don't expect to have to query for specific records or range of records, you can do without any kind of key.

p.marino
+5  A: 

Wouldn't the primary key naturally be the time? would you ever have more than one temperature for a given time?

A better question to ask would be, "Why would you ever make a table without a primary key"

Joel Martinez
+1 Assuming there's no other data time would have to be a logical primary key. Don't see how you could get 2 different temperatures at the same exact time...
Walter
In my opinion natural primary key is bad. Of course there cannot be two different temperatures at the same time, unless you collect temperatures from device which returns result every 15 seconds, but you try to save time of measurement without seconds :(. In other words -- any kind of bug or wrong assumption is typical killer of natural primary key.
Grzegorz Gierlik
A: 

The time would then become your primary key. It will help index that column so that you can query data based on say a date range. The PK is what ultimately makes your row unique, so in your example, the datetime is the PK.

JonH
+3  A: 

You don't need a PK, but it's recommended that you have one. It's the best way to identify unique rows. Sometimes you don't want an auto incremental int PK, but rather create the PK on something else. For example in your case, if there's only one unique row per time, you should create the PK on the time. It makes looks up based on time faster, plus it ensures that they're unique (you can be sure that the data integrity isn't violated):

reko_t
+2  A: 

Like always it depends.

Table does not have to have primary key. Much more important is to have correct indexes. On database engine depends how primary key affects indexes (i.e. creates unique index for primary key column/columns).

In your case I would add a new auto increment unique column like temp_id and make it primary key. It makes much easier maintaining this table -- for example finding and removing records (i.e. duplicated records).

Grzegorz Gierlik
A: 

I've got a better example of a table that doesn't need a primary key - a joiner table. Say I have a table with something called "capabilities", and another table with something called "groups", and I want a joiner table that tells me all the capabilities that all the groups might have, so it's basicallly

create table capability_group
(  capability_id varchar(32),
    group_id     varchar(32));

There is no reason to have a primary key on that, because you never address a single row - you either want all the capabilities for a given group, or all the groups for a given capabilty. It would be better to have a unique constraint on (capabilty_id,group_id), and separate indexes on both fields.

Paul Tomblin
But by giving it a unique constraint you are acknowledging it is a Candidate Key so why not just make it the PK? Implementation wise it would use an index to enforce the constraint anyway.
Martin Smith
In this case, `(capability_id, group_id)` should make a composite `PRIMARY KEY`. This will also make this table more efficient for the engines with clustered storage (`InnoDB`, `SQL Server` (clustered PK), `Oracle` (`ORGANIZATION INDEX`))
Quassnoi
+1  A: 

IF you ended up with duplicate entries you would struggle to update or delete one without a PK (though it would be possible via ROW_NUMBER())

Martin Smith
+5  A: 

Technically, you can declare such a table.

But in your case, the time should be made the PRIMARY KEY, since it's probably wrong to have different temperatures for the same time and probably useless to have same more than once.

Logically, each table should have a PRIMARY KEY so that you could distinguish two records.

If you don't have a candidate key in you data, just create a surrogate one (AUTO_INCREMENT, SERIAL or whatever your database offers).

The only excuse for not having a PRIMARY KEY is a log or similar table which is a subject to heavy DML and having an index on it will impact performance beyond the level of tolerance.

Quassnoi
Celko would say it's not a table if it doesn't have a PK!
Martin Smith
@Martin: relational databases and `SQL` operate with multisets, not sets, so it would still be a table. However, internally, this is still a set (the records should be distinguished), and a surrogate `PK` just helps making the records distinguishable from the outside.
Quassnoi
+1 Interesting I had never never heard that before. Someone should tell Joe! http://www.eggheadcafe.com/software/aspnet/31906606/delete-only-9-of-10-rows.aspx
Martin Smith
@Martin: also in my blog: http://explainextended.com/2009/03/14/deleting-duplicates/ But good luck doing the same in `MySQL` :)
Quassnoi
A: 

I would include a surrogate/auto-increment key, especially if there is any possibility of duplicate time/temperature readings. You would have no other way to uniquely identify a duplicate row.

Steve