Differences between char, varchar, text, nchar, nvarchar, and ntext in SQL
I have encountered this problem before, but I don't know the system. I happened to ask this question a few days ago. I checked the information and summarized it.
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 you store
Whether the data reaches 10 bytes occupies 10 bytes of space.
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
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.
Degree + 1. Why "+ 1? This byte is used to save the actual length.
Before modification (char)
After modification (varchar)
The above two figures are posted by netizens in the forum. We can see that the modified pw_user "Weight Loss" effect is quite obvious.
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 Unicode data is stored.
Type. We know that only one byte is required for English characters, but many Chinese characters require two bytes for English characters.
The Unicode Character Set is generated to solve the incompatibility problem of character sets.
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. And
Compared with Char and varchar, nchar and nvarchar can store a maximum of 4000 characters, whether in English or Chinese characters, while char and varchar
A maximum of 8000 English and 4000 Chinese characters can be stored. We can see that when using the nchar and nvarchar data types, you do not have to worry that the entered characters are English characters.
It is more convenient to use Chinese characters, 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.
4. Note: SQL statements must be separated by spaces.
5 if the parameter does not need to be floating
<Div style = "clear: both"> </div>
6. Notes on the ASPX page are very problematic.