I have a small social networking website, with posts and comments.
I decided to let users create posts with any number of charس they want, and I think the best Datatype
There are various places where VARCHAR
and TEXT
do or do not differ. Some things I say:
TEXT
.TINYTEXT
-- it has only negatives relative to VARCHAR(255)
.VARCHAR(255)
; pick a reasonable max instead of 255. (This is a minor optimization relating to temp table in complex queries.)CHAR
only for things that are truly fixed length. In almost all such case, tack on CHARACTER SET ascii
(or latin1). (Else it will take more space than you expect.CHARACTER SET ut8mb4
. (Exceptions are becoming more an more rare.)(Sorry, I digress. Back on topic...)
I indexing, in layout of InnoDB rows (there are 4 different ROW_FORMATs
), etc, VARCHAR(513)
will be essentially indistinguishable from TEXT
.
One of the few arguments for VARCHAR
is that it limits the store to the length given. But how often is that important?
Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535, which would seem big enough to handle your user's expected comment length.
The four TEXT types are TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT. The maximum amount of data that can be stored for each data type is approximately:
TINYTEXT 256 bytes
TEXT 65,535 bytes ~64kb
MEDIUMTEXT 16,777,215 bytes ~16MB
LONGTEXT 4,294,967,295 bytes ~4GB
See also When to use TEXT in mysql instead of VARCHAR
Use one of the four type of TEXT available [TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT] depending on your requirements. http://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html