1. Char type:
Occupies 1 bytes for English (ASCII) characters, for a Chinese character 2 bytes, char storage fixed-length data is convenient, Char field index is very efficient, such as the definition of char (10), so whether you store data to reach 10 bytes, it takes up 10 bytes of space. Because it is a fixed length, so the speed is high efficiency. For example, if you define char (10), you take up 10 bytes of space, regardless of whether the data you store reaches 10 bytes. Because it is a fixed length, so the speed is high efficiency.
2. VarChar storage variable length data
If the possible value of a field is an unfixed length, we only know that it cannot exceed 10 characters, and it is most cost-effective to define it as VARCHAR (10). The actual length of the varchar type is the actual length of its value +1. Why "+1"? This byte is used to save how much length is actually used.
Varchar type: The type of Varchar is not filled with spaces, and char fills up, such as Varchar (100), but its value is only "Qian", so the value it saves in the database is "Qian", and char is not the same, such as char (100), its The value is "Qian" and in fact it is "Qian" in the database (Qian a total of 96 spaces after it is filled to 100 bytes).
Note: Because char is fixed in length, it will be much faster than varchar! But the program to deal with a bit of trouble, to use the function such as trim to remove both sides of the space!
3. What is the nchar type and nvarchar type?
In order to convert to a variety of other characters, such as Chinese, phonetic symbols, and so on, each English (ASCII) character occupies 2 bytes, also occupies two bytes for a Chinese character, all characters occupy 2 bytes.
VARCHAR (n): variable-length character data type, stored for a maximum length of 8,000 characters
nvarchar (n): variable-length Unicode data with a maximum length of 4,000 characters. The storage size of the byte is twice times the number of characters entered, which means that it is double-byte to store the data. If a single byte is stored in the data, it also occupies storage space in two bytes.
VarChar is generally applicable in English and numerals, nvarchar is suitable for Chinese and other characters, where n represents a Unicode constant, which resolves the problem of conversion between multilingual character sets.
The following are supplementary:
For fields with generic string types in your program, there are four types of char, varchar, nchar, and nvarchar in SQL Server, so what's the difference between these four types?
1. Fixed-length or variable-length
The so-called fixed-length is long, when the length of the data to be saved will automatically fill in the back of the English space, so that the length of the corresponding length; there is the Var prefix, indicating that the actual storage space is dynamic change, such as Varchar,nvarchar variable length character data will not be filled with space.
2.Unicode or non-Unicode
In a database, English characters require only one byte of storage, but Chinese characters and many other non-English characters require two bytes of storage. If the English and Chinese characters exist simultaneously, due to the number of space occupied, it is easy to create confusion, resulting in the reading of the string is garbled. The Unicode character set is created to resolve incompatible problems with the character set, all of which are represented in two bytes, that is, English characters are also represented in two bytes. The prefix n represents Unicode characters, such as Nchar,nvarchar, which use the Unicode character set.
3. Maximum capacity for storage of several data types
Char,varchar up to 8,000 English, 4,000 Chinese characters
Nchar,nvarchar can store up to 4,000 characters, whether in English or Chinese