133

SQL Server 中 DECIMAL 和 NUMERIC 数据类型之间有什么区别吗?

什么时候应该使用 DECIMAL,什么时候应该使用 NUMERIC?

4

6 回答 6

109

他们是一样的。数字在功能上等同于十进制。

MSDN:十进制和数字

于 2009-04-17T07:43:37.867 回答
42

这就是 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>.
于 2009-04-17T08:49:08.333 回答
11

据我所知,NUMERIC 和 DECIMAL 数据类型之间没有区别。它们是彼此的同义词,可以使用任何一个。DECIMAL 和 NUMERIC 数据类型是具有固定精度和小数位数的数值数据类型。

编辑:

与几位同事交谈可能与 DECIMAL 是 ANSI SQL 标准和 NUMERIC 是 Mircosoft 更喜欢的一种有关,因为它在编程语言中更常见。...也许 ;)

于 2009-04-17T07:46:31.400 回答
2

Joakim Backman 的回答是具体的,但这可能会使其更加清晰。

有一个小的区别。根据 SQL For Dummies,第 8 版(2013 年):

DECIMAL 数据类型类似于 NUMERIC。...不同之处在于您的实现可能指定的精度大于您指定的精度 - 如果是这样,则实现使用更高的精度。如果您不指定精度或小数位数,则实现使用默认值,就像使用 NUMERIC 类型一样。

似乎某些SQL 实现的区别在于数据完整性。DECIMAL 允许从基于某些系统默认值定义的内容中溢出,而 NUMERIC 则不允许。

于 2017-08-26T20:52:02.937 回答
1

它们是同义词,完全没有区别。十进制和数字数据类型是具有固定精度和小数位数的数字数据类型。

-- 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
于 2016-07-26T16:52:35.363 回答
-2

它们完全相同。使用时保持一致。在您的数据库中使用其中之一

于 2018-09-05T15:28:43.123 回答