Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between VARCHAR(255) and TINYTEXT string types in MySQL?

Tags:

types

mysql

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?

like image 661
planetp Avatar asked Feb 18 '10 12:02

planetp


People also ask

What is the difference between VARCHAR and Tinytext?

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.

What does VARCHAR 255 mean in MySQL?

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.

What is Tinytext in MySQL?

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.

Why we use VARCHAR 255 in SQL?

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.


2 Answers

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.

like image 174
Quassnoi Avatar answered Oct 11 '22 09:10

Quassnoi


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.

like image 41
Donny Kurnia Avatar answered Oct 11 '22 11:10

Donny Kurnia