【发布时间】:2010-10-20 01:15:53
【问题描述】:
SQL Server 中 DECIMAL 和 NUMERIC 数据类型有什么区别吗?
什么时候应该使用 DECIMAL,什么时候应该使用 NUMERIC?
【问题讨论】:
SQL Server 中 DECIMAL 和 NUMERIC 数据类型有什么区别吗?
什么时候应该使用 DECIMAL,什么时候应该使用 NUMERIC?
【问题讨论】:
它们是一样的。数字在功能上等同于十进制。
MSDN:decimal and numeric
【讨论】:
decimal 与声明的一样完全精确,而numeric 是 至少与声明的一样精确。在 SQL Server 中,两者都与声明的一样精确,即它没有使用标准允许的numeric 的灵活性。
Column '<referencedColumn>' is not the same data type as referencing column '<parentTable>.<parentColumn>' in foreign key '<yourKeyName>'。它们必须都是 NUMERIC(x,y),或两者都是是十进制(x,y)。
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>.
【讨论】:
据我所知,NUMERIC 和 DECIMAL 数据类型之间没有区别。它们是彼此的同义词,可以使用任何一个。 DECIMAL 和 NUMERIC 数据类型是具有固定精度和小数位数的数值数据类型。
编辑:
与一些同事交谈可能与 DECIMAL 是 ANSI SQL 标准和 NUMERIC 是 Mircosoft 更喜欢的一种有关,因为它在编程语言中更常见。 ...也许;)
【讨论】:
Joakim Backman 的回答是具体的,但这可能会使其更加清晰。
有细微的差别。根据 SQL For Dummies,第 8 版(2013 年):
DECIMAL 数据类型类似于 NUMERIC。 ...不同的是 您的实现可能指定的精度大于您的精度 指定——如果是这样,实现使用更高的精度。如果你 不指定精度或比例,实现使用默认值 值,就像对 NUMERIC 类型一样。
似乎一些 SQL 实现的区别在于数据完整性。 DECIMAL 允许从基于某些系统默认值定义的内容中溢出,而 NUMERIC 则不允许。
【讨论】:
它们是同义词,完全没有区别。Decimal 和 Numeric 数据类型是具有固定精度和小数位数的数值数据类型。
-- 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
【讨论】:
它们完全一样。使用时保持一致。在您的数据库中使用其中之一
【讨论】: