In SQL Server, when we set character fields, there are often many data types for us to choose from, such as char nchar varchar nvarchar. Which one should we choose? The differences are described one by one.
Char:Fixed Length, non-Unicode character data, length is n Bytes. The value range of n is 1 to 8,000, and the storage size is n Bytes. The synonym for SQL2003 of char is character.
Varchar:Variable Length, non-Unicode character data. The value range of n is 1 to 8,000. Max indicates that the maximum storage size is 2 ^ 31-1 bytes. The storage size is the actual length of the input data plus two bytes. The length of the input data can be 0 characters. In the SQL-2003, The varchar is charvarying or charactervarying.
Nchar:Unicode character data of a fixed length of n characters. The n value must be between 1 and 4,000 (inclusive ). The storage size is twice n Bytes. The SQL-2003 synonyms of nchar are nationalchar and nationalcharacter.
Nvarchar:Variable-length Unicode character data. N is between 1 and 4,000 (inclusive ). Max indicates the maximum storage size is 2 ^ 31-1 bytes. The storage size is twice the number of characters entered + 2 bytes. The length of the input data can be 0 characters. The SQL-2003 synonyms for nvarchar are nationalcharvarying and nationalcharactervarying.
Char, varchar:Up to 8000 English letters and 4000 Chinese Characters
Nchar, nvarchar:It can store 4000 characters, regardless of English or Chinese Characters
Char, nchar:Fixed Length, fast speed, large space occupation, need to be processed
Varchar, nvarchar:Variable Length, slow speed, small space, no processing required
A fixed length is a fixed length. when the length of the input data does not reach the specified length, it is automatically filled with English spaces to make the length reach the corresponding length.
Variable-length character data is not filled with spaces. The exception is that text is also variable-length.
1. char.It is very convenient for char to store fixed-length data, and the indexing efficiency of char fields is high. For example, if char (10) is defined, no matter whether the data you store reaches 10 bytes, it takes up 10 bytes of space. If the space is insufficient, it is automatically filled with spaces. Therefore, Trim () may be used multiple times during reading ().
2. varchar.Variable-length data is stored, but the storage efficiency is not as high as char. If the possible value of a field is not fixed, we only know that it cannot exceed 10 characters. It is the most cost-effective to define it as varchar (10. The actual length of the varchar type is the actual length of its value plus 1. Why "+ 1? This byte is used to save the actual length. From the perspective of space, it is appropriate to use varchar; from the perspective of efficiency, char is suitable, and the key is to find a trade-off point based on the actual situation.
3. nchar, nvarchar, and ntext.The three names are named n more than the first three ". It indicates that characters of the Unicode data type are stored. We know that only one byte is required for English characters, but there are many Chinese characters and two bytes are required for storage. It is easy to cause confusion when both English and Chinese characters exist, unicode Character Set is generated to solve the incompatibility problem of character sets. All its characters are expressed in two bytes, that is, English characters are also expressed in two bytes. The length of nchar and nvarchar is between 1 and 4000. Compared with char and varchar, nchar and nvarchar can store up to 4000 characters, whether in English or Chinese. char and varchar can store up to 8000 English and 4000 Chinese characters. It can be seen that when using nchar and nvarchar data types, you do not have to worry about whether the entered characters are English or Chinese characters, which is more convenient, but there is some loss in the amount of stored English hours.
Therefore, in general, if it contains Chinese characters, use nchar/nvarchar. If it contains pure English characters and numbers, use char/varchar.