MySQL - TEXT vs CHAR and VARCHAR
Solution 1
TEXT is a variable length datatype, with a maximum of 65,000 characters.
LONGTEXT can be used for over 4 trillion characters.
To answer your question: it's a variable lenght, and it will only occupy the amount of characters you store.
Solution 2
TEXT occupies a number actual length of your data + 2 bytes.
Alix Axel
If you need to, you can contact me at: alix [dot] axel [at] gmail [dot] com. I'm #SOreadytohelp Some of my GitHub repositories: phunction, a minimalistic PHP HMVC Framework. halBox, bash script to bootstrap Debian/Ubuntu servers. ArrestDB, RESTful API for SQLite, MySQL and PostgreSQL databases. genex.js, Genex module for Node.js. If you know how to work with regexes, have a look at http://namegrep.com/. ;)
Updated on July 09, 2022Comments
-
Alix Axel almost 2 years
Reading this question, a doubt popped into my head:
- char and varchar can store up to 255 chars
- text can store up to 65k chars
- char size in bytes is number of chars
- varchar size in bytes is number of chars used + 1
So how much bytes does TEXT actually occupy? ~65KB or number of chars used + 1?
-
Anon. over 14 yearsPlus a little bit of overhead to indicate that length - and LONGTEXT has more overhead than TEXT has more overhead than VARCHAR.
-
Pindatjuh over 14 yearsTrue: "LONG" in "LONGTEXT" actually indicates how much characters. A long value is 8 bytes, so the overhead is 8 bytes. Though it's insignificant, thus I didn't mention it.
-
Evan Carroll about 14 yearsLogically, TEXT is also stored as a BLOB
-
TommyAutoMagically about 9 yearsSoooo if you're storing 256 or more characters, VARCHAR and TEXT take up the same amount of storage space?