Why should I ever choose any other length than 255 for varchar in MySQL?

39,709

Solution 1

1) If you dont want to limit the maximum size of a stored varchar, then yes it is fine. That being said...

2) In many cases you want to set an upper limit for the size of a varchar. Lets say you are storing a mailing list, and have a limited amount of space for an address line. By setting an upper limit for your address field, you now allow the database to enforce a maximum address line length for you.

Solution 2

Excerpt from the MySQL documentation:

The CHAR and VARCHAR types are similar, but differ in the way they are stored and retrieved. As of MySQL 5.0.3, they also differ in maximum length and in whether trailing spaces are retained.

The CHAR and VARCHAR types are declared with a length that indicates the maximum number of characters you want to store. For example, CHAR(30) can hold up to 30 characters.

The length of a CHAR column is fixed to the length that you declare when you create the table. The length can be any value from 0 to 255. When CHAR values are stored, they are right-padded with spaces to the specified length. When CHAR values are retrieved, trailing spaces are removed.

Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in 5.0.3 and later versions. The effective maximum length of a VARCHAR in MySQL 5.0.3 and later is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.

In contrast to CHAR, VARCHAR values are stored as a one-byte or two-byte length prefix plus data. The length prefix indicates the number of bytes in the value. A column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.

Solution 3

CHAR Vs VARCHAR

CHAR is used for Fixed Length Size Variable.
VARCHAR is used for Variable Length Size Variable.

E.g.

create table emp
(f_name CHAR(20),
 l_name VARCHAR(20)
);

insert into emp values('Suraj','Chandak');

select length(f_name), length(l_name) from emp;

Output will be

length(f_name)          Length(l_name)
   20                       7

The best answer for CHAR vs VARCHAR

Edit

  • You can set maximum upper limit for the column.
  • Performance and storage can have effect.

Thanks.

Solution 4

Fixed-length (Static) Tables are Faster. When every single column in a table is “fixed-length”, the table is also considered “static” or “fixed-length”. Examples of column types that are NOT fixed-length are: VARCHAR, TEXT, BLOB.

http://net.tutsplus.com/tutorials/other/top-20-mysql-best-practices/

So if your table does not have any other fields that are varchar, text, or blob; you can use char and make your table a static one. That way they are faster.

Solution 5

I have read elsewhere that varchar comes with a performance hit relative to char, when you run selects against columns defined with them. So, maybe you want to choose char, if you know for sure the field will always be a certain length, and you have performance issues...

Share:
39,709
Lizard
Author by

Lizard

I am a PHP Web Developer

Updated on July 18, 2020

Comments

  • Lizard
    Lizard almost 4 years

    I know the differnce between CHAR and VARCHAR,

    CHAR - Fixed length

    VARCHAR - Variable length (size + 1 byte)

    But I wanted to know what was the purpse of the having the option for a varchar length e.g. VARCHAR(50), VARCHAR(100), VARCHAR(255)

    This seems pointless to me because the actual space used depends on the value stored in the database.

    So my questions are:

    1) It is fine to set all my varchar's to 255 2) Why would you want to specify any other lenght?

  • OMG Ponies
    OMG Ponies almost 14 years
    Mind that you link to the source when you quote.
  • qdot
    qdot about 12 years
    sqlite is not exactly high performance database - and varchar is a performance hit if it forces the row size to be variable.
  • Toby Allen
    Toby Allen about 12 years
    Yes but for most people and most uses its fast enough
  • qdot
    qdot about 12 years
    True, but claiming that it 'no longer' matters and using sqlite as a pinnacle of database engineering is really misleading. And no, for most people and most uses it isn't fast enough - it's just easiest to configure and that outweighs the costs, at least initially.. as long has you never have simultaneous requests from more than one user, that is.
  • jweyrich
    jweyrich over 10 years
    A small correction: The linked documentation says VARCHAR has an upper limit of 65,535 for MySQL >= 5.0.3. Quoting: "The length can be specified as a value from 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in 5.0.3 and later versions."
  • dotancohen
    dotancohen over 10 years
    I would like to see a reference for this claim.
  • Greg Gauthier
    Greg Gauthier over 10 years
    Fair enough, and I would give you one, only this comment was over three years ago, and I can't even remember where I saw it.
  • Davor
    Davor over 9 years
    That answer has nothing to do with the question asked here.
  • Davor
    Davor over 9 years
    The question is "1) It is fine to set all my varchar's to 255 2) Why would you want to specify any other lenght?", not CHAR vs VARCHAR.
  • musafar006
    musafar006 over 9 years
    and you are right. I don't remember why I posted that answer. Maybe at that time, I was searching for something related. Maybe he edited his question?
  • Davor
    Davor over 9 years
    Dunno, could be. I don't know how to see his edit history, or whether I have enough reputation to see it in the first place.