+ Reply to Thread
Results 1 to 7 of 7

Thread: VARCHAR Field Length

  1. #1
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    VARCHAR Field Length

    As I understand it, a VARCHAR field type only uses the amount of space it needs for the data it contains, so does it have a huge affect on space useage if I make all my VARCHAR fields 255 in size, as opposed to making them different in length.

    For example a LastName field may only need to be a size of 30, but would it make a difference to data storage if I made it 255 in size? If someone's surname was Smith, as I understand it, a field of 30 would use the same amount of space to store that name as a field of 255 would.

  2. #2
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    Personally i always use the maximum characters i expect/allow for a field.
    So if the field should have max 10 characters, i set 10 for it.

    Regarding your question, see if this link answers you!
    What are the optimum varchar sizes for MySQL? - Stack Overflow

  3. #3
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    That helps alot m. Thanks.

  4. #4
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    Quote Originally Posted by richyrich View Post
    That helps alot m. Thanks.
    cool

  5. #5
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Second micky here -- I always make my varchar field match the real world equivilents, thus if the max number of characters allowed in a name fied is 30, then I'll use 30.

    You are correct in that varchar fields only use space as needed, tho the minimum length if I remember correctly is 2-bytes; basically the overhead for storing the length. Thus, varchars don't make much sense for storing short strings unless you expect a large number of empty values. For example, if I have a SKU that's 6 characters in almost every case, I'll use char(6) over varchar(6) in this case to save the 2-byte overhead. In other words, varchar makes the best sense when the length of the stored values is greatly variable or there are a large number of empty values.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  6. #6
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Yeah. If I have a fixed length string, I'd use a CHAR field also.

    I'm working on something where I'm not too sure what the maximum length will be (lots of chemical names and things! ) hence why I'm not sure what size to make some of the fields.

  7. #7
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    In that case, go with VARCHAR(MAX) or VARCHAR(8000) or whatever MySql supports and have no worries.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

+ Reply to Thread

Similar Threads

  1. Length cannot be less than zero error
    By peebman2000 in forum .NET Development
    Replies: 4
    Last Post: December 18th, 2008, 05:15 PM
  2. Length should be 8 digits
    By Rebelle in forum JavaScript Programming
    Replies: 1
    Last Post: October 15th, 2008, 09:06 AM
  3. Form: Textarea (restrict length)
    By Rebelle in forum JavaScript Programming
    Replies: 2
    Last Post: September 11th, 2008, 11:40 AM
  4. Field that is a link (rs) and/or text field question
    By Rebelle in forum ASP Development
    Replies: 14
    Last Post: August 12th, 2008, 09:43 AM
  5. Conversion failed when converting the varchar error
    By guddu in forum Microsoft SQL Server
    Replies: 9
    Last Post: July 17th, 2008, 12:08 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO