Why is Varchar(255) widely used?

Asked

Viewed 12,535 times

31

Why in various databases, say to set the size as varchar(255)?

For example, in a field email why put varchar(255)? It would not be better (in question of storage and processing speed) to put varchar(80) or maybe varchar(100)? In addition there are few people who have more than 80 characters in the email.

In my case I was looking on the internet, looking about utf8mb4 and saw that in some fields it is already said to put varchar(255), context-independent.

  • 1

    You didn’t want reference?

5 answers

35


The VARCHAR by definition has variable size, you can put the size you want there, at least within the ANSI standard. This 255 is just information of how it should be shown in a selection, it does not mean that it will occupy this disk space. There is no waste.

You can define whatever you want, it doesn’t change anything significantly. 255 is a number that holds most of the information. In the past this was the maximum size of a VARCHAR, then I could show everything.

Some databases use this as Constraint and do not let pass of this value, ie it is only a maximum possible.

Specifically about email if you want to have enough space for any email that can be up to 254 in size according to RFC. You can’t model the bank to handle 99.999% inserts, it has to be suitable for 100% of registrations.

It is the opposite of what in theory should happen to the CHAR that has fixed size. In thesis if a CHAR is 255 in size that is what it will occupy even if the value only has one character. I say in theory, because it has Sgdbs that can store up to one CHAR variable size, although it preserves the semantics of CHAR. If the row has some column of variable size has no reason to keep one of fixed size, there is no gain in it.

This can be confirmed in OS response from one of the leading database experts. To Wikipedia talks about it. And you still have the documentation of the guys in the databases:

  • 1

    It is written in the answer that.

  • Thank you very much, I just wanted a reference

  • 1

    From what I understood in the reference in the case of Mysql when you fix the size of VARCHAR in 255 without need causes greater memory consumption in joins.

  • 1

    @Piovezan no JOIN it’s even possible

13

The VARCHAR by definition has variable size, but you can not put the size you want there, as answered above. This 255 is the maximum size allowed for the column, which in the case of the Oracle, the maximum possible is 4000.

In fact, he uses only what is necessary. So, for example, in the case of a 10-character string (in 1-byte Charset), only 10 characters will be stored.

6

Varchar(255) is widely used because it is the default value and many developers keep this value non-stop to wonder if it really takes 255 characters to store that information.

I do not see as a problem of the database but of the lack of attention to the details of who set the table for the first time.

3

I believe that formerly in several databases, the maximum size was 250, so it ended up "standardizing" this common size.

-1

In fact, the maximum size of the varchar was 255. As we can notice, 255 would be exactly the size of 1Byte, or 8 bits, which was the maximum address size. The number 255 in binary is 11111111, that is, all "setted" Bites in "UP". Nowadays, in the most modern DBMS, it is already possible to increase this storage.

  • "varchar maximum size was 255" => can share the font? The question is about Mysql if we believe the tags, but then you say "in the most modern Dbms", indicating that the phrase I highlighted was true in many Dbms

Browser other questions tagged

You are not signed in. Login or sign up in order to post.