15
Assuming the following fields (for example): Nome
, Rua
, Cidade
,UF
.
The Nome
would be the client’s name. There are people with 2 short names or people with 5 or more names, so it is possible to have a "João Silva" or a "Maria Clara Ramos Santos da Silva", that is, people with 10 characters and people with 27 characters (or even more).
The Rua
would be the customer’s street name: Same case as the previous example.
The Cidade
would be the city of the customer: Same case as the previous example.
The UF
would be the client’s state: As I would like only the Acronym, it will not have been with 5 or more letters, only 2.
It is worth putting VARCHAR(255)
for everything or is it better to choose which size is best for each field? When I say "best," I am referring to nay spend unnecessary space, like having a 255 character field and only storing values with only 2 letters (UF). I read in other answers than the VARCHAR()
is a dynamic format, the value passed between parentheses is just the maximum value that field will support, is that true? If it is, then it means it’s worth putting VARCHAR(255)
for all fields, since the value will be dynamic with a maximum of 255, so the maximum is 255, but it will only have 2 letters so it will store only the space needed for 2 letters and not 255.
"Keep picking better size" = Nome VARCHAR(100)
, Rua VARCHAR(120)
, Cidade VARCHAR(100)
, UF VARCHAR(2)
.
I had this doubt because I am about to create a database for a Mysql system, but my doubt refers to the database in general, not only Mysql.
Varchar is for fields that vary in size. UF and CEP, for example, do not vary, so should be CHAR even. Size is not the only factor. Each type has its purpose, the DB manual describes each one. Even, VARCHAR spends a few bytes more than the content, precisely to store the size.
– Bacco