0

我有这两张表,公司和所有者。现在它们都处于正常形式,但我需要在它们之间创建多对多关系,因为一个公司可以有很多所有者,而一个所有者可以有很多公司

我之前得到了一个答案,即向公司添加一组 CompanyOwners(带有所有者 UUID)是否会破坏 Normal Form,它会破坏 Normal Form,并且已经能够收集到可以使用的是Junction Table,请参阅线程

我的问题如下:创建一个额外的连接表,如下所示,会打破正常形式吗?

-- This is the junction table.
CREATE TABLE CompanyOwners(
    Connection-ID UUID NOT NULL, // Just the ID (PK) of the relationship.
    Company-ID UUID NOT NULL REFERENCES Company (Company-ID),
    Owner-ID UUID NOT NULL REFERENCES Owner (Owner-ID),
    CONSTRAINT "CompanyOwners" PRIMARY KEY ("Connection-ID")
)
4

2 回答 2

1

您的结构允许重复数据。例如,它允许这样的数据。(UUID 被缩写以防止水平滚动。)

Connection_id            Company_id               Owner_id
--
b56f5dc4...af5762ad2f86  4d34cd58...a4a529eefd65  3737dd70...a359346a13b3
0778038c...ad9525bd6099  4d34cd58...a4a529eefd65  3737dd70...a359346a13b3
8632c51e...1876f6d2ebd7  4d34cd58...a4a529eefd65  3737dd70...a359346a13b3

关系中的每一行都应该有不同的含义。该表允许数百万行具有相同的含义。

沿着这些路线的东西更好。它在 5NF 中。

CREATE TABLE CompanyOwners(
    Company_ID UUID NOT NULL references Company (Company_ID),
    Owner_ID UUID NOT NULL references Owner (Owner_ID),
    PRIMARY KEY (Company_ID, Owner_ID)
);

标准 SQL 不允许在标识符中使用“-”。

于 2013-12-27T14:36:29.220 回答
0

这很好,但您可以添加更多列,例如

DateOwned Datetime  --<-- when the owner bought the company 
DateSold Datetime  --<-- when a the owner sold the compnay

毕竟,您想知道公司是否仍归同一所有者所有,并跟踪公司的所有权历史等。

于 2013-12-22T22:23:23.163 回答