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?

3 Answers 3

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.

3
  • What does this have to do with security?
    – atxdba
    Oct 17, 2014 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, 2014 at 2:44
  • 3
    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, 2014 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).

1
  • 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.
    – user1822
    Jul 9, 2013 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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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