MySQL - TEXT vs CHAR and VARCHAR

41,557

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.

Share:
41,557
Alix Axel
Author by

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, 2022

Comments

  • Alix Axel
    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.
    Anon. over 14 years
    Plus a little bit of overhead to indicate that length - and LONGTEXT has more overhead than TEXT has more overhead than VARCHAR.
  • Pindatjuh
    Pindatjuh over 14 years
    True: "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
    Evan Carroll about 14 years
    Logically, TEXT is also stored as a BLOB
  • TommyAutoMagically
    TommyAutoMagically about 9 years
    Soooo if you're storing 256 or more characters, VARCHAR and TEXT take up the same amount of storage space?