SQL VARCHAR 长度

SQL VARCHAR length

我阅读了很多关于该主题的文章和 SO 问题,但它们所做的只是让我更加困惑。

我想了解 VARCHAR 可以容纳的最长字符串是多少,以及如何定义它。

有些地方说可以创建最大长度为 255 的 VARCHAR(即 VARCHAR(255))——我不明白它是指 255 个字节还是字符。

在其他地方它说 VARCHAR 最多可以容纳 8000 个字节 - 然后取决于语言(如果它是每个字符 1 个字节,例如拉丁语,或更多 - 这决定了最长字符串的长度).

简单来说,VARCHAR(n)中的n代表什么,n的取值范围是多少? 是字节吗?是一个字符的编号吗?在 0-255 之间?在 0-8000 之间?

超长的文本如何保存?它会分成多列吗?

VARCHAR 以每个符号 1 个字节的形式存储字符串(与每个字符可以使用 2 个或更多字节的 nvarchar 相反)。您可以阅读详情here.

A common misconception is to think that CHAR(n) and VARCHAR(n), the n defines the number of characters. But in CHAR(n) and VARCHAR(n) the n defines the string length in bytes (0-8,000). n never defines numbers of characters that can be stored. This is similar to the definition of NCHAR(n) and NVARCHAR(n). The misconception happens because when using single-byte encoding, the storage size of CHAR and VARCHAR is n bytes and the number of characters is also n. However, for multi-byte encoding such as UTF-8, higher Unicode ranges (128-1,114,111) result in one character using two or more bytes. For example, in a column defined as CHAR(10), the Database Engine can store 10 characters that use single-byte encoding (Unicode range 0-127), but less than 10 characters when using multi-byte encoding (Unicode range 128-1,114,111). For more information about Unicode storage and character ranges, see Storage differences between UTF-8 and UTF-16.