int, bigint, smallint, and tinyint
An exact numeric data type that uses integer data.
bigint
Integer data (all numbers) from -2^63 (-9223372036854775808) to 2^63-1 (9223372036854775807).
The storage size is 8 bytes.
Int
Integer data (all numbers) from -2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647). The storage size is 4 bytes. The SQL-92 synonymous word for int is integer.
smallint
Integer data from -2^15 (-32,768) to 2^15-1 (32,767).
The storage size is 2 bytes.
tinyint
Integer data from 0 to 255. The storage size is 1 bytes.
Gaze
Support for bigint data types where integer values are supported. However, bigint is used in some special cases when the integer value exceeds the range supported by the INT data type, the bigint can be used.
In SQL Server, the int data type is the basic integer data type.
In the data type precedence table, bigint is located between smallmoney and Int.
Only when the parameter expression is a bigint data type does the function return bigint. SQL Server does not proactively promote other integer data types (tinyint, smallint, and int) to bigint.
Descriptions of char, varchar, nchar, nvarchar types:
First, the basic difference:
1, char, varchar
CHAR (n): fixed-length data type, n is the maximum byte length that can be stored (value range 1~8000), when the number of characters less than N, the end of the space is insufficient.
When the number of characters exceeds N, the characters after the database phase n are truncated.
VARCHAR (n): variable-length data type, n is the maximum byte length that can be stored (value range 1~8000). When the storage character occupies less than n, the actual use is the same, and when it exceeds N, it is truncated.
Why does this say n is to limit the bytes stored?
The reason for this is when a char or varchar is used to store the data: the character is due to a character, which takes up one byte. In characters, a Chinese character is two bytes in normal circumstances. The characters to be deposited are mixed in Chinese and English. Calculated as the actual bytes used. such as the ' Chinese ABC ' storage occupies bytes: 2*2+3:. Here's a sample example:
DECLARE @var1 varchar (6), @var2 varchar (5);
Set @var1 = ' Chinese ab ';
Set @var2 = ' Chinese ab ';
Select @var1, @var2;
Running results such as the following:
| Unknown column 1 | Unknown column 2 |
---------------------------------------
| English AB | Chinese A |
The reason is that "Chinese AB" uses the varchar type to store the bytes as: 2*2+2=6. The second varchar (5) limits the maximum to 5 bytes so it will break out of the partial truncation, both ' B ' lost.
2, Ncahr, nvarchar
NCHAR (n): fixed length data type. The stored characters are Unicode encoded type, each character occupies two bytes, n is the maximum number of characters that can be stored (the range 1~4000 "Think about what is the maximum value of 4000?"). "), when the number of characters is less than N, the trailing space is insufficient. When the number of characters is more than N. The character after the database phase N.
nvarchar (n): variable-length data type, the storage character is a Unicode encoding type, each character occupies two bytes, n is the maximum number of characters that can be stored (the range of Values 1~4000), when the storage character occupies less than n, the actual use of the quasi-when more than N, truncated.
The same example illustrates that n in nvarchar (n) refers to the number of characters that are qualified in parentheses.
declare @var1 nvarchar (4), @var2 nvarchar (3);
Set @var1 = ' Chinese ab ';
Set @var2 = ' Chinese ab ';
Select @var1, @var2;
Running results such as the following:
| Unknown column 1 | Unknown column 2 |
---------------------------------------
| English AB | Chinese A |
Second, the use of:
"This paragraph is excerpted from http://www.cnblogs.com/ebaidu/archive/2007/08/14/854778.html for learning only"
Very many developers in the database design often do not have much to consider char, varchar type, some are not aware of, because the storage price has become more and more inexpensive, forget the most basic design theory and principles, this reminds me of today's young people, A big hand and a wave of renminbi slipped away from his hand. In fact, I think whether it is a person or not. Do the development, the details of the grasp directly determine a lot of things. Of course, the other part of the people do not understand their differences at all, and choose one. I want to make a simple analysis of them here. Of course, if there is an incorrect place, we would like to advise you.
1, CHAR. Char is convenient for storing fixed-length data, and the index on a char field is highly efficient, for example, char (10), regardless of whether the data you store has reached 10 bytes. To take up 10 bytes of space, not enough to take the initiative to fill with space, so in the reading time may have to use the trim ().
2, VARCHAR.
Store variable-length data. But storage efficiency is no higher than char. Assuming that a field may have a value that is not fixed, we only know that it cannot exceed 10 characters, and it is 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.
be considered in space. To be suitable for using varchar; Use char to fit. The key is to find a trade-off point based on reality.
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 the characters. English characters need only a byte storage is enough, but the number of Chinese characters, need two bytes of storage, English and Chinese characters at the same time when there is easy to create confusion. The Unicode character set is created to address incompatibilities such as character sets, all of which are represented by two bytes, meaning that English characters are also represented in two bytes. The length of the nchar and nvarchar is between 1 and 4000.
Compare with char, 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. Be able to see the use of nchar, nvarchar data types do not worry about the input characters are English or Chinese characters, more convenient, but in the storage of English number of some losses.
Add: char (n) reads the length of the string and can read it one at a time. varchar (n), like the string processing in C, ends with ' + ' at the end. So it can't be read out at once. One character is required to read the talent and know where to end.
So the char type reads faster than the varchar type.
So generally speaking. Suppose to contain Chinese characters, using Nchar/nvarchar, assuming plain English and numbers, with Char/varchar
I summed up their differences as follows:
CHAR, NCHAR fixed length, fast, occupy large space. Need to handle
VARCHAR. NVARCHAR. TEXT variable length, small space, slow speed, no processing
NCHAR, NVARCHAR, ntext processing Unicode codes
"This paragraph is excerpted from http://wenku.baidu.com/view/eee97bf5f61fb7360b4c652b.html for learning only"
varchar uses a single byte to store data in SQL Server, and nvarchar is using Unicode to store data. Chinese characters stored in SQL Server are saved as two bytes (typically Unico encoded). English characters are saved to the database, assuming that the field is of type varchar, it will only occupy one byte. Instead, assume that the field is of type nvarchar. It takes two bytes.
Under normal circumstances, we can also use varchar to store Chinese characters, but assuming that the operating system is an English-language operating system and the support of English fonts is not comprehensive, in the SQL Server store, the text character varchar will be garbled (shown as?).
?). and under normal circumstances. The host will support the Chinese environment, so it is assumed that varchar is used to store the data. It is not found in the development phase. In most cases, there will be no problem at the time of deployment.
But!
If the host is an English-language 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 under normal circumstances you will not know that this is because you are using the wrong data type to store the resulting, you try to install Chinese fonts, try to set the operating system of the language environment ... None of this solves the problem, and the only solution to this problem is to nvarchar (or nchar) the type of database field. To project management more familiar with the friend should know, to the deployment stage to change the database is a very scary thing.
Another advantage of using nvarchar is that you can infer a string without having to consider the difference between Chinese and English characters.
Of course, using nvarchar to store English characters will increase storage space by one more times. However, the cost of storage is already very low. Prioritizing compatibility gives you a lot of other benefits.
Therefore, when design should try to use nvarchar to store data. Only when you make sure that the field is not saved in Chinese will it be stored with varchar.
Add: varchar (n) and nvarchar (n) can handle Chinese, but there are some details that need to be noted: when encountering mixed English and Chinese conditions. The varchar type reads each byte to infer whether it is an English character or a wait and the next byte resolves to a Chinese character, while nvarchar does not, and resolves to a Unicode character two bytes at a time. Speed yourself to realize it.
--------------------------------------------------------------------------------------------------------------- ---------
Summarize:
1, varchar (n) in parentheses, n is the maximum number of bytes that can be stored, and nvarchar (n) is limited to the maximum number of characters that can be stored.
2. VarChar and Nvarcahr use case: varchar is used in all English characters. All Chinese is the use of nvarchar, English and Chinese mixed when the recommended use of nvarchar.
3. Char and varchar use case: use char when reading efficiency is important (note that the trailing spaces are removed during use). Use varchar when using space.
Database basic often uses type resolution