货币值的最佳SQL数据类型是什么?我正在使用MySQL,但更喜欢数据库独立的类型。
当前回答
唯一需要注意的是,如果从一个数据库迁移到另一个数据库,你可能会发现DECIMAL(19,4)和DECIMAL(19,4)表示不同的东西
(http://dev.mysql.com/doc/refman/5.1/en/precision-math-decimal-changes.html)
DBASE: 10,5 (10 integer, 5 decimal) MYSQL: 15,5 (15 digits, 10 integer (15-5), 5 decimal)
其他回答
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.
计算出计算所需的小数点后多少位也很重要。
我开发了一个股票价格应用程序,需要计算100万股的价格。报价的股价必须精确存储到7位数。
唯一需要注意的是,如果从一个数据库迁移到另一个数据库,你可能会发现DECIMAL(19,4)和DECIMAL(19,4)表示不同的东西
(http://dev.mysql.com/doc/refman/5.1/en/precision-math-decimal-changes.html)
DBASE: 10,5 (10 integer, 5 decimal) MYSQL: 15,5 (15 digits, 10 integer (15-5), 5 decimal)
Assaf的回应
这取决于你有多少钱…
听起来很轻率,但实际上是相关的。
就在今天,我们遇到了一个问题,记录未能插入到我们的Rate表中,因为其中一列(GrossRate)被设置为Decimal(11,4),而我们的产品部门刚刚获得了一份合同,在Bora Bora的某个令人惊讶的度假胜地,每晚售价数百万太平洋法郎……这是10年前设计数据库模式时从未预料到的。
虽然这可能有点晚,但对其他人来说是有帮助的。根据我的经验和研究,我已经知道并接受十进制(19,6),这是在使用php和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