怪我咯2017-04-17 11:13:21
When the character set is UTF-8:
ver < 4.1
: VARCHAR is stored in units of bytes, so assuming all are common Chinese characters (UTF-8 3-byte encoding length), VARCHAR(255) can store a total of about 85 Chinese characters; ver >= 4.1
: VARCHAR is stored in units of characters . Assuming that the input is still commonly used Chinese characters, VARCHAR(255) can store 255 Chinese characters. In addition, as far as I know, MySQL's support for UTF-8 is only limited to 1~3 byte encoding length (Unicode: 0x0000~0xFFFF), which can meet most needs, but rare characters cannot.
高洛峰2017-04-17 11:13:21
Well it’s hard to choose the best answer to this question...I’m here to add to the confusion...
The three replies above are all correct but not comprehensive...This mainly depends on the database character set...
If it is latin1... calculate the byte length... greatghoul's answer is correct...
If it is utf-8... calculate the character length... joyeu's answer is correct...
As for the difference between 4.1 and the critical point mentioned in Theo's answer... This really has nothing to do with the version...
It’s only because charset support was introduced after 4.1!
If you choose latin1, the byte length will be calculated regardless of the version!
In order to avoid this answer seeming to be completely copied from the one above... I will mention something else...
Actually, there is another pitfall in this question...that is, the things to be saved are Chinese characters...
If you want to save utf-8 characters, the latin1 character set can save 63 - 127 characters...
But if you want to save Chinese characters...then the maximum number is probably only 85 to be exact...
Because before I coded these characters, I tried every method to test but could not find a Chinese character that takes up four bytes...
怾gi
黁nun
兺bun
乯olo
乭daori
旕eosi
銰ngai
虄sari
哛popuni
硛ceoke
縇seone
穒keweoke
Is this set of characters uncommon enough? Each character only takes up three bytes...
As for the two-byte Chinese characters...I can't find it either...what greathoul said 〇
is wrong... 〇
occupies three bytes...
Let me make a concluding statement...
VARCHAR(255) in MySQL can store 1 - 255 Chinese characters in UTF-8 format...
The latin1 character set can store up to 85 Chinese characters in UTF-8 format...
The utf-8 character set can store up to 255 Chinese characters in UTF-8 format...
If anyone can find a Chinese character that takes up two bytes or four bytes, then my answer is wrong...
If you can’t find it, you can just trust it...
Above...
大家讲道理2017-04-17 11:13:21
This depends on what characters are stored. UTF-8 has become longer. Some Chinese characters take up three, and some take up four.
You can refer to this article:
http://blog.csdn.net/chummyhe89/artic...
Occupies 2 bytes: 〇
Occupies 3 bytes: Basically equivalent to GBK, containing more than 21,000 Chinese characters
Occupies 4 bytes: Chinese characters in the super large character set of China, Japan and Korea , there are more than 50,000
A utf8 number occupies 1 byte
A utf8 English letter occupies 1 byte
迷茫2017-04-17 11:13:21
It should be 255 under MySQL5. For the latin1 character set, Chinese characters are garbled when saved, and they are displayed as English question marks when selected?
The actual measurement of Varchar(2) under MySQL5 is as follows:
CREATE TABLE `test`.`tb1`( `p1` VARCHAR(2) ) CHECKSUM=1 COMMENT='' DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC CHARSET=utf8 ;
INSERT INTO `test`.`tb1`(`p1`) VALUES ( '你好');
INSERT INTO `test`.`tb1`(`p1`) VALUES ( '你很坏');
INSERT INTO `test`.`tb1`(`p1`) VALUES ( '1234');
SELECT * FROM `test`.`tb1` LIMIT 0, 50;
得到:
你好
你很
12
CREATE TABLE `test`.`tb2`( `p1` VARCHAR(2) ) CHECKSUM=1 COMMENT='' DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC CHARSET=latin1 ;
INSERT INTO `test`.`tb2`(`p1`) VALUES ( '1234');
INSERT INTO `test`.`tb2`(`p1`) VALUES ( '你好');
INSERT INTO `test`.`tb2`(`p1`) VALUES ( '你12');
SELECT * FROM `test`.`tb2` LIMIT 0, 50;
得到:
12
??
?1
伊谢尔伦2017-04-17 11:13:21
I feel that both of the above answers are wrong. The number in the brackets of char or varchar in MySQL refers to the maximum number of characters , not the number of bytes. All 255 means that a maximum of 255 Chinese characters are allowed. If there are letters, the total number of letters and Chinese characters cannot exceed 255.