6

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?

2

The 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.

  • What does this have to do with security? – atxdba Oct 17 '14 at 21:33
  • @atxdba "Data integrity" is really a better term. It's just nice to know that one field will always be a certain length, if it's supposed to be. – Libbux Oct 18 '14 at 2:44
  • 2
    To that extent I'd recommend ensuring running the mysqld w/ SQL_MODE strict. This will ensure attempts to insert something longer fails hard with an error. Otherwise it will succeed with just a warning and truncating the value – atxdba Oct 20 '14 at 18:03
1

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).

  • 7
    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. – a_horse_with_no_name Jul 9 '13 at 6:22
0

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.

Full-Text Search

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.