PSQL中DECIMAL和NUMERIC数据类型的区别

时间:2015-11-16 07:46:39

标签: postgresql rdbms sqldatatypes

postgreSQL中decimalnumeric数据类型的用法是什么。根据参考,以下是对这些数据类型的解释。

Decimal,numeric --> It is a user specified precision, exact and range up to 131072 digits before the decimal point and up to 16383 digits after the decimal point.

以上语句显示了decimalnumeric数据类型的说明。但是,我仍然不明白是什么     确切使用这些数据类型以及使用它而不是其他数据类型。

非常感谢以示例回答......

3 个答案:

答案 0 :(得分:32)

Right from the manual:

  

类型decimalnumeric是等效的。这两种类型都是SQL标准的一部分。

至于“为什么我需要使用它”,这也在手册中解释:

  

数字类型可以存储数字位数非常多且完全执行计算

(强调我的)。

如果您需要带小数的数字,请使用decimal(或numeric),如果您需要不带小数的数字,请使用integerbigintdecimal作为列类型的典型用法是“产品价格”列或“利率”。整数类型的典型用途是例如。一个列,用于存储许多产品的订购方式(假设您不能订购“半”产品)。

doublereal也是可以存储小数值的类型,但它们是近似类型。这意味着您不一定要检索存储的值。有关详细信息,请参阅:http://floating-point-gui.de/

答案 1 :(得分:7)

直接从https://www.postgresql.org/message-id/20211.1325269672@sss.pgh.pa.us

引用
  

在Postgres中没有任何区别。有两种类型名称   因为SQL标准要求我们接受这两个名称。快点   看看标准看起来唯一的区别是:

     17)NUMERIC specifies the data type exact numeric, with the decimal
        precision and scale specified by the <precision> and <scale>.

     18)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>.
     

即,对于DECIMAL,允许实现允许更多数字   比小数点左边的要求。 Postgres没有   行使自由,所以这些类型之间没有区别   我们。

      regards, tom lane

答案 2 :(得分:3)

它们是彼此的同义词,功能相同。 SQL:2003标准说:

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>.