简体   繁体   English

主键和外键数据库设计

[英]Primary and foreign keys database design

I am creating SQL Server tables for a currency exchange system. 我正在为货币兑换系统创建SQL Server表。

I have one table which is the following: 我有一张桌子如下:

CREATE TABLE [CurrencyKeys]
(
    [Key] [nchar](3) NOT NULL, 
    [Currency] [nvarchar](30) NOT NULL
    CONSTRAINT [PK_Key] PRIMARY KEY ([Key])
);

How would I create the table for the exchange rates itself which references the keys from the CurrencyKeys table? 我如何为汇率本身创建表,该表引用CurrencyKeys表中的键?

Currently I have the following: 目前,我有以下内容:

CREATE TABLE [ExchangeRates]
(
    [DateTime] [datetime]NOT NULL,
    [FromCurrCode] [nchar](3) NOT NULL,
    [ToCurrCode] [nchar] (3) NOT NULL,
    [Rate] [money] NOT NULL
)

Would I need to create ( FromCurrCode , ToCurrCode ) as primary key as well? 我是否还需要创建( FromCurrCodeToCurrCode )作为主键?

If I understand you correctly, you need to define the foreign keys on the exchangeRates table. 如果我对您的理解正确,则需要在exchangeRates表上定义外键。

You would define a foreign key for EACH column, like so: 您将为EACH列定义一个外键,如下所示:

alter table [ExchangeRates] add constraint fk_ExchangeRates_01 foreign key (fromCurrCode) references CurrencyKeys([Key]);

alter table [ExchangeRates] add constraint fk_ExchangeRates_02 foreign key (toCurrCode) references CurrencyKeys([Key]);

That will define foreign keys for your exchangeRates table. 这将为您的exchangeRates表定义外键。

Don't you just want to do this? 您不是只想这样做吗?

CREATE TABLE [ExchangeRates]
(
    [DateTime] [datetime]NOT NULL,
    [FromCurrCode] [nchar](3) NOT NULL,
    [ToCurrCode] [nchar] (3) NOT NULL,
    [Rate] [money] NOT NULL,
    Foreign Key([FromCurrCode]) References [CurrencyKeys]([Key]),
    Foreign Key([ToCurrCode]) References [CurrencyKeys]([Key])
)

Or are you trying to ask how to maintain primary key values for ExchangeRates table itself. 或者,您是否正在尝试询问如何维护ExchangeRates表本身的主键值。 Can you please make yourself clear? 你能说清楚点吗?

It depends on whether your table could have several rows referring to the same (From,To) pair (to track different exchange rates on different dates) 这取决于您的表是否可以有多行引用同一对(从,到)对(以跟踪不同日期的不同汇率)。

If that is the case, then the PK could be (FromCurrCode, ToCurrCode, DateTime) 如果是这样,则PK可以是(FromCurrCode,ToCurrCode,DateTime)

If you are only saving one/the last exchange rate, then (FromCurrCode, ToCurrCode) should be enough. 如果您仅保存一个/最后一个汇率,那么(FromCurrCode,ToCurrCode)应该就足够了。

It appears that you are using natural keys in lieu of surrogate keys. 看来您正在使用自然键代替代理键。

In order for the ExchangeRates table to use natural keys, you would need to create a composite primary key using FromCurrCode and ToCurrCode 为了使ExchangeRates表使用自然键,您需要使用FromCurrCode和ToCurrCode创建一个复合主键

CREATE TABLE [ExchangeRates]
(
    [DateTime] [datetime]NOT NULL,
    [FromCurrCode] [nchar](3) NOT NULL,
    [ToCurrCode] [nchar] (3) NOT NULL,
    [Rate] [money] NOT NULL
    CONSTRAINT [PK_ExchangeRates] PRIMARY KEY ([FromCurrCode], [ToCurrCode])
)

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

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