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

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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 July 3rd, 2002, 03:34 AM
Castaway Castaway is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Posts: 5 Castaway User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Limiting the length of varchar

Is there a point in defining varchar-fields that are smaller than 255 bytes? Does it help with indexing or something?

Reply With Quote
  #2  
Old July 3rd, 2002, 08:57 AM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed God (5000 - 5499 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 5,163 Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 6 Days 1 h 34 m 20 sec
Reputation Power: 791
size usage. Only use what you need.

Reply With Quote
  #3  
Old July 4th, 2002, 04:49 AM
Castaway Castaway is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Posts: 5 Castaway User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Excuse my ignorance, but isn't the difference between char and varchar that when using the latter, only the amount of characters that equals the length of the string are saved, plus one byte indicating the length of the string. When using char, by contrast, a fixed amount of data is saved, no matter how long the contents may be.

So if you have, for example, a field called email that contains users' email addresses, averaging some 25 characters in length. You could keep this information in a char field with a fixed size of, say 40 characters, but then there would always be fairly much slack. Another thing is that when the user with an email address with one character more than you have defined as length arrives, problems arise.

A better solution would most probably be to use a varchar field with a size of at least 40 characters. My assumption is, therefore, that because only the amount of bytes needed to store the string, plus one byte for size info, are saved in a varchar field, there should be no reason whatsoever not to always use varchar fields of size 255, unless you are using the database itself to restrict the length of the (user) input to the varchar fields (which should be done with the overlying software anyway). Varchar(40) and varchar(255) both take the same amount of space, assuming (in the context of this example) that the strings stored are under 40 characters long.

Because the using of varchar fields with a size less than 255 characters is quite common thing to see in modern database table definitions, I'm just wondering what is the point behind this approach.

Reply With Quote
  #4  
Old July 5th, 2002, 09:21 AM
MattR MattR is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: High above the mucky-muck (Columbus, OH)
Posts: 266 MattR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Send a message via ICQ to MattR
There are little/no performance implications with a VARCHAR( 255 ) vs. VARCHAR( 40 ) in theory. I know Oracle/MS SQL/Sybase have no problems with them, but I do not know about MySQL.

However, your data structures should match the business requirements. This way the data structure themselves becomes a data dictionary for others to model their applications (report generation and the like). If you have your email address VARCHAR( 255 ) and another application reads/writes to it, they may inadvertantly not adhere to a 40-character limit, which will break your first application. You would need strict documentation stating "Yeah we have it set to 255 but it MUST be kept under 40" which every application must remember to adhere to. Defining it VARCHAR( 40 ) is self-documenting.

Also, indexes may be needlessly wide on a VARCHAR( 255 ) regardless of whether or not you have 255 characters in there. It depends on your RDBMS and how they physically store their indexes so it would be wise to see if inefficiencies exist in a VARCHAR( 255 ) index over a VARCHAR( 40 ).

Further RDBMS-specific indexing may impose a limit on the number of bytes an index can hold. So if you define your table with username and password of 255 characters and attempt to place an index on those two (since typically authentication is done with username and password) you will not be able to create it if your max index length (as specified by the RDBMS) is 300 bytes.

Reply With Quote
  #5  
Old July 8th, 2002, 05:04 AM
Castaway Castaway is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Posts: 5 Castaway User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks MattR, now I can sleep a bit better

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Limiting the length of varchar

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap