What's the difference between VARCHAR(255) and TINYTEXT string types in MySQL?
Each of them allows to store strings with a maximum length of 255 characters. Storage requirements are also the same. When should I prefer one over another?
TINYTEXT shines over VARCHAR when storing data that's under 255 characters with an inconsistent length and no need to be used for sorting criteria.
The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used. Make sure you are aware of the effects of a multi-byte character set. VARCHAR(255) stores 255 characters, which may be more than 255 bytes.
TINYTEXT can store up to 255 characters i.e 255 bytes. It is suitable for storing summaries of articles, short notices, captions, etc. It takes 1-Byte overhead. MEDIUMTEXT. MEDIUMTEXT can store up to 16,777,215 characters i.e 16,777,215 bytes or 64MB of data.
1 byte for the length, and the actual storage for the rest. 255 is the max limit, not an allocation. 255 is not the max limit. There is a variable MAX you can use which Varchar(max) stores a maximum of 2 147 483 647 characters.
You cannot assign a DEFAULT
value to a TINYTEXT
and you cannot create an unprefixed index on the latter.
Internally, additional objects are allocated in memory to handle TEXT
(incl. TINYTEXT
) columns which can cause memory fragmentation on the large recordsets.
Note that this only concerns the column's internal representation in the recordsets, not how they are stored on the disk.
Using VARCHAR
you can set the column to NULL
or NOT NULL
and you can set DEFAULT
value, but not with TEXT
. Use VARCHAR
if you need one or both feature, NULL
and DEFAULT
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With