货币值的最佳SQL数据类型是什么?我正在使用MySQL,但更喜欢数据库独立的类型。
当前回答
For accounting applications it's very common to store the values as integers (some even go so far as to say it's the only way). To get an idea, take the amount of the transactions (let's suppose $100.23) and multiple by 100, 1000, 10000, etc. to get the accuracy you need. So if you only need to store cents and can safely round up or down, just multiply by 100. In my example, that would make 10023 as the integer to store. You'll save space in the database and comparing two integers is much easier than comparing two floats. My $0.02.
其他回答
Decimal(19,4)在大多数情况下工作得很好。您可以调整比例和精度,以适应您需要存储的数字的需要。即使在SQL Server中,我也不倾向于使用“money”,因为它是不标准的。
计算出计算所需的小数点后多少位也很重要。
我开发了一个股票价格应用程序,需要计算100万股的价格。报价的股价必须精确存储到7位数。
For accounting applications it's very common to store the values as integers (some even go so far as to say it's the only way). To get an idea, take the amount of the transactions (let's suppose $100.23) and multiple by 100, 1000, 10000, etc. to get the accuracy you need. So if you only need to store cents and can safely round up or down, just multiply by 100. In my example, that would make 10023 as the integer to store. You'll save space in the database and comparing two integers is much easier than comparing two floats. My $0.02.
Assaf的回应
这取决于你有多少钱…
听起来很轻率,但实际上是相关的。
就在今天,我们遇到了一个问题,记录未能插入到我们的Rate表中,因为其中一列(GrossRate)被设置为Decimal(11,4),而我们的产品部门刚刚获得了一份合同,在Bora Bora的某个令人惊讶的度假胜地,每晚售价数百万太平洋法郎……这是10年前设计数据库模式时从未预料到的。
非常晚进入,但GAAP是一个很好的经验法则。对货币字段应用DECIMAL(13,4)就足够了。
如果你的应用程序需要处理高达一万亿的货币价值,那么这应该工作:13,2如果你需要遵守GAAP(公认会计原则),那么使用:13,4 通常你应该把你的货币值加到13,4,然后把输出四舍五入到13,2。
在MySQL中,您可以使用DECIMAL或NUMERIC数据类型,因为它们存储精确的数值数据值。
使用13,4可以得到999,999,999.9999美元。根据ISO 4217,只有2个国家使用小数点后4位(智利和乌拉圭)。
来源:
ISO 4217 -定义用于表示货币的alpha和数字代码。 流通货币一览表 在MySQL中存储货币值的最佳数据类型
推荐文章
- 如何关闭mysql密码验证?
- 如何在Ruby On Rails中使用NuoDB手动执行SQL命令
- 查询JSON类型内的数组元素
- 确定记录是否存在的最快方法
- MySQL区分大小写查询
- 获得PostgreSQL数据库中当前连接数的正确查询
- 在SQL选择语句Order By 1的目的是什么?
- MySQL数据库表中的最大记录数
- 原则-如何打印出真正的sql,而不仅仅是准备好的语句?
- PHP/MySQL插入一行然后获取id
- 我如何循环通过一组记录在SQL Server?
- 如何从命令行通过mysql运行一个查询?
- 外键约束可能导致循环或多条级联路径?
- 使用LIMIT/OFFSET运行查询,还可以获得总行数
- 当恢复sql时,psql无效命令\N