货币值的最佳SQL数据类型是什么?我正在使用MySQL,但更喜欢数据库独立的类型。


当前回答

Assaf的回应

这取决于你有多少钱…

听起来很轻率,但实际上是相关的。

就在今天,我们遇到了一个问题,记录未能插入到我们的Rate表中,因为其中一列(GrossRate)被设置为Decimal(11,4),而我们的产品部门刚刚获得了一份合同,在Bora Bora的某个令人惊讶的度假胜地,每晚售价数百万太平洋法郎……这是10年前设计数据库模式时从未预料到的。

其他回答

Decimal(19,4)在大多数情况下工作得很好。您可以调整比例和精度,以适应您需要存储的数字的需要。即使在SQL Server中,我也不倾向于使用“money”,因为它是不标准的。

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年前设计数据库模式时从未预料到的。

虽然这可能有点晚,但对其他人来说是有帮助的。根据我的经验和研究,我已经知道并接受十进制(19,6),这是在使用php和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)