如何向 IS-A-to-Two-Tables 关系添加外键约束引用?

How to add a foreign key constraint references to the IS-A-to-Two-Tables relationship?

首先,在我的 ER 模型中,我有一个 account 实体,它与两个不相交的子实体有 IS-A 关系 储蓄账户支票账户。 但是我有一个客户实体与 account 实体有 depositor 关系,这样的用例如下面的 ER 图。

因此我将 ER 翻译成 table 这样的:

客户(cid,cname)

存款人(cid,账号)

saving-account(account-number, balance, interest-rate)

checking-account(account-number, balance, overdraft-amount)

所以最后,我只从 IS-A 关系中提取 两个 tables 储蓄账户和支票账户。


然后问题出现了,要创建存款人table,我取一个T-SQL:

CREATE TABLE depositor(
    customer_id         int             not null,
    account_number      int             not null,
    access_date         Date            DEFAULT GETDATE(),
    PRIMARY KEY(customer_id, account_number),
    FOREIGN KEY(customer_id)    REFERENCES customer(customer_id),
    FOREIGN KEY(account_number) REFERENCES account(account_number)
)

At the final line, the foreign key account_number should reference to the account table, but what I have are saving-account and checking-account tables. In this situation, how do I add a constraint in T-SQL? Is it possible to add a constraint if I only take two tables from the IS-A relationship?

我建议你有一个额外的 table 叫做 account。然后 saving-accountchecking-account 应该是分开的 table 指向那个。

此外,您应该有一个 account_id 字段作为 PK 而不是 account_number。作为最佳实践规则,PK不应该有任何商业意义,它应该是一个抽象的概念。

总而言之,我建议遵循 tables:

account(aid, account-number, balance, plus any other common field for account)
saving-account([optional pk], aid_ref, interest-rate, plus fields specific to saving-account)
checking-account([optional pk], aid_ref, overdraft-amount, plus fields specific to saving-account)
customer(cid, cname)
depositor(cid, aid)