(1) Definition:
Char: fixed length, storing ANSI characters, insufficient complement English half-width space.
NCHAR: fixed length, storing Unicode characters, insufficient complement to English half-width space
VARCHAR: variable length, storing ANSI characters, automatically changing according to data length.
NVARCHAR: variable length, storing Unicode characters, automatically changing according to data length.
nvarchar (n): A variable-length Unicode character data that contains n characters. The value of n must be between 1 and 4,000. The storage size of bytes is twice times the number of characters entered. The input data character length can be zero.
varchar[(n)]: variable-length, non-Unicode character data with a length of n bytes. n must be a numeric value between 1 and 8,000. Storage size is the actual length of bytes of input data, not n bytes. The input data character length can be zero.
Note: ANSI mainly stores data in single byte, which is generally suitable for English. Our commonly used Chinese characters need to be stored in two bytes, so we will use Unicode data type, otherwise the data read out may be garbled.
(2) Difference:
① from the storage mode, nvarchar is stored by character, while varchar is stored in bytes;
② from storage, varchar saves space because the storage size is the actual length of bytes, while nvarchar is a double-byte storage;
③ in use, if the storage contents are English characters and no Chinese characters and other language symbols, the proposed use of varchar, including the use of Chinese characters nvarchar, because nvarchar is the use of Unicode encoding, that is, a unified character encoding standard, will reduce the chance of garbled;
④ If you do a project that may involve conversion between different languages, it is recommended to use nvarchar.
(3) Advantages and disadvantages:
Nvarchar advantages: When judging the string can not consider the difference between the two characters in English, you can avoid the program garbled problem.
Disadvantage: Storing English characters will increase storage space by one more times. But given the low cost of storage, prioritizing compatibility gives you more benefits, without the efficiency of a varchar high.
(4) Why use nvarchar?
with n prefixes, n denotes Unicode characters, that is, all characters account for two bytes, Nchar,nvarchar
Character, the English characters only need one byte storage is sufficient, but the Chinese character is numerous, requires two bytes of storage, English and Chinese characters are prone to confusion, the Unicode character set is to solve the problem of incompatible character sets, all of its characters are represented by two bytes, That is, the English character is also represented in two bytes.
(5) A brief introduction to Var:
With the Var prefix, it means that the actual storage space is getting longer, Varchar,nvarchar
The so-called length is fixed, when the input length of the data does not reach the specified length will be automatically filled with English space after it, so that the length of the corresponding length, and the variable length character data will not be filled with spaces, the exception is that the text storage is also variable length.
(6) How do I use these types?
If you are sure of the length of the stored data and do not pack in Chinese, you can choose the char type.
If you are sure of the stored data length, but may include Chinese, you can choose the nchar type.
If you are unsure of the length of the stored data, store only English, the best use of the number varchar
If you are unsure of the length of the stored data, it is possible to have Chinese, you can choose the nvarchar type, and in SQL Server2005 is the more commonly used character data type.
The difference between char, varchar, nchar, nvarchar in SQL Server