简体   繁体   English

我已经分配了一个有关SQL中货币汇率的项目。 如何保存货币汇率?

[英]I have been assigned a project for currency exchange rate IN SQL . How to save exchange rates for currencies?

I Have to create a table currencies 我必须创建表格货币

CREATE TABLE currencies (
       from_currency        CHAR(30) NOT NULL,
       ExchRate             REAL NOT NULL,
       to_currency          CHAR(30) NOT NULL,
       PRIMARY KEY (from_currency),
       FOREIGN KEY (to_currency) REFERENCES currencies(from_currency)
)

I have to save exchange rate for 6 currencies: (inr,eur,cad,usd,gbp,cfp) But the problem is i can't store same values in first column ie I can save my currency rate as follow 我必须保存6种货币的汇率:(inr,eur,cad,usd,gbp,cfp)但问题是我无法在第一列中存储相同的值,即我可以按如下方式保存货币汇率

from_curr      ER --> to_curr 
INR        --> 60 -->   USD
USD        --> 0  -->   USD
GBP        --> 70 -->   USD

etc for all the currencies but now the problem is while i try to store the currency for other er like 对于所有货币等,但现在的问题是,当我尝试为其他货币存储货币时

INR        --> 1  -->   USD
or
GBP        --> 70 -->   USD 

I get error duplicates values are not allowed ? 我收到错误重复值,是不允许的吗? How to solvve this problem ? 如何解决这个问题?

You are not storing a currency, but rather a pair of currencies. 您不是在存储货币,而是在存储两种货币。

CREATE TABLE currency_exchange_rates (
       from_currency        CHAR(30) NOT NULL,
       to_currency          CHAR(30) NOT NULL,
       ExchRate             REAL NOT NULL,
       PRIMARY KEY (from_currency,to_currency),
)

I would also suggest a separate table holding the six currencies and linking the table above to ensure valid currency codes. 我还建议使用一个单独的表格,其中包含六种货币,并链接上述表格以确保有效的货币代码。 You might also want to hold a date field (and make it part of the primary key), since rates can change over time... 您可能还想保留一个日期字段(并将其作为主键的一部分),因为费率会随着时间而变化...

You should either create a composite primary key as a pair of 2 currencies is uniquely identifying a record for you. 您应该创建一个复合主键,因为两种货币对可以唯一地标识一条记录。

CREATE TABLE currencies (
   from_currency        CHAR(30) NOT NULL,
   ExchRate             REAL NOT NULL,
   to_currency          CHAR(30) NOT NULL,
   PRIMARY KEY (from_currency, to_currency)
)

Or alternatively you can add a new column for the primary key, eg as autoincrement column: 或者,您可以为主键添加一个新列,例如作为自动增量列:

CREATE TABLE currencies (
   [Id] [int] IDENTITY(1,1) NOT NULL
   from_currency        CHAR(30) NOT NULL,
   ExchRate             REAL NOT NULL,
   to_currency          CHAR(30) NOT NULL,
   PRIMARY KEY ([Id])
)

Of course you can't. 当然不能。 That's because you defined PRIMARY KEY () . 那是因为您定义了PRIMARY KEY () Thus only distinct value is allowed for this column. 因此,此列仅允许使用不同的值。 What you need instead is that pairs of from_currency/to_currency are unique. 相反,您需要的是成对的from_currency / to_currency是唯一的。 You can do this by defining a composite PK in your table: 您可以通过在表中定义一个复合PK来做到这一点:

CREATE TABLE currencies (
       from_currency        CHAR(30) NOT NULL,
       ExchRate             REAL NOT NULL,
       to_currency          CHAR(30) NOT NULL,
       PRIMARY KEY (from_currency, to_currency)
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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