Friday 9 November 2018

Maximum length for MySQL type text

I'm creating a form for sending private messages and want to set the maxlength value of a textarea appropriate to the max length of a text field in my MySQL database table. How many characters can a type text field store?
If a lot, would I be able to specify length in the database text type field as I would with varchar?

 Answers


TINYBLOB, TINYTEXT       L + 1 bytes, where L < 2^8    (255 Bytes)
BLOB, TEXT               L + 2 bytes, where L < 2^16   (64 Kibibytes)
MEDIUMBLOB, MEDIUMTEXT   L + 3 bytes, where L < 2^24   (16 Mebibytes)
LONGBLOB, LONGTEXT       L + 4 bytes, where L < 2^32   (4 Gibibytes)
L is the number of bytes in your text field. So the maximmum number of chars for text is 216-1 (using single-byte characters). Means 65 535 chars(using single-byte characters).
UTF-8/MultiByte encoding: using MultiByte encoding each character might consume more than 1 byte of space. For UTF-8 space consumption is between 1 to 4 bytes per char.



Type       | Approx. Length     | Exact Max. Length Allowed
-----------------------------------------------------------
TINYTEXT   | 256 Bytes          | 255 characters
TEXT       | 64  Kilobytes      | 65,535 characters
MEDIUMTEXT | 16  Megabytes      | 16,777,215 characters
LONGTEXT   | 4   Gigabytes      | 4,294,967,295 characters
Note: If using multibyte characters, the column "Exact Max. Length Allowed" will have different length. For example: if you use 2-bytes characters, the exact maximum length for TINYTEXT would be 127 characters. Basically, it's the number of bytes allowed -1.



How many characters can a type text field store?
According to Documentation You can use maximum of 21,844 characters if the charset is UTF8
If a lot, would I be able to specify length in the db text type field as I would with varchar?
You dont need to specify the length. If you need more character use data types MEDIUMTEXT or LONGTEXT. With VARCHAR, specifieng length is not for Storage requirement, it is only for how the data is retrieved from data base.



TEXT is a string data type that can store up to 65,535 characters. But still if you want to store more data then change its data type to LONGTEXT
ALTER TABLE name_tabel CHANGE text_field LONGTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;

0 comments:

Post a Comment