是什么意思nvarchar
?
SQL Server 中的char
、nchar
、varchar
和有什么区别?nvarchar
只是为了澄清......或总结......
nchar
并且nvarchar
可以存储Unicode字符。char
并且不能存储 Unicode字符。varchar
char
并且nchar
是固定长度的,即使您没有用完所有空间,也会为您指定的字符数保留存储空间。varchar
并且nvarchar
是可变长度的,只会用完您存储的字符的空格。它不会像char
或nchar
那样保留存储空间。nchar
并且nvarchar
将占用两倍的存储空间,因此仅在需要Unicode支持时才使用它们可能是明智之举。
nchar 和 char 的运行方式几乎完全相同,nvarchar 和 varchar 也是如此。它们之间的唯一区别是 nchar/nvarchar 存储 Unicode 字符(如果您需要使用扩展字符集则必不可少),而 varchar 不存储。
因为 Unicode 字符需要更多存储空间,所以 nchar/nvarchar 字段占用两倍的空间(例如,在 SQL Server 的早期版本中,nvarchar 字段的最大大小为 4000)。
这个问题是这个问题的副本。
只是为了添加更多内容: nchar - 在数据中添加尾随空格。 nvarchar - 不向数据添加尾随空格。
因此,如果您要通过“nchar”字段过滤数据集,您可能需要使用 RTRIM 删除空格。例如,名为 BRAND 的 nchar(10) 字段存储单词 NIKE。它在单词的右侧添加了 6 个空格。因此,过滤时,表达式应为: RTRIM(Fields!BRAND.Value) = "NIKE"
希望这对那里的人有所帮助,因为我刚刚挣扎了一会儿!
My attempt to summarize and correct the existing answers:
First, char
and nchar
will always use a fixed amount of storage space, even when the string to be stored is smaller than the available space, whereas varchar
and nvarchar
will use only as much storage space as is needed to store that string (plus two bytes of overhead, presumably to store the string length). So remember, "var" means "variable", as in variable space.
The second major point to understand is that, nchar
and nvarchar
store strings using exactly two bytes per character, whereas char
and varchar
use an encoding determined by the collation code page, which will usually be exactly one byte per character (though there are exceptions, see below). By using two bytes per character, a very wide range of characters can be stored, so the basic thing to remember here is that nchar
and nvarchar
tend to be a much better choice when you want internationalization support, which you probably do.
Now for some some finer points.
First, nchar
and nvarchar
columns always store data using UCS-2. This means that exactly two bytes per character will be used, and any Unicode character in the Basic Multilingual Plane (BMP) can be stored by an nchar
or nvarchar
field. However, it is not the case that any Unicode character can be stored. For example, according to Wikipedia, the code points for Egyptian hieroglyphs fall outside of the BMP. There are, therefore, Unicode strings that can be represented in UTF-8 and other true Unicode encodings that cannot be stored in a SQL Server nchar
or nvarchar
field, and strings written in Egyptian hieroglyphs would be among them. Fortunately your users probably don't write in that script, but it's something to keep in mind!
Another confusing but interesting point that other posters have highlighted is that char
and varchar
fields may use two bytes per character for certain characters if the collation code page requires it. (Martin Smith gives an excellent example in which he shows how Chinese_Traditional_Stroke_Order_100_CS_AS_KS_WS exhibits this behavior. Check it out.)
UPDATE: As of SQL Server 2012, there are finally code pages for UTF-16, for example Latin1_General_100_CI_AS_SC, which can truly cover the entire Unicode range.
char
:定长字符数据,最大长度为 8000 个字符。nchar
: 固定长度的 unicode 数据,最大长度为 4000 个字符。Char
= 8 位长度NChar
= 16 位长度nchar[(n)]
(national character)
n
defines the string length and must be a value from 1 through 4,000.n
bytes.nvarchar [(n | max)]
(national character varying.)
n
defines the string length and can be a value from 1 through 4,000.max
indicates that the maximum storage size is 2^31-1 bytes (2 GB).char [(n)]
(character)
non-Unicode
string data.n
defines the string length and must be a value from 1 through 8,000.n
bytes.varchar [(n | max)]
(character varying)
n
defines the string length and can be a value from 1 through 8,000.max
indicates that the maximum storage size is 2^31-1 bytes (2 GB).nchar(10) 是长度为 10 的固定长度 Unicode 字符串。nvarchar(10) 是最大长度为 10 的可变长度 Unicode 字符串。通常,如果所有数据值都是 10 个字符,则使用前者,后者如果长度不同。
nchar 需要比nvarchar 更多的空间。
例如,
一个 nchar(100) 将始终存储 100 个字符,即使您只输入 5,剩余的 95 个字符将用空格填充。在 nvarchar(100) 中存储 5 个字符将节省 5 个字符。
区别在于:
另一个区别是长度。nchar 和 nvarchar 都可以长达 4,000 个字符。char 和 varchar 最长可达 8000 个字符。但是对于 SQL Server,您还可以使用 [n]varchar(max) 最多可以处理 2,147,483,648 个字符。(2 GB,带符号的 4 字节整数。)
nchar 是固定长度的,可以保存 unicode 字符。它每个字符使用两个字节存储。
varchar 是可变长度的,不能保存 unicode 字符。它每个字符使用一个字节存储。
NVARCHAR可以存储 Unicode 字符,每个字符占用 2 个字节。