I currently have a database with the "user" table having username as one of the columns.
I allow a maximum of 15 characters for the username in my system. Is it fine to have the username column as a varchar(15) datatype?
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It only takes a minute to sign up.
Sign up to join this communityThe best way to store any short length string in plain text is to use the varchar
datatype, setting the maximum length to 15, only for the purpose of integrity. The length of the username should obviously be validated within the application.
The only thing to keep in mind would be the charset of the database, table, and column. Make sure that you are using a charset which covers all of the expected characters in the username, in whatever human language they are being inserted in.
You should choose CHAR(15)
instead of VARCHAR(15)
.
Please have a look at link What is the performance impact of using CHAR vs VARCHAR on a fixed-size field? You will get answer why to choose CHAR(15).
char(15)
is not a good choice due to the blank padding that is applied. It might be a good choice for values that are always the same length, but not for anything that as a variable length.
Yes, you can use varchar(15) datatype to store username.
Varchar stores variable-length character string. it can require less storage than fixed-length types because it uses only as much space as it need.
Varchar(15) actually allocates dynamically up to 16 bytes, up to 15 for data and, at least, 1 additional byte to store the the length of the data.
If you consider Indexing you can user Full-Text Search function in this varchar(15) Username column.