MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old July 5th, 2004, 09:41 AM
bah26 bah26 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 18 bah26 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 29 sec
Reputation Power: 0
PRIMARY KEY: INT v. VARCHAR(255)

I've got the following design:
Code:
CREATE TABLE a( 
   aId INT NOT NULL, 
   aValue VARCHAR(255) NOT NULL, 

   PRIMARY KEY (aId), 
   UNIQUE (aValue) 
) TYPE=InnoDB; 

CREATE TABLE b( 
   ... 
   aId INT NOT NULL, 
   ... 
   FOREIGN KEY aId REFERENCES a (aId), 
   ... 
) TYPE=InnoDB;


Is my design (above) prefered to the following:
Code:
CREATE TABLE a( 
   aValue VARCHAR(255) NOT NULL, 
   PRIMARY KEY (aValue), 
) TYPE=InnoDB; 

CREATE TABLE b( 
   ... 
   aValue VARCHAR(255) NOT NULL, 
   ... 
   FOREIGN KEY aValue REFERENCES a (aValue), 
   ... 
) TYPE=InnoDB;



In other words: is using an INT PRIMARY KEY prefered to using a VARCHAR PRIMARY KEY.

In what cases should a VARCHAR (if ever) PRIMARY KEY be used??


Cheers,

Ben

Reply With Quote
  #2  
Old July 5th, 2004, 10:36 AM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Location: Sydney, AU
Posts: 2,058 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 6 m 11 sec
Reputation Power: 8
Send a message via ICQ to stumpy Send a message via MSN to stumpy
I depends on what it is that you are storing in the varchar field. It was discussed here recently (and concluded) that the most appropriate key is simply a meaningless, incrememting number - http://forums.devarticles.com/t8658/s.html
__________________
DevArticles Moderator
BlueSix - Web Development and Consulting

Reply With Quote
  #3  
Old July 6th, 2004, 03:02 AM
bah26 bah26 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 18 bah26 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 29 sec
Reputation Power: 0
Quote:
Originally Posted by stumpy
I depends on what it is that you are storing in the varchar field. It was discussed here recently (and concluded) that the most appropriate key is simply a meaningless, incrememting number - http://forums.devarticles.com/t8658/s.html

I read the article - and it appears there are very mixed views!

Does a VARCHAR affect the performance in anyway? (being a variable length field)

I can gaurantee that the VARCHAR value is unique. Example:
Animal(id, animalName, .....)

In this case it makes no sense to have the same animalName twice!! Therefore animalName is a suitable PRIMARY KEY, however..... I don't want to limit the length of the animalName, therefore would like to use VARCHAR(255)..... But will this create performance problems??


Cheers for your help,

Ben

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > PRIMARY KEY: INT v. VARCHAR(255)


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 5 hosted by Hostway