Many developers in the database design often do not have much to consider char, varchar type, some are not pay attention to, because the storage price has become more and more cheap, forget the beginning of some basic design theory and principles, this reminds me of the present young people, Big hand a wave of renminbi from his hands slipped away, in fact, I think whether it is a person or do development, details of the grasp directly decided a lot of things. Of course, some people do not understand their differences at all, and choose one. Here I would like to make a simple analysis of them, of course, if there is no place to ask for advice.
1, CHAR. Char is convenient to store the fixed-length data, the index on the Char field is more efficient, such as the definition of char (10), then regardless of whether your stored data reached 10 bytes, to take up 10 bytes of space, insufficient to automatically fill with spaces, Therefore, it is possible to use trim () multiple times when reading.
2, VARCHAR. Store variable-length data , but the storage efficiency is no higher than char. If the possible value of a field is not fixed length, we only know that 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.
3, TEXT. Text stores non-Unicode data of variable length, with a maximum length of 2^31-1 (2,147,483,647) characters.
4, NCHAR, NVARCHAR, NTEXT. These three kinds of names from the first three more than the previous "N". It represents a character stored in a Unicode data type. We know that characters, the English character only need a byte storage is enough, but the number of Chinese characters, need two bytes of storage, English and Chinese characters at the same time prone to confusion, theUnicode character set is to solve the character set this incompatibility problem, all of its characters are expressed in two bytes, That is, the English character is also represented in two bytes. the length of the nchar and nvarchar is between 1 and 4000. Compared to char and varchar, nchar and nvarchar store up to 4,000 characters, whether in English or Chinese characters, while char and varchar can store up to 8,000 English and 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
Their differences are summarized as follows:
CHAR,NCHAR fixed length, high speed, occupy space, need to handle
Varchar,nvarchar,text variable length, small space, slow speed, no need to handle
NCHAR, NVARCHAR, ntext processing Unicode codesHere is a piece of information, source http://wenku.baidu.com/view/eee97bf5f61fb7360b4c652b.html
varcharIn SQL Server, a single byte is used to store the data,
nvarcharis to use Unicode to store the data. Chinese characters stored in SQL Server are saved as two bytes (typically with Unico encoding), and English characters are saved to the database if the type of the field is
varchar, it will only occupy
a single byte, and if the type of the field is
nvarchar, it will occupy
two bytes.
Under normal circumstances, we use varchar
You can also store Chinese characters, but if the operating system is an English operating system and the Chinese font
when support is not comprehensive, in SQL Server storage, text identifier
varcharIt will appear garbled (shown as??). And under normal circumstances, the host will support the Chinese environment, so if you use
varcharTo store data, which is not found in the development phase. In most cases, the
Distribution Department
Of course, using
nvarcharStoring English characters increases the storage space one times. But given the low cost of storage, prioritizing compatibility gives you more benefits.
therefore, when design should try to use nvarchar to store data. Use varchar to store only if you are sure that the field is not saved in Chinese.
, there will be no problem.
But! If the host computer is an English operating system and does not support the Chinese environment, then the problem comes out. All varchar fields are garbled when they are stored in Chinese (shown as??). ). And generally you don't know this because you're using the wrong data type to store the resulting, you'll try to install the Chinese font, try to set the operating system's language environment ... None of this solves the problem, the only way to solve the problem is to take the type of database field
nvarchar(or nchar). More familiar with the project management of friends should know, to the deployment stage to modify the database is a very scary thing.
Another good thing about using nvarchar is that you don't need to consider the difference between the two characters when judging a string.
What is the difference between varchar and nvarchar in SQL Server?