For the string field in the program, SQL Server has char, varchar, nchar, nvarchar four types to correspond (temporarily regardless of text and ntext), open the database, for these four types are often more ambiguous, Here's a comparison.
- Fixed or variable length
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, the var prefix, indicating that the actual storage space is longer, such as Varchar,nvarchar variable-length character data is not padded with spaces, except that the text is stored in a variable length.
- Unicode or non-Unicode
In a database, English characters require only one byte to store, but Chinese characters and many other non-English characters require two bytes of storage. If the English and Chinese characters exist simultaneously, because of the different occupied space, it is easy to cause confusion, resulting in the reading of the string is garbled. The Unicode character set is created to address the incompatibility of the character set, and all of its characters are represented in two bytes, meaning that the English character is also represented in two bytes. The prefix n represents Unicode characters, such as Nchar,nvarchar, which use the Unicode character set.
- Look at the field capacity based on the two points above
Char,varchar |
Up to 8,000 English, 4,000 kanji |
Nchar,nvarchar |
Can store 4,000 characters, whether English or Chinese |
- Use (personal preference)
If the amount of data is very large and can be 100% to determine the length and save only ANSI characters, Char
Can determine the length is not necessarily ANSI characters or, then use nchar;
For oversized data, such as article content, use nText
Other general-purpose nvarchar
Character comparison of char, varchar, nchar and nvarchar
- CHAR
Char is convenient for storing fixed-length data, and the index on achar field is highly efficient, such as defining char(10), which takes up 10 bytes of space regardless of whether the data you store is 10 bytes.
- VARCHAR
Store variable length data, but the storage efficiency is not high, if the value of a field may be not fixed length, we only know it cannot exceed 10 characters, it is the most advantageous to define it as VARCHAR(10). The actual length of the VARCHAR type is +1 of the actual length of its value. Why "+1"? This byte is used to hold the length that is actually used.
From the space consideration, with the varchar suitable, from the efficiency consideration, uses the char to be suitable, the key is to find the tradeoff point according to the actual situation.
- TEXT
Text stores non-Unicode data of variable length, with a maximum length of 2^31-1 (2,147,483,647) characters.
- NCHAR, NVARCHAR, NTEXT
These three kinds of names from the first three more than the previous "N". Compared to charandvarchar ,ncharandnvarchar store up to 4,000 characters, whether in English or Chinese characters, while Char, varchar can store up to 8,000 English, 4,000 Chinese characters. It can be seen that the use of nchar,nvarchar data types without worrying about the input characters are English or Chinese characters, more convenient, but in the storage of English number of some losses.
So generally, if it contains Chinese characters, use Nchar/nvarchar, if pure English and numbers, with Char/varchar
The difference between char, varchar, nchar, nvarchar