Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - TEXT vs CHAR and VARCHAR

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?

like image 781
Alix Axel Avatar asked Jan 18 '10 03:01

Alix Axel


People also ask

What is difference between CHAR VARCHAR and TEXT in mysql?

TEXT has a fixed max size of 2¹⁶-1 = 65535 characters. VARCHAR has a variable max size M up to M = 2¹⁶-1 . So you cannot choose the size of TEXT but you can for a VARCHAR . The other difference is, that you cannot put an index (except for a fulltext index) on a TEXT column.

Should I use TEXT or VARCHAR in mysql?

In most circumstances, VARCHAR provides better performance, it's more flexible, and can be fully indexed. If you need to store longer strings, use MEDIUMTEXT or LONGTEXT, but be aware that very large amounts of data can be stored in columns of these types.

Is TEXT a CHAR or VARCHAR?

The CHAR is fixed-length character type while the VARCHAR and TEXT are varying length character types. Use VARCHAR(n) if you want to validate the length of the string ( n ) before inserting into or updating to a column. VARCHAR (without the length specifier) and TEXT are equivalent.

What is TEXT in mysql?

TEXT is the family of column type intended as high-capacity character storage. The actual TEXT column type is of four types-TINYTEXT, TEXT, MEDIUMTEXT and LONGTEXT. The four TEXT types are very similar to each other; the only difference is the maximum amount of data each can store.


1 Answers

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.

like image 136
Pindatjuh Avatar answered Sep 21 '22 16:09

Pindatjuh