SQL Server中的DECIMAL和NUMERIC数据类型有什么区别?
何时应该使用DECIMAL,何时应该使用NUMERIC?
SQL Server中的DECIMAL和NUMERIC数据类型有什么区别?
何时应该使用DECIMAL,何时应该使用NUMERIC?
以下是 SQL2003 标准(§6.1 数据类型)对这两个概念的定义:
<exact numeric type> ::=
NUMERIC [ <left paren> <precision> [ <comma> <scale> ] <right paren> ]
| DECIMAL [ <left paren> <precision> [ <comma> <scale> ] <right paren> ]
| DEC [ <left paren> <precision> [ <comma> <scale> ] <right paren> ]
| SMALLINT
| INTEGER
| INT
| BIGINT
...
21) NUMERIC specifies the data type
exact numeric, with the decimal
precision and scale specified by the
<precision> and <scale>.
22) DECIMAL specifies the data type
exact numeric, with the decimal scale
specified by the <scale> and the
implementation-defined decimal
precision equal to or greater than the
value of the specified <precision>.
-- Initialize a variable, give it a data type and an initial value
declare @myvar as decimal(18,8) or numeric(18,8)----- 9 bytes needed
-- Increse that the vaue by 1
set @myvar = 123456.7
--Retrieve that value
select @myvar as myVariable
它们完全相同。在使用时要保持一致。在您的数据库中使用其中之一。
Column '<referencedColumn>' is not the same data type as referencing column '<parentTable>.<parentColumn>' in foreign key '<yourKeyName>'
。它们必须都是NUMERIC(x,y),或者都是DECIMAL(x,y)。 - Doug_Ivisondecimal
至少和声明一样精确,而numeric
则完全如声明所示精确。 - Gili