SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist ckchin's Avatar
    Join Date
    Mar 2002
    Location
    msia
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Char versus Varchar

    I got this from book ... if your field stores something like last name, you'll probably want to allow 25 characters, just to be safe. If you were to use the char column type and someone had last name 'Smith', your column would contain 20 trailing spaces. there's no need for it; you're much better off using varchar and allowing MySQL to track the size of the column. However, when you want to store passwords of five to seven characters, it would be a waste to use varchar to track the size of the column. Every time avarchar field is update, MySQL has to check the length of the field and change the character that stores the field length. You'd better off using char(7).

    My question is:

    Does it mean that when we use the varchar type, mysql need to check the total number of characters in the varchar field and discharge any unnecessary space that is not in use, eg: in smith case, we save 20 char spaces. The trade-off is it slow down the mysql speed. Am I right?

    BTW, do not use a CHAR column for this in MySQL by DR_LaRRY_PEpPeR

    http://www.sitepointforums.com/showt...threadid=57448

  2. #2
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    first of all, that quote from me was just telling him not to store numerical data in a text-type column. it could've said "do not use a CHAR or VARCHAR column for this in MySQL."

    manual page that explains CHAR and VARCHAR: www.mysql.com/doc/C/H/CHAR.html

    basically CHAR is fixed length and VARCHAR is variable length, like your book says. you can use CHAR(x) if you know that the length of the text will always be x. use VARCHAR for text that will vary in length. however, if you specify CHAR or VARCHAR, MySQL may silently change its type to the other one. this is explained here: www.mysql.com/doc/S/i/Silent_column_changes.html

    i have another post about this here (last 3 paragraphs): http://www.sitepointforums.com/showt...370#post399370
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  3. #3
    SitePoint Evangelist ckchin's Avatar
    Join Date
    Mar 2002
    Location
    msia
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks DR_LaRRY_PEpPeR


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
  •