353

I would like to know what the max size is for a MySQL VARCHAR type.

I read that the max size is limited by the row size which is about 65k. I tried setting the field to varchar(20000) but it says that that's too large.

I could set it to varchar(10000). What is the exact max I can set it to?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
user1832628
  • 3,859
  • 3
  • 16
  • 10

8 Answers8

341

Keep in mind that MySQL has a maximum row size limit

The internal representation of a MySQL table has a maximum row size limit of 65,535 bytes, not counting BLOB and TEXT types. BLOB and TEXT columns only contribute 9 to 12 bytes toward the row size limit because their contents are stored separately from the rest of the row. Read more about Limits on Table Column Count and Row Size.

Maximum size a single column can occupy, is different before and after MySQL 5.0.3

Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in 5.0.3 and later versions. The effective maximum length of a VARCHAR in MySQL 5.0.3 and later is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.

However, note that the limit is lower if you use a multi-byte character set like utf8 or utf8mb4.

Use TEXT types inorder to overcome row size limit.

The four TEXT types are TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT. These correspond to the four BLOB types and have the same maximum lengths and storage requirements.

More details on BLOB and TEXT Types

Even more

Checkout more details on Data Type Storage Requirements which deals with storage requirements for all data types.

rajukoyilandy
  • 5,341
  • 2
  • 20
  • 31
70

As per the online docs, there is a 64K row limit and you can work out the row size by using:

row length = 1
             + (sum of column lengths)
             + (number of NULL columns + delete_flag + 7)/8
             + (number of variable-length columns)

You need to keep in mind that the column lengths aren't a one-to-one mapping of their size. For example, CHAR(10) CHARACTER SET utf8 requires three bytes for each of the ten characters since that particular encoding has to account for the three-bytes-per-character property of utf8 (that's MySQL's utf8 encoding rather than "real" UTF-8, which can have up to four bytes).

But, if your row size is approaching 64K, you may want to examine the schema of your database. It's a rare table that needs to be that wide in a properly set up (3NF) database - it's possible, just not very common.

If you want to use more than that, you can use the BLOB or TEXT types. These do not count against the 64K limit of the row (other than a small administrative footprint) but you need to be aware of other problems that come from their use, such as not being able to sort using the entire text block beyond a certain number of characters (though this can be configured upwards), forcing temporary tables to be on disk rather than in memory, or having to configure client and server comms buffers to handle the sizes efficiently.

The sizes allowed are:

TINYTEXT          255 (+1 byte  overhead)
TEXT          64K - 1 (+2 bytes overhead)
MEDIUMTEXT    16M - 1 (+3 bytes overhead)
LONGTEXT      4G  - 1 (+4 bytes overhead)

You still have the byte/character mismatch (so that a MEDIUMTEXT utf8 column can store "only" about half a million characters, (16M-1)/3 = 5,592,405) but it still greatly expands your range.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 4
    Keep in mind that TEXT types are NOT able to be stored in memory tables, so there is a significant performance penalty for using them when a VARCHAR would suffice. – Camden S. Jan 28 '14 at 01:01
  • 1
    'the three-bytes-per-character property of utf8' of *MySql utf8*, which isn't actually utf8 at all. In reality the [max. bytes in a utf-8 char](https://stijndewitt.wordpress.com/2014/08/09/max-bytes-in-a-utf-8-char/) is **4**. For this reason you should always [set the encoding to `utf8mb4` in MySQL](https://mathiasbynens.be/notes/mysql-utf8mb4). `utf8mb4` is MySql's name for what the rest of the word calls utf8. – Stijn de Witt May 31 '15 at 22:38
  • 1
    @StijndeWitt, thanks for that. clarified to indicate I meant MySQL's utf8 encoding method rather than UTF-8. I generally use the capitalised variant to indicate "real" UTF-8 since that's the accepted IANA convention. – paxdiablo Sep 08 '15 at 05:45
  • What's `delete_flag`? – Sumit Oct 12 '20 at 11:47
43

Source

The max length of a varchar is subject to the max row size in MySQL, which is 64KB (not counting BLOBs):

VARCHAR(65535) However, note that the limit is lower if you use a multi-byte character set:

VARCHAR(21844) CHARACTER SET utf8

Community
  • 1
  • 1
Attila
  • 3,206
  • 2
  • 31
  • 44
  • 32
    Please stop using `CHARACTER SET utf8` in examples. It should be `CHARACTER SET utf8mb4` (if you want *all* Unicode text to be stored properly... and who doesn't want that?) – Stijn de Witt Sep 08 '15 at 09:15
  • 7
    For `CHARSET=utf8mb4` use `VARCHAR(16383)`. – Wil Moore III Jan 06 '17 at 22:10
  • 3
    Using utf8mb4 will put you up against the limit on index width in a situation where utf8 does not. If you examine the character sets that are included in utf8mb4 but not in utf8, you may find that including all of the various forms of hieroglyphics and other such arcane character sets is not worth the significant performance penalty (determined empirically). It's not quite as cut and dried as Stijn implies. – kcrossen Jul 02 '17 at 17:03
  • 2
    Many emojis are also present in utf8mb4 and missing from utf8, so that may change the equation of whether it's worthwhile. – Brian Morearty Feb 04 '19 at 18:48
35

From MySQL documentation:

The effective maximum length of a VARCHAR in MySQL 5.0.3 and later is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used. For example, utf8 characters can require up to three bytes per character, so a VARCHAR column that uses the utf8 character set can be declared to be a maximum of 21,844 characters.

Limits for the VARCHAR varies depending on charset used. Using ASCII would use 1 byte per character. Meaning you could store 65,535 characters. Using utf8 will use 3 bytes per character resulting in character limit of 21,844. BUT if you are using the modern multibyte charset utf8mb4 which you should use! It supports emojis and other special characters. It will be using 4 bytes per character. This will limit the number of characters per table to 16,383. Note that other fields such as INT will also be counted to these limits.

Conclusion:

utf8 maximum of 21,844 characters

utf8mb4 maximum of 16,383 characters

Firze
  • 3,939
  • 6
  • 48
  • 61
7

Before Mysql version 5.0.3 Varchar datatype can store 255 character, but from 5.0.3 it can be store 65,535 characters.

BUT it has a limitation of maximum row size of 65,535 bytes. It means including all columns it must not be more than 65,535 bytes.

In your case it may possible that when you are trying to set more than 10000 it is exceeding more than 65,535 and mysql will gives the error.

For more information: https://dev.mysql.com/doc/refman/5.0/en/column-count-limit.html

blog with example: http://sforsuresh.in/mysql_varchar_max_length/

Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
6

you can also use MEDIUMBLOB/LONGBLOB or MEDIUMTEXT/LONGTEXT

A BLOB type in MySQL can store up to 65,534 bytes, if you try to store more than this much data MySQL will truncate the data. MEDIUMBLOB can store up to 16,777,213 bytes, and LONGBLOB can store up to 4,294,967,292 bytes.

SRIRAM
  • 1,888
  • 2
  • 17
  • 17
1

In my case, I tried 20'000 according to @Firze answer (with UTF8 limit) and phpMyAdmin responded with the maximum size; the answer was to decrease or choose BLOB instead.

So, I think, finally, the best is to test yourself according to the version of MySQL you have and the engine used. As MySQL / phpMyAdmin has safeguards.

Meloman
  • 3,558
  • 3
  • 41
  • 51
-12

You can use TEXT type, which is not limited to 64KB.

mvp
  • 111,019
  • 13
  • 122
  • 148