Results 1 to 5 of 5

Thread: var and varchar

  1. #1
    Join Date
    Nov 2009
    Posts
    9

    Unanswered: var and varchar

    Hello.

    I understand that VAR is a fixed length datatype and it makes sense using it for strings that have always the same amount of characters. I know that VARCHAR is variable length datatype and it stores only as many characters as it needs to store thus it is better to use it with strings of different length.
    What I don't understand is giving a length to a VARCHAR datatype like for example VARCHAR(25) - what does it really do and when to use it?

    Thanks

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    instead of VAR and VARCHAR, you meant to say CHAR and VARCHAR

    the length of a VARCHAR is the maximum allowed length

    so you cannot enter the string 'abcdefghijklmnopqrstuvwxyz' into a VARCHAR(25) column, because it's too long
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Nov 2009
    Posts
    9
    hmmm that actually makes a lot of sense,

    ...and yes I meant CHAR not VAR (l guess I should quit learning MySQL during night time)

    So it basically prevents inputting longer strings, right?

    Ehh, enough for tonight

    Thanks.

  4. #4
    Join Date
    Nov 2003
    Posts
    2,935
    Provided Answers: 12
    Quote Originally Posted by r937 View Post
    instead of VAR and VARCHAR, you meant to say CHAR and VARCHAR

    the length of a VARCHAR is the maximum allowed length

    so you cannot enter the string 'abcdefghijklmnopqrstuvwxyz' into a VARCHAR(25) column, because it's too long
    Depends.
    I think with the default installation, MySQL will simply truncate the value, but will not refuse to insert it.

  5. #5
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by shammat View Post
    I think with the default installation, MySQL will simply truncate the value, but will not refuse to insert it.
    nicely spotted

    ya learn sumpin new ever day
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Tags for this Thread

Posting Permissions

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