PSQL中DECIMAL和NUMERIC数据类型之间的差异



在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数据类型的描述。但是,我仍然不明白这些数据类型的确切使用,以及在哪里使用它而不是其他数据类型。

请举例回答。。。

手册右侧:

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

至于"为什么我需要使用它",手册中也有解释:

类型numeric可以存储具有大量数字的数字,并且精确地执行计算

(强调我的)。

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

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

直接引用自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

它们是彼此的同义词,功能相同。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>.

相关内容

最新更新