varchar(limit) 是如何工作的?

How does the varchar(limit) work?

我一直在使用 MySQL 并且非常模糊地理解 VARCHAR(This Number Here) 部分。该数字是该列可以存储的字符总数吗?

例如,假设我有一个 VARCHAR(400) latin1_general_ci,400 是指字符串的 400 字节限制,还是字符串可以有 400 个字符?我可以在该列变量中存储多大的字符串?

这是字段的最大字符串长度(参见 here)(不是字节):

The CHAR and VARCHAR types are declared with a length that indicates the maximum number of characters you want to store. For example, CHAR(30) can hold up to 30 characters.

无论编码如何,这都将允许 30 个字符。

Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535. 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.

In contrast to CHAR, VARCHAR values are stored as a 1-byte or 2-byte length prefix plus data. The length prefix indicates the number of bytes in the value. A column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.