Sql – Should I use VARCHAR(20) or VARCHAR(255) to store a name

databasesqlstring-lengthvarchar

At the university I was told to use VARCHAR(20) to store a first name. The VARCHAR type takes space depending of string length, so is it necessary to specify smaller length range? I'm asking because RedBean ORM creates on default a VARCHAR(255) field for strings which length is <= 255 chars.

Is it any difference between using VARCHAR(20) and VARCHAR(255)? Except the maximum string length that can be stored 🙂

*I know that such questions have already been asked but all I understand from them is that using VARCHAR(255) where it isn't necessary could cause excessive memory consumption in DB applications.

What it is in real life programming? Should I use VARCHAR(255) for all short text inputs or try to limit length whenever it is possible?

Best Answer

Because 255 is now just an arbitrary choice for a VARCHAR length.

Explanation: Prior to MySQL 5.0.3 (give or take a few point releases - I forget) a VARCHAR column could be 255 characters in length maximum, so VARCHAR(255) was often used as a default. Now, however, you can go up to 65,535 characters on VARCHAR, so if you're still using "255" then that seems arbitrary and not well thought out (or your schema is just old).