What is the maximum length of mysql VARCHAR?

Source: Internet
Author: User

I used to think that there are two bytes to record the length (a small length can also be recorded in one byte), so this problem was boring at the time, but someone in the group explained it later, it was suddenly discovered that it was not that simple.

In mysql compact format, each record has a byte to indicate the distribution of NULL fields. If any field in the table is allowed to be NULL, the maximum value is 65532. If no field is allowed to be NULL, that byte can be saved, and the maximum value can be defined as 65533. I wonder if this is the reason.

So I read some documents online and did some experiments locally. the maximum length of the original vachar is really variable (determined by whether there are non-empty fields)
I conducted a local experiment in the innodb + latin environment.

Copy codeThe Code is as follows:
-- Success
Drop table if exists test;
Create table test (name varchar (65533) not null) engine = innodb default charset = latin1
-- Too large
Drop table if exists test;


Create table test (name varchar (65533) engine = innodb default charset = latin1
In the second case, when a blank field is allowed, it cannot be added to the length of 65533. The maximum value is 65532. It should be the quote.

Some people have done similar experiments on the internet, refer to http://stackoverflow.com/questions/8295131/best-practise-for-sql-varchar-column-length
Copy codeThe Code is as follows:
Name varchar (100) not null will be 1 byte (length) + up to 100 chars (latin1)
Name varchar (500) not null will be 2 bytes (length) + up to 500 chars (latin1)
Name varchar (65533) not null will be 2 bytes (length) + up to 65533 chars (latin1)
Name varchar (65532) will be 2 bytes (length) + up to 65532 chars (latin1) + 1 null byte

To sum up, although the original mysql vachar field type has a maximum length of 65535, it does not store so much data and can reach a maximum of 65533 (when non-empty fields are not allowed ), when a non-empty field is allowed, it can only be 65532.

The following is a supplement provided by other netizens:

This is not a fixed number. This article briefly describes the restrictions.

Strlen calculates the string length. A Chinese character must be 2 characters long.
Mb_strlen uses its character encoding mode to calculate the character"

Count: calculates the number of elements in the array or the number of attributes in the object.

Copy codeThe Code is as follows:
<? Php
Header ('content-Type: text/html; charset = UTF-8 ');
$ String1 = "Xie chunye"; // defines Chinese Character Variables
$ String2 = "xcy"; // defines English character Variables
// Output them directly to check their length
Echo strlen ($ string1 );
Echo "</br> ";
Echo strlen ($ string2 );
Echo "</br> ";
// Use the php multi-byte Extension function mb_strlen to try it out.
Echo mb_strlen ($ string1, 'utf8 ');
Echo "</br> ";
Echo mb_strlen ($ string2, 'utf8 ');
Echo "</br> ";
?>


The output result is:
9
3
3
3

1. Restrictions
Field restrictions include the following rules when a field is defined:
A) Storage restrictions
The varchar field stores the actual content separately in the clustered index. The content starts with 1 to 2 bytes to indicate the actual length (2 bytes if the length exceeds 255 ), therefore, the maximum length cannot exceed 65535.
B) encoding length limit
If the character type is gbk, each character occupies a maximum of 2 bytes, and the maximum length cannot exceed 32766;
If the character type is utf8, each character occupies up to 3 bytes, and the maximum length cannot exceed 21845.
For forums with a lot of English, using GBK occupies 2 bytes for each character, while using UTF-8 English occupies only one byte.
If the preceding limits are exceeded during definition, the varchar field is forcibly converted to the text type and generates a warning.
C) row length limit
In practice, the length of a varchar is limited by the length defined by a row. MySQL requires that the definition length of a row cannot exceed 65535. If the defined table length exceeds this value, a prompt is displayed.
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs.
2. computing example
Here are two examples to illustrate the calculation of the actual length.
A) if a table has only one varchar type, for example
Create table t4 (c varchar (N) charset = gbk;
The maximum value of N is (65535-1-2)/2 = 32766.
The reason for the decrease of 1 is that the actual Row Storage starts from the second byte ';
The reason for the decrease of 2 is that the two bytes in the varchar header indicate the length;
The reason for Division 2 is that the character encoding is gbk.

B) if a table is defined
Create table t4 (c int, c2 char (30), c3 varchar (N) charset = utf8;
The maximum value of N here is (65535-1-2-4-30*3)/3 = 21812
Subtraction 1 and subtraction 2 are the same as those in the previous example;
The reason for 4 reduction is that int Type c occupies 4 bytes;
The reason for the decrease of 30*3 is that char (30) occupies 90 bytes and the encoding is utf8.
If varchar exceeds the preceding B rule and is forced to be of the text type, each field occupies 11 bytes. Of course, this is no longer a "varchar.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.