Mysql 将资金保存到数据库中。设计决策
我需要一个表来存储金融交易的状态。 该事务的状态可以由该类大致建模Mysql 将资金保存到数据库中。设计决策,mysql,database-design,currency,Mysql,Database Design,Currency,我需要一个表来存储金融交易的状态。 该事务的状态可以由该类大致建模 class FinancialTransaction { Integer txId, Money oldLimit, Money newLimit, Money oldBalance, Money newBalance, Date txDate } class Money { Currency curr, BigDecimal amount } 我对模式的初
class FinancialTransaction
{
Integer txId,
Money oldLimit,
Money newLimit,
Money oldBalance,
Money newBalance,
Date txDate
}
class Money
{
Currency curr,
BigDecimal amount
}
我对模式的初始设计如下所示:
CREATE TABLE tx
(
txId bigint(20) unsigned NOT NULL,
oldlimit_currency varchar(3) NULL,
oldlimit_amount decimal(7,5) default 0.00,
newlimit_currency varchar(3) NULL,
newlimit_amount decimal(7,5) default 0.00,
----snipped----
PRIMARY KEY (txId)
)
有两件事让我担心:
CREATE TABLE tx
(
txId bigint(20) unsigned NOT NULL,
oldlimit_money_id int NOT NULL,
newlimit_money_id int NOT NULL,
----snipped----
PRIMARY KEY (txId),
FOREIGN KEY (oldlimit_money_id) REFERENCES MONEY(id) ON DELETE NO ACTION ON UPDATE NO ACTION,
FOREIGN KEY (newlimit_money_id) REFERENCES MONEY(id) ON DELETE NO ACTION ON UPDATE NO ACTION
)
有其他的设计吗
谢谢懒虫网
货币和货币价值是两个不同的概念,所以最好将它们分开。没有必要为“价值”单独创建一个表,但最好为货币创建一个表,因为它们是独立的实体。新设计看起来像:
CREATE TABLE tx
(
id bigint(20) unsigned primary key,
old_limit_currency_id int not null references CURRENCY(id),
old_limit_value decimal(7,5) not null,
new_limit_currency_id int not null references CURRENCY(id),
new_limit_value decimal(7,5) not null
)
此外,检查十进制(7,5)是否有足够的空间用于场景,它看起来有点低。有一句老话:“安全总比遗憾好”:如果这不是一种过激行为,请更进一步,将所有金额/价值存储在一种货币中,并维护一个汇率表 第三个想法呢:
CREATE TABLE tx
(
txId bigint(20) unsigned NOT NULL,
currency varchar(3) NOT NULL,
oldlimit decimal(7,5) default 0.00,
newlimit decimal(7,5) default 0.00,
----snipped----
PRIMARY KEY (txId)
)
一个事务的所有货币值都必须使用相同的货币,对吗?为什么要将货币存储在事务上,而不是通过SQL事务创建引用到事务的新货币条目,然后再执行SUM()检查状态。这样你就可以更好地跟踪所涉及的数量。你需要保持整个速率历史来执行有效的计算,这是一个很好的优化,这是我可能考虑的事务。谢谢,谢谢你指出我对小数点(7,5)的理解。我一定会和DBA一起回顾这一点。