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:
  #1  
Old May 29th, 2003, 02:43 PM
criss criss is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Indonesia
Posts: 6 criss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 47 m 51 sec
Reputation Power: 0
Send a message via ICQ to criss Send a message via Yahoo to criss
Question Id As Primary Key

The Id thin make me confused. I have a little problem bout ID.

For example, i want to make a table for 'member' consists of attribut such us :

Table Member, with attribut :
member_code
member_name
member_address
member_email

All of those attributs above could be added or edited by user or administrator. Should I add one attribut named 'id' as the primary key, although the member_code is already unique that could differ every record of the table?

So Far i usually add 'id' (auto_increment) as the primary key. Why?? Because I think we need some uniqid as prymary key that untouchable by user or administrator but computer. So the prymary key is generated by computer. I think that right to avoid the human error on generate the uniqid.
But, I just think it is useless because the member_code could differ every record from the table. Its already unique.

So thats makes me confuse. Should I add 'id' or not?

I need some suggestions please....

Tengkyu....

Reply With Quote
  #2  
Old May 29th, 2003, 03:20 PM
md_doc
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
a primary key is always good to have... but it sounds like you could really make your member_code a primary key no?

Reply With Quote
  #3  
Old May 29th, 2003, 03:39 PM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 8,041 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 6 Days 14 h 32 m 29 sec
Reputation Power: 281
Make shure you'll never have to change your member codes or it will turn in messing up with PK and indexing.
There are also other possible dangers ...

Reply With Quote
  #4  
Old May 29th, 2003, 04:13 PM
md_doc
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
oh yes that is true if you have to change the member_code... but if the member_code is unique already wouldn't it be weird to change it? I guess there are a bunch of cases where you would change a unique value but I cannot think of any where you would do it on a table without a primary key... or at least I would not trust doing it without a primary key.

Reply With Quote
  #5  
Old May 29th, 2003, 04:45 PM
rod k rod k is offline
Apprentice Deity
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237 rod k User rank is Private First Class (20 - 50 Reputation Level)rod k User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 m 8 sec
Reputation Power: 13
Send a message via AIM to rod k
PKs should NEVER change as pabloj mentioned.

Simple rules on whether or not to use an auto-generated PK:

1) Is there any other field in the table that is UNIQUE and will NEVER change?
2) If so, is that field of a type suitable to be used in other tables as a reference?
3) If not, use the auto-generated PK
4) If in doubt, use the auto-generated PK

Reply With Quote
  #6  
Old June 5th, 2003, 09:07 AM
fathomgringo fathomgringo is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 26 fathomgringo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I have a guestion about indexing. I always create an auto incremented column for and id number to be used as a primary key. But recently I saw a php script used to create a mysql table that looked like this:
PHP Code:
 $news_table_def  "id_number mediumint(9) NOT NULL auto_increment,";
$news_table_def .= "add_date datetime NOT NULL default '0000-00-00 00:00:00',";
$news_table_def .= "text_entry text NOT NULL,";
$news_table_def .= "active enum('y','n') NOT NULL default 'y',";
$news_table_def .= "PRIMARY KEY  (id_number),";
$news_table_def .= "UNIQUE KEY id_number (id_number)"

The primary key is set up there just as I would have done it, but this author also added a "unique key" using the primary key. I dont understand why. What is the benefit of that, and how does it work?

Reply With Quote
  #7  
Old June 5th, 2003, 11:03 AM
rod k rod k is offline
Apprentice Deity
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237 rod k User rank is Private First Class (20 - 50 Reputation Level)rod k User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 m 8 sec
Reputation Power: 13
Send a message via AIM to rod k
There is no benefit. PKs are unique by definition. The script author is confused.

Reply With Quote
  #8  
Old June 5th, 2003, 12:01 PM
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 12 m 27 sec
Reputation Power: 56
criss,

I tend to believe that using natural primary keys is often better than an auto-generated one. By 'natural', I mean that the key already has meaning in your database model, as in 'member_code' above.

Yes, rodk is right that you should be careful about the potential for your primary key values to change.

But, even if those values can change, this is not necessarily a problem if your database system has full referential integrity. Most DBMS systems with full referential integrity can force an update of all relate values, when you change the primary key on which they reference. But, if these values are updated too much, this of course could have bad performance implications.

It looks like you are using MySQL. Are you using InnoDB tables with referential integrity? Are you enforcing referential integrity between your tables?

Another note: Some people like to name the primary key in every table 'id'. I think this is a bad design choice. Every table should have a unique name for its primary key, and every table that references that primary key should use the same name. This avoids a lot of confusion as your app gets larger.

For example, if you 'invoices' table has an 'id' column, and you 'customers' table has an 'id' column, then queries which join the two start to look ugly. Also, new every time you reference that key in another table, you will have to call it something like 'invoices_id' and 'customers_id'. I think it is much more natural that the key should have the same name wherever it is used or referenced, and that every primary key should have a unique name in the database.
__________________
The real n-tier system:

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

Amazon wishlist -- rycamor (at) gmail.com

Reply With Quote
  #9  
Old June 5th, 2003, 10:07 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 18,246 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 17 h 3 m 28 sec
Reputation Power: 1055
rod's point #2 -- is that field of a type suitable to be used in other tables as a reference? -- is a very important point

if you find that your natural primary key is a composite (more than one column), then a surrogate key (e.g. auto_increment) is often better, simply from the point of view of efficiency of joins

however, don't forget that when you have a surrogate primary key, it is usually important to declare a unique index on the "real" key -- which is now called a candidate key (a slightly inappropriate name, but more acceptable than "the real pk" which i usually call it)

this failure to add that additional unique index is what leads people to discover that their table eventually has "duplicates"

as for the naming issue, i agree with rycamor -- but it's really six of one versus a half dozen of the other, as there are pros and cons for both schemes (calling every pk "id" versus "foo_id" and "bar_id" etc.)

the only place it really makes a difference is if the database supports natural joins, which you won't be able to use if you call every pk "id"

rudy
http://r937.com/

Reply With Quote
  #10  
Old June 26th, 2003, 11:00 AM
criss criss is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Indonesia
Posts: 6 criss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 47 m 51 sec
Reputation Power: 0
Send a message via ICQ to criss Send a message via Yahoo to criss
So its depend on the database relation structure and the database integrity.

Ok, thanx for all

Reply With Quote
  #11  
Old February 20th, 2004, 04:26 AM
databi databi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 25 databi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Just one minor comment about the id column name: you should always reference columns in DML sentences referencing the name of the table owner of the column, that is:
SELECT tablename.column1name, tablename.column2name,....

especially when you are joining two (or more) tables. Why? Because if in the future you update the structure of any of the tables and there is a column name that is repeteaded (that wasn't before) and you didn't explicitely indicated the table name in the instructions, you will receive an error message, as the database won't be able to decide which of the columns are you referring to, so you will be forced to update your query (update, etc) even if you want to do exactly the same than before. That's why is not confusing to use the name "id" for all the columns in the tables with automatic ids, because you will reference them as "employee.id", "customer.id", etc; otherwise it would be customer.customer_id, employee.employee_id which is a lot of duplication ... you only use a prefix, as customer_id, if you are referencing the id of the customer table in another table (fk column).
In this way you avoid to update your code when you don't have to change its functionality!

Hope this helps!

Cheers,
Ana

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Id As Primary Key


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 |