Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old December 24th, 2002, 06:18 AM
RyanA RyanA is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 23 RyanA User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Primary Key .. is it really necessary?

Lets say my tables look like this:

PHP Code:
 customer
    id 
(auto-inc/primary key)
    
name

products
    id 
(auto-inc/primary key)
    
name

order
    customer id 
(index)
    
product id 


..must I have a primary key set for the orders? I can't set either field as a primary key because PKs must be UNIQUE while a customer may place several orders or an item may be purchased by multiple customers.

Why must there be a primary key for every table? (I have created tables without PKs on multiple occasions) It seems like a waste of space to setup an auto-inc id field when I can't see a use for it.

Last edited by RyanA : December 24th, 2002 at 06:21 AM.

Reply With Quote
  #2  
Old December 24th, 2002, 07:08 AM
kingmike's Avatar
kingmike kingmike is offline
Super Genius
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: Orlando, Florida
Posts: 314 kingmike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 7 sec
Reputation Power: 7
Send a message via AIM to kingmike
Sometimes it is, sometimes it's not.

Using a primary key whenever possible is a good idea. It doesn't always have to be an auto_incrementing integer either. The point is to give that one record a unique field to set it apart from the rest. Generally, I'll use a non-integer primary key when I need to make sure that a field containing text can't be duplicated (username, email address, etc). I'll always use an integer primary key when I need to refrence something quickly say ... through a url (category id, product id, order id, etc).

As far as I know, the difference between the a primary key() field and a unique() field is that a primary key automaticly has an index on it.
__________________
Sleep is highly overrated.

Reply With Quote
  #3  
Old December 24th, 2002, 08:36 AM
Wingman Wingman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Bavaria, Germany
Posts: 140 Wingman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 40 m 41 sec
Reputation Power: 6
As kingmike already said, using primary keys wherever possible is a very good idea due to two reason:

1. Indizes

This is database depended, some database automaticly generate an index on every primary key column. This gives your table a huge speed increasement (even on tables with little data), but as our data grows, you can't live w/o indizes.

2. Referential integrity.

A primary key implies a unique column, so you don't have to worry about duplicate entries on it since it's just another constraint.

Also you need primary keys in order to create foreign key constraints (just as kingmike described, but e.g. with MSSQL you don't create other primary key's on your referencing columns but a foreign key)

Reply With Quote
  #4  
Old December 24th, 2002, 10:19 AM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Pembroke Pines, Florida, USA
Posts: 2,300 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 3 h 2 m 4 sec
Reputation Power: 43
I really think this is a question of database design, not just PK usage.

At one point there was a serious attempt by relational database creators to enforce uniqueness on every table. In other words, their point was: if you can't think of a reason why every row of a table should be unique, then you haven't thought through your database design properly.

Even though most SQL DB systems don't enforce uniqueness, I make sure that every table I design has a unique key. "Saying something twice doesn't make it any more true." (C.J. Date). Thus having the same identical record more than once doesn't provide any benefit at all, and is a way to invite confusion into your data.

In your case, I think you are missing a table. You should use the Order table only as a marker for each unique order amount and description, while the orders table would also have a foreign key relationship with an order_details table, which has its own independent primary key, as well as a foreign key matching order IDs and a foreign key matching product IDs. This way, the 'orders' table would handle the main invoice total, while the order_details table would show the line-item details.
__________________
The real n-tier system:

FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

Amazon wishlist -- rycamor (at) gmail.com

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Primary Key .. is it really necessary?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway