This article introduces the differences between charncharvarcharnvarchartextntext of character data fields in SQL.
This article introduces the differences between char, nchar, varchar, nvarchar, text, and ntext of character data fields in SQL.
When you enter a certain field, you only need to enter seven numbers for the item number field. When you select a data type, you do not know whether to select varchar, char, or nvarchar, therefore, I have collected some differences about these data types from the Internet and posted them here. I hope this will also help you with this confusion.
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. TEXT. Text stores variable-length non-Unicode data. The maximum length is 2 ^ 31-1 (2,147,483,647) characters.
4. 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 English letters and numbers, use char/varchar.
CHAR, NCHAR fixed length, fast speed, large space, need to be processed
VARCHAR, NVARCHAR, and TEXT cannot be long, the space is small, and the speed is slow. No processing is required.
NCHAR, NVARCHAR, and NTEXT process Unicode codes
Char (n) |
Fixed Length |
High indexing efficiency using trim in programs to remove unnecessary blank space |
NIt must be a value between 1 and 8,000. The storage size isNBytes |
Varchar (n) |
Variable Length |
Efficiency not as high as char flexibility |
NIt must be a number between 1 and 8,000. The storage size is the actual length of the input data bytes, insteadNBytes |
Text (n) |
Variable Length |
Non-Unicode data |
|
Nchar (n) |
Fixed Length |
Unicode data type processing (all characters are expressed in two bytes) |
NMust be between 1 and 4,000. The storage size isNDouble the byte |
Nvarchar (n) |
Variable Length |
Unicode data type processing (all characters are expressed in two bytes) |
NMust be between 1 and 4,000. The storage size of bytes is twice the number of input characters. The entered data character length can be zero. |
Ntext (n) |
Variable Length |
Unicode data type processing (all characters are expressed in two bytes) |