加密货币MySQL数据类型? [英] Crypto Currency MySQL Datatypes ?

查看:195
本文介绍了加密货币MySQL数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于在SQL数据库中存储货币值时有关数据类型的臭名昭著的问题.

但是在这些艰难的时期,我们现在拥有的货币最多可以保留18个小数位(谢谢ETH).

这现在重新提出了经典的论点.

IDEAS

选项1 BIGINT使用大整数保存实际值,然后存储货币有多少个小数位(在转换中将A除以10^B)?

选项2 Decimal(60,30)将数据类型存储在较大的十进制数中,这不可避免地会占用大量空间.

选项3 VARCHAR(64)存储在字符串中.这会对性能产生影响.


我想知道人们的想法以及他们在处理加密货币值时正在使用什么.令我感到困惑的是最好的进行程序的方法.

解决方案

在您提出的三个建议中,有一个显而易见的最佳选择(在注释中加上一个).

BIGINT —仅使用8个字节,但最大的BIGINT仅具有19个十进制数字;如果您除以10 18 ,则可以代表的最大值是9.22,这是不够的范围.

DOUBLE (双精度)—仅具有15-17个十进制数字;具有浮点运算的所有已知缺点.

VARCHAR -如果要处理18个小数位,则将使用20个以上字节;将需要恒定的string conversionint转换;无法排序;无法比拟的;不能在数据库中添加;许多缺点.

DECIMAL(27,18) –如果使用MySQL,则将占用12个字节( 解决方案

There's a clear best option out of the three you suggested (plus one from the comments).

BIGINT — uses just 8 bytes, but the largest BIGINT only has 19 decimal digits; if you divide by 1018, the largest value you can represent is 9.22, which isn't enough range.

DOUBLE — only has 15–17 decimal digits of precision; has all the known drawbacks of floating-point arithmetic.

VARCHAR — will use 20+ bytes if you're dealing with 18 decimal places; will require constant string↔int conversions; can't be sorted; can't be compared; can't be added in DB; many downsides.

DECIMAL(27,18) – if using MySQL, this will take 12 bytes (4 for each group of 9 digits). This is quite a reasonable storage size, and has enough range to support amounts as large as one billion or as small as one Wei. It can be sorted, compared, added, subtracted, etc. in the database without loss of precision.

I would use DECIMAL(27,18) (or DECIMAL(36,18) if you need to store truly huge values) to store cryptocurrency money values.

这篇关于加密货币MySQL数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆