且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL Server 2008 r2数据类型之间的差异?

更新时间:2023-02-06 13:14:55

这可能对你有所帮助 -

了解数据类型差异 [ ^ ]



数据类型(Transact-SQL) [ ^ ]



如果安装,你可以使用sql server 2008 R2的索引(help->索引)。
This might help you-
Understanding Data Type Differences[^]

Data Types (Transact-SQL)[^]

further you can use index(help->index) of sql server 2008 R2 if installed.


字符串和二进制类型

BINARY和VARBINARY之间的差异

BINARY1

长度为n字节的定长二进制数据,其中n是1到8,000之间的值。存储大小为n个字节。



VARBINARY

可变长度的二进制数据。 n可以是1到8,000之间的值。



Max表示最大存储大小为2 ^ 31-1个字节。



存储大小是输入数据的实际长度+ 2个字节。



输入的数据长度可以是0个字节。



varbinary的ANSI SQL同义词是二元变量。



结论:

当列数据条目的大小一致时使用二进制文件。



当列数据条目的大小变化很大时使用varbinary 。



当列数据条目超过8,000字节时使用varbinary(max)。



CHAR和VARCHAR之间的差异

CHAR

固定长度。因此,字段中的任何剩余空间都用空格填充。

非Unicode字符数据,长度为n个字节,n必须是1到8,000之间的值。存储大小为n个字节

char的SQL-2003同义词是字符。



VARCHAR

可变长度的非Unicode字符数据。 n可以是1到8,000之间的值。 max表示最大存储大小为2 ^ 31-1个字节。

存储大小是输入数据的实际长度+ 2个字节。

输入的数据长度可以是0个字符。

varchar的SQL-2003同义词是char变化或字符变化。

例如,如果设置VARCHAR(100)数据类型=''Jen'',那么它需要3个字节(对于J,E和N)加上2个字节,或者总共5个字节。



结论:

使用字符,当使用列中的固定长度数据时数据条目是一致的,如电话号码

使用Varchar在列&中使用可变长度数据时数据条目差异很大,如地址



VARCHAR / VARCHAR(MAX)和NVARCHAR / NVARCHAR(MAX)之间的差异

Varchar / Varchar(MAX)

Varchar(8000)最多可存储8000个字符。 Varchar(max)最多可存储2 147 483 647个字符或2GB。

可变长度非Unicode字符数据。

存储大小是输入数据的实际长度+ 2个字节

SQL-2003同义词for varchar是char变化或字符变化。



NVarchar / NVarchar(MAX)

NVarchar(4000)最多可存储4000个字符。 Varchar(max)最多可存储1 073 741 823个字符或1GB。

可变长度Unicode字符数据。

存储大小(以字节为单位)是输入字符数的两倍+ 2字节

SQL -2003 nvarchar的同义词是国家字符变化和国家字符变化。



NCHAR和NVARCHAR之间的差异

NCHAR


固定长度。因此,字段中的任何剩余空间都用空格填充。

长度为n个字节的Unicode字符数据,n必须是1到4,000之间的值。存储大小为n个字节

nchar的ISO同义词是国家字符和国家字符。



NVARCHAR

可变长度,非Unicode字符数据。 n可以是1到4,000之间的值。 max表示最大存储大小为2 ^ 31-1个字节(2GB)。

存储大小以字节为单位,是实际输入数据长度的两倍+ 2个字节

nvarchar的ISO同义词是国家字符变化和国家字符变化。 />


结论:

当列数据条目的大小可能类似时,请使用nchar。 />
当列数据条目的大小可能会有很大差异时使用nvarchar。



NTEXT,TEXT之间的差异AND IMAGE

NTEXT


可变长度的Unicode数据,最大字符串长度为2 ^ 30 - 1(1,073,741,823)。

存储大小(以字节为单位)是输入字符串长度的两倍。

ntext的ISO同义词是国家文本。



TEXT

服务器代码页中的可变长度非Unicode数据,最大字符串长度为2 ^ 31-1(2,147,483,647)。 />
当服务器代码页使用双字节字符时s,存储仍为2,147,483,647字节。

根据字符串的不同,存储大小可能小于2,147,483,647字节。



IMAGE


从0到2​​ ^ 31-1(2,147,483,647)字节的可变长度二进制数据。



NUMERIC DATATYPES



十进制[(p [,s])]和数字之间的差异[( p [,s])]



固定精度和刻度数。

使用最大精度时,有效值为from - 10 ^ 38 +1到10 ^ 38 - 1.

十进制的ISO同义词是dec和dec(p,s)。

数字在功能上等同于十进制。



p(精度)


小数点左侧和右侧可存储的最大小数位数。

精度必须是从1到最大精度为38的值。

默认精度为18.



s(比例)

可存储在小数点右侧的最大小数位数。

比例必须是0到p之间的值。

只有在指定精度时才能指定比例。

默认比例为0;因此,0< = s< = p。

最大存储容量根据精度而变化。



精度存储字节



1 - 9 5

10-19 9

20-28 13

29-38 17



Float与Real之间的区别



数据类型:浮动

范围: - 1.79E + 308到-2.23E-308,0和2.23E -308到1.79E + 308

存储空间:取决于价值n



数据类型:真实

范围: - 3.40E + 38至-1.18E - 38,0和1.18E - 38至3.40E + 38 >
存储: 4字节



nvalue: 1-24

精度: 7位数

存储空间: 4个字节



nvalue: 25-53

精度: 15位数

存储空间: 8字节





INT,BIGINT,SMALLINT之间的差异和TINYINT



数据类型: int

范围: -2 ^ 31(-2,147,483,648)到2 ^ 31-1(2,147,483,647)

存储: 4字节



数据类型: bigint

范围: -2 ^ 63(-9,223,372,036,854,775,808)至2 ^ 63-1(9,223,372,036,854,775,807)

存储: 8字节



数据类型: smallint

范围: -2 ^ 15(-32,768)至2 ^ 15-1(32,767)

存储: 2字节



数据类型: tinyint

范围: 0到255

存储: 1字节



结论:



int数据类型是SQL Server中的主要整数数据类型。



bigint数据类型适用于整数值可能超出范围的情况。由int数据类型支持。



bigint在数据类型优先级图表中适合smallmoney和int之间。



函数只有在参数表达式是bigint数据类型时才返回bigint。



SQL Server不会自动提升其他整数数据类型(tinyint ,bigint和int)到bigint。





MONEY和SMALLMONEY之间的差异



数据类型:

范围: -922,337,203,685,477.5808至922,337,203,685,477.5807

存储: 8字节



数据类型:

范围: - 214,748.3648至214,748.3647

存储: 4字节





结论:

money和s​​mallmoney数据类型精确到它们所代表的货币单位的万分之一。
STRING AND BINARY TYPES
Differences between BINARY AND VARBINARY
BINARY1
Fixed-length binary data with a length of n bytes, where n is a value from 1 through 8,000. The storage size is n bytes.

VARBINARY
Variable-length binary data. n can be a value from 1 through 8,000.

Max indicates that the maximum storage size is 2^31-1 bytes.

The storage size is the actual length of the data entered + 2 bytes.

The data that is entered can be 0 bytes in length.

The ANSI SQL synonym for varbinary is binary varying.

Conclusion:
Use binary when the sizes of the column data entries are consistent.

Use varbinary when the sizes of the column data entries vary considerably.

Use varbinary(max) when the column data entries exceed 8,000 bytes.

Differences between CHAR AND VARCHAR
CHAR
Fixed-length. so any remaining space in the field is padded with blanks.
non-Unicode character data with a length of n bytes, n must be a value from 1 through 8,000. The storage size is n bytes
The SQL-2003 synonym for char is character.

VARCHAR
Variable-length, non-Unicode character data. n can be a value from 1 through 8,000. max indicates that the maximum storage size is 2^31-1 bytes.
The storage size is the actual length of data entered + 2 bytes.
The data entered can be 0 characters in length.
The SQL-2003 synonyms for varchar are char varying or character varying.
For example, if you set a VARCHAR(100) datatype = ''Jen'', then it would take up 3 bytes (for J, E, and N) plus 2 bytes, or 5 bytes in all.

Conclusion:
Use Char, When Using the fixed length data in column & data entries are consistent, like phone number
Use Varchar When using the variable length data in column & data entries vary considerably, like address

Differences between VARCHAR/VARCHAR(MAX) and NVARCHAR/NVARCHAR(MAX)
Varchar/Varchar(MAX)
Varchar(8000) stores a maximum of 8000 characters. Varchar(max) stores a maximum of 2 147 483 647 characters or 2GB.
Variable length Non-Unicode Characters data.
The storage size is the actual length of data entered + 2 bytes
The SQL-2003 synonyms for varchar are char varying or character varying.

NVarchar/NVarchar(MAX)
NVarchar(4000) stores a maximum of 4000 characters. Varchar(max) stores a maximum of 1 073 741 823 characters or 1GB.
Variable length Unicode Characters data.
The storage size, in bytes, is two times the number of characters entered + 2 bytes
The SQL-2003 synonyms for nvarchar are national char varying and national character varying.

Differences between NCHAR AND NVARCHAR
NCHAR

Fixed-length. so any remaining space in the field is padded with blanks.
Unicode character data with a length of n bytes, n must be a value from 1 through 4,000. The storage size is n bytes
The ISO synonyms for nchar are national char and national character.

NVARCHAR
Variable-length, non-Unicode character data. n can be a value from 1 through 4,000. max indicates that the maximum storage size is 2^31-1 bytes (2GB).
The storage size is in bytes, is two times the actual length of data entered+ 2 bytes
The ISO synonyms for nvarchar are national char varying and national character varying.

Conclusion:
Use nchar when the sizes of the column data entries are probably going to be similar.
Use nvarchar when the sizes of the column data entries are probably going to vary considerably.

Differences between NTEXT, TEXT AND IMAGE
NTEXT

Variable-length Unicode data with a maximum string length of 2^30 - 1 (1,073,741,823).
Storage size, in bytes, is two times the string length entered.
The ISO synonym for ntext is national text.

TEXT
Variable-length non-Unicode data in the code page of the server and with a maximum string length of 2^31-1 (2,147,483,647).
When the server code page uses double-byte characters, the storage is still 2,147,483,647 bytes.
Depending on the character string, the storage size may be less than 2,147,483,647 bytes.

IMAGE

Variable-length binary data from 0 through 2^31-1 (2,147,483,647) bytes.

NUMERIC DATATYPES

Difference between Decimal[ (p[ ,s] )] and Numeric[(p[ ,s] )]

Fixed precision and scale numbers.
When maximum precision is used, valid values are from - 10^38 +1 through 10^38 - 1.
The ISO synonyms for decimal are dec and dec(p, s).
Numeric is functionally equivalent to decimal.

p (precision)

The maximum total number of decimal digits that can be stored, both to the left and to the right of the decimal point.
The precision must be a value from 1 through the maximum precision of 38.
The default precision is 18.

s (scale)
The maximum number of decimal digits that can be stored to the right of the decimal point.
Scale must be a value from 0 through p.
Scale can be specified only if precision is specified.
The default scale is 0; therefore, 0 <= s <= p.
Maximum storage sizes vary, based on the precision.

Precision Storage bytes

1 - 9 5
10-19 9
20-28 13
29-38 17

Difference between Float and Real

Data type: float
Range: - 1.79E+308 to -2.23E-308, 0 and 2.23E-308 to 1.79E+308
Storage: Depends on the value of n

Data type: real
Range: - 3.40E + 38 to -1.18E - 38, 0 and 1.18E - 38 to 3.40E + 38
Storage: 4 Bytes

nvalue: 1-24
Precision: 7 digits
Storage size: 4 bytes

nvalue: 25-53
Precision: 15 digits
Storage size: 8 bytes


Differences between INT, BIGINT, SMALLINT and TINYINT

Data type: int
Range: -2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647)
Storage: 4 Bytes

Data type: bigint
Range: -2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807)
Storage: 8 Bytes

Data type: smallint
Range: -2^15 (-32,768) to 2^15-1 (32,767)
Storage: 2 Bytes

Data type: tinyint
Range: 0 to 255
Storage: 1 Bytes

Conclusion:

The int data type is the primary integer data type in SQL Server.

The bigint data type is intended for use when integer values might exceed the range that is supported by the int data type.

bigint fits between smallmoney and int in the data type precedence chart.

Functions return bigint only if the parameter expression is a bigint data type.

SQL Server does not automatically promote other integer data types (tinyint, smallint, and int) to bigint.


Differences between MONEY and SMALLMONEY

Data type: money
Range: -922,337,203,685,477.5808 to 922,337,203,685,477.5807
Storage: 8 Bytes

Data type: money
Range: - 214,748.3648 to 214,748.3647
Storage: 4 Bytes


Conclusion:
The money and smallmoney data types are accurate to a ten-thousandth of the monetary units that they represent.