Which PostgreSQL column type should be used to store a Java BigDecimal?

好久不见. 提交于 2020-02-21 07:32:30

问题


What PostgreSQL column type should I use to store a Java BigDecimal?


回答1:


See PostgreSQL datatypes - perhaps Numeric, which can act as an arbitrary precision type (this is a PostgreSQL extension).

...without any precision or scale creates a column in which numeric values of any precision and scale can be stored, up to the implementation limit on precision.

I am not entirely sure what "implementation limit on precision is", though. Have never tried really large numbers. If the limit is reached, then a fallback to text is possible. But, I suspect there will be significant other issues before then ;-) If you wish to store less, then specify a precision and/or scale to numeric.

Edit as sjr pointed out, the restriction is 1000 decimal digits of precision (from the same link):

The type numeric can store numbers with up to 1000 digits of precision [in currentl implementations] and perform calculations exactly. It is especially recommended for storing monetary amounts and other quantities where exactness is required...

If more precision is needed - despite having a much bigger problem at hand - then a numeric column will not be suitable (by itself). But this is really more of a very extreme "what if" and likely does not play a limiting role.




回答2:


Just use the Java mappings for the common SQL data types. In this case you can use a NUMERIC or DECIMAL.



来源:https://stackoverflow.com/questions/4643592/which-postgresql-column-type-should-be-used-to-store-a-java-bigdecimal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!