SQL Server 中不同字符串类型的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4187677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between different string types in SQL Server?
提问by bevacqua
What is the difference between char
, nchar
, ntext
, nvarchar
, text
and varchar
in SQL?
是什么区别char
,nchar
,ntext
,nvarchar
,text
和varchar
在SQL?
Is there really an application case for each of these types, or are some of them just deprecated?
这些类型中的每一种是否真的有一个应用案例,或者其中一些只是被弃用?
回答by Remus Rusanu
text
and ntext
are deprecated, so lets omit them for a moment. For what is left, there are 3 dimensions:
text
并且ntext
已被弃用,所以让我们暂时忽略它们。对于剩下的,有 3 个维度:
- Unicode (UCS-2) vs. non-unicode:
N
in front of the name denotes Unicode - Fixed length vs. variable length:
var
denotes variable, otherwise fixed - In-row vs. BLOB:
(max)
as length denotes a BLOB, otherwise is an in-row value
- Unicode (UCS-2) vs. non-unicode:
N
名字前面表示Unicode - 固定长度 vs. 可变长度:
var
表示可变,否则固定 - In-row vs. BLOB:
(max)
因为长度表示一个BLOB,否则是一个行内值
So with this, you can read any type's meaning:
所以有了这个,你可以阅读任何类型的含义:
CHAR(10)
: is an in-row fixed length non-Unicode of size 10NVARCHAR(256)
: is an in-row variable length Unicode of size up-to 256VARCHAR(MAX)
: is a BLOB variable length non-Unicode
CHAR(10)
: 是大小为 10 的行内固定长度非 UnicodeNVARCHAR(256)
: 是大小不超过 256 的行内可变长度 UnicodeVARCHAR(MAX)
: 是一个 BLOB 可变长度的非 Unicode
The deprecated types text
and ntext
correspond to the new types varchar(max)
and nvarchar(max)
respectively.
不推荐使用的类型text
和ntext
对应新的类型varchar(max)
,并nvarchar(max)
分别。
When you go to details, the meaning of in-row
vs. BLOB
blurs for small lengths as the engine mayoptimize the storage and pull a BLOB in-row or push an in-row value into the 'small BLOB' allocation unit, but this is just an implementation detail. See Table and Index Organization.
当你进入细节时,in-row
vs.BLOB
模糊对于小长度的含义,因为引擎可能会优化存储并在行中拉出 BLOB 或将行内值推入“小 BLOB”分配单元,但这只是一个实施细节。请参阅表和索引组织。
From a programming point of view, all types: CHAR
, VARCHAR
, NCHAR
, NVARCHAR
, VARCHAR(MAX)
and NVARCHAR(MAX)
, support an uniform string API: String Functions. The old, deprecated, types TEXT
and NTEXT
do notsupport this API, they have a separate, deperated, TEXT API to manipulate. You should not use the deprecated types.
从编程的角度来看,所有类型:CHAR
,VARCHAR
,NCHAR
,NVARCHAR
,VARCHAR(MAX)
和NVARCHAR(MAX)
,支持统一的串API:字符串函数。旧的,过时,类型TEXT
和NTEXT
做不支持这个API,他们有一个独立的,deperated,TEXT API来处理。您不应使用已弃用的类型。
BLOB types support efficient in-place updates by using the UPDATE table SET column.WRITE(@value, @offset)
syntax.
BLOB 类型通过使用UPDATE table SET column.WRITE(@value, @offset)
语法支持高效的就地更新。
The difference between fixed-length and variable length types vanishes when row-compression on a table. With row-compression enabled, fixed lenght types and variable length are stored in the same format and trailing spaces are not stored on disk, see Row Compression Implementation. Note that page-compression implies row-compression.
当表上的行压缩时,固定长度和可变长度类型之间的差异消失了。启用行压缩后,固定长度类型和可变长度以相同格式存储,尾随空格不存储在磁盘上,请参阅行压缩实现。请注意,页面压缩意味着行压缩。
回答by Sachin Shanbhag
- 'n' represents support for unicode characters.
- char- specifies string with fixed length storage. Space allocated with or without data present.
- varchar- Varying length storage. Space is allocated as much as length of data in column.
- text- To store huge data. The space allocated is 16 bytes for column storage.
- ' n' 表示支持 unicode 字符。
- char- 指定具有固定长度存储的字符串。存在或不存在数据的情况下分配的空间。
- varchar- 变长存储。分配的空间与列中的数据长度一样多。
- text- 存储大量数据。分配的空间为 16 字节用于列存储。
Additionally - text and ntext have been deprecated for varchar(max) and nvarchar(max)
此外 - text 和 ntext 已被弃用用于 varchar(max) 和 nvarchar(max)
回答by Gerald Davis
The n prefix simply means Unicode. They "n" types work similarly to the plain versions except they work with Unicode text.
n 前缀仅表示 Unicode。它们的“n”类型与普通版本的工作方式类似,只是它们使用 Unicode 文本。
char is a fixed length field. Thus char(10) filled with "Yes" will still take 10 bytes of storage.
char 是一个固定长度的字段。因此,用“Yes”填充的 char(10) 仍将占用 10 个字节的存储空间。
varchar is a variable length field. char(10) filled with "Yes" will take 5 bytes of storage (there is a 2 byte overhead for using var data types).
varchar 是可变长度字段。用“Yes”填充的 char(10) 将占用 5 个字节的存储空间(使用 var 数据类型有 2 个字节的开销)。
char(n) holding string of length x. Storage = n bytes. varchar(n) holding string of length x. Storage = x+2 bytes.
char(n) 保存长度为 x 的字符串。存储 = n 字节。varchar(n) 保存长度为 x 的字符串。存储 = x+2 字节。
vchar and nvarchar are similar except it is 2 bytes per character.
vchar 和 nvarchar 相似,只是每个字符是 2 个字节。
Generally speaking you should only use char & char (over varchar & nvarchar) when working with fixed or semi-fixed strings. A good example would be a product_code or user_type which is always n characters long.
一般来说,在处理固定或半固定字符串时,您应该只使用 char 和 char(超过 varchar 和 nvarchar)。一个很好的例子是 product_code 或 user_type,它总是 n 个字符长。
You shouldn't use text (or ntext) as it has been deprecated. varchar(max) & nvarchar(max) provides the same functionality.
您不应使用 text(或 ntext),因为它已被弃用。varchar(max) & nvarchar(max) 提供相同的功能。
回答by bdukes
text
and ntext
are deprecatedin favor of varchar(max)
and nvarchar(max)
text
而ntext
被弃用赞成varchar(max)
和nvarchar(max)
回答by JNK
N
prefix indicates unicode support and takes up twice the bytes per characterof non-unicode.
N
prefix 表示支持 unicode ,每个字符占用的字节数是非 unicode 的两倍。
Varchar
is variable length. You use an extra 2 bytes per fieldto store the length.
Varchar
是可变长度。每个字段使用额外的 2 个字节来存储长度。
Char
is fixed length. If you know how long your data will be, use char
as you will save bytes!
Char
是固定长度。如果您知道数据的长度,请使用char
as 您将节省字节!
Text
is mostly deprecated in my experience.
Text
根据我的经验,大多已被弃用。
Be wary of using Varchar(max)
and NVarchar(max)
as these fields cannot be indexed.
小心使用Varchar(max)
,NVarchar(max)
因为这些字段不能被索引。
回答by Mike M.
Text is deprecated.
文本已弃用。
Char is a set value. When you say char(10), you are reserving 10 characters for every single row, whether they are used or not. Use this for something that shouldn't change lengths (For example, Zip Code or SSN)
Char 是一个设定值。当你说 char(10) 时,你为每一行保留了 10 个字符,不管它们是否被使用。将此用于不应更改长度的内容(例如,邮政编码或 SSN)
varchar is variable. When you say varchar(10), 2 bytes is set aside to store the size of the data, as well as the actual data (which might be only say, four bytes).
varchar 是可变的。当您说 varchar(10) 时,会留出 2 个字节来存储数据的大小以及实际数据(可能只是四个字节)。
The N represents uni-code. Twice the space.
N代表单码。两倍的空间。
回答by TomTom
n-prefix: unicode. var*: variable length, the rest is fixed length.
n 前缀:unicode。var*:变长,其余为定长。
All data types are properly and nicely... documented.
所有数据类型都正确且很好地......记录在案。
Like here:
像这儿:
http://msdn.microsoft.com/en-us/library/ms187752.aspx
http://msdn.microsoft.com/en-us/library/ms187752.aspx
Is there really an application case for each of these types, or are some of them just deprecated?
这些类型中的每一种是否真的有一个应用案例,或者其中一些只是被弃用?
No, there is a good case for ANY of them.
不,他们中的任何一个都有很好的案例。
回答by Martijn
Text is meant for very large amounts of text, and is in general not meant to be searchable (but can be in some circumstances. It will be slow anyway).
文本适用于大量文本,通常不可搜索(但在某些情况下可以。无论如何它都会很慢)。
The char/nchar datatypes are of fixed lenghts, and are padded if entered stuff is shorter, as opposed to the varchar/nvarchar types, which are variable length.
char/nchar 数据类型是固定长度的,如果输入的内容较短,则填充,与可变长度的 varchar/nvarchar 类型相反。
The n types have unicode support, where the non-n types don't.
n 类型有 unicode 支持,非 n 类型没有。
回答by Umair A.
I only know between "char" and "varchar".
我只知道“char”和“varchar”之间。
char: it can allocate memory of specified size whether or not it is filled
char:无论是否填满,都可以分配指定大小的内存
varchar: it will allocate memory based on the number of characters in it but it should have some size called maximum size.
varchar:它将根据其中的字符数分配内存,但它应该有一些称为最大大小的大小。