简体   繁体   中英

Database schema for Exchange rates application

I am developing a custom web applicaton for a small company that exchanges currencies with its own rates which will store in the application, will store client transactions and keep past daily rates stored for reports.

The first functional part for which I am going to design the MySQL DB is the following:

  • User inputs every day and for once manually the currency (for ex. USD,GBP,AUD) exchange rates that will sell/buy each coin based on 1 EUR, daily. For example's simplicity sake let's say that will have only sell rates set.
  • Daily rates will remain in the DB for being used in history reports and will not be overwritten with the ones of the next day.

So there are some options to design the MySQL DB for this part

First schema option

currencies (id, name)  

|___ there will be the unique abbreviation name of each coin (USD,GBP, etc.)

rates_daily (id, date, currencies.id[fk], net_price, fee, total_price)

|___ there will be stored the daily rates of each currencies.name

Since it has to store multiple currencies.name rates in same table, I have to keep data integrity by making a UNIQUE key by combining date & currencies.id

However it does not feel right to use one table which stores daily rates of multiple currencies is going to be huge if every let's say 20 currencie rate record are stored in same table.

Second schema option

currencies (id, name)

|___ there will be the unique names of each coin (USD,GBP, etc.)

currencies_daily (id, date, currencies.id[fk])  

|___ UNIQUE key by combining date & currencies.id

rates_daily (id, currencies_daily.date[fk], currencies_daily.currencies.id[fk], net_price, fee, total_price)  

|___ UNIQUE key by combining currencies_daily.date & currencies_daily.currencies.id

Maybe my mind is weird but I have this feeling that there could be a Third schema option for this part which will be more tidier.

If any, could you suggest me a better one ?

From what I can tell, your second option does not offer any advantages over the first one, except to add an unnecessary third table to the schema. Here is what I might use:

CREATE TABLE currencies (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(55) NOT NULL,
    PRIMARY KEY (id)
)

CREATE TABLE rates_daily (
    id INT NOT NULL AUTO_INCREMENT,
    date DATETIME NOT NULL,
    currency_id INT NOT NULL,
    bid NUMERIC (15, 2) NOT NULL,
    ask NUMERIC (15, 2) NOT NULL,
    FOREIGN KEY fk_curr (currency_id)
    REFERENCES currencies (id)
)

The two monetary points I think you would want to capture for each currency, on each day, is the bid and ask price. These two prices determine the spread, and also how much one would expect to pay when purchasing a currency, or to receive when selling a currency.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM