【问题标题】:Is there any difference between DECIMAL and NUMERIC in SQL Server?SQL Server 中 DECIMAL 和 NUMERIC 之间有什么区别吗?
【发布时间】:2010-10-20 01:15:53
【问题描述】:

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

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

【问题讨论】:

    标签: sql-server sqldatatypes


    【解决方案1】:

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

    MSDN:decimal and numeric

    【讨论】:

    • 功能等价不等于等价。实际上,在 MS6232A 课程的讲师幻灯片中,有一条评论补充说它们几乎相同。同时,Microsoft 没有就其中一种或另一种提出建议(但是 DECIMAL 更有意义,因为它是标准的数据类型,而不是 Sybase 的旧数据类型)。仍然想知道真正的区别(在屏幕后面)是什么:-)。
    • @vstrien:我能找到的唯一区别是,在 SQL-92 标准中,decimal 与声明的一样完全精确,而numeric至少与声明的一样精确。在 SQL Server 中,两者都与声明的一样精确,即它没有使用标准允许的numeric 的灵活性。
    • 但是请注意,SQL Server 不会将它们视为可互换的:例如,如果您有一个“DECIMAL(18,0)”格式的“父”列,并且您尝试添加如果 外键 引用“NUMERIC(18,0)”格式的列,您将收到错误 Column '<referencedColumn>' is not the same data type as referencing column '<parentTable>.<parentColumn>' in foreign key '<yourKeyName>'。它们必须都是 NUMERIC(x,y),或两者都是是十进制(x,y)。
    • @Guffa:您的回答与stackoverflow.com/a/759606/14731 引用的 SQL2003 标准相矛盾。具体来说,decimal至少与声明的一样精确,而numeric完全与声明的一样精确。
    • @Gili:是的,在审查原始材料时,您似乎是对的,我确实交换了它们。
    【解决方案2】:

    这就是 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>.
    

    【讨论】:

    【解决方案3】:

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

    编辑:

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

    【讨论】:

    • 重新编辑:不 - 请参阅上面的@JoakimBackman 的回答 - 他用 NUMERIC 和 DECIMAL 引用 SQL 标准。
    【解决方案4】:

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

    有细微的差别。根据 SQL For Dummies,第 8 版(2013 年):

    DECIMAL 数据类型类似于 NUMERIC。 ...不同的是 您的实现可能指定的精度大于您的精度 指定——如果是这样,实现使用更高的精度。如果你 不指定精度或比例,实现使用默认值 值,就像对 NUMERIC 类型一样。

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

    【讨论】:

      【解决方案5】:

      它们是同义词,完全没有区别。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
      

      【讨论】:

        【解决方案6】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-02-06
          • 1970-01-01
          • 2013-09-13
          • 2017-04-05
          • 2014-09-18
          • 2015-06-14
          相关资源
          最近更新 更多