实体关系模型 - 三元运算符

Entity Relationship Model - Ternary operators

目前,我正在做一些数据库方面的工作。为此,我创建了一个实体关系模型,但是当涉及到三元运算符时,我在阅读它时遇到了一个小问题。我们来看这个例子:

             -------------  (0|N)                    (0|N)  -------------
             |   Mother  |-----------------x----------------|   Father  |
             -------------                 |                -------------
                                           |
                                           |
                                           |
                                           |
                                           | (1|1)
                                     -------------
                                     |   Child   |
                                     -------------

这个小实体关系模型是这样写的:

A mother can have 1 to N childs - Okay

A father can have 1 to N childs - Okay

A child has exatcly 1 father and 1 mother - Okay

但也可以这样说:

A father has N mothers - Not okay

A mother has N fathers - Not okay

我是不是理解错了,或者这些三元运算符不是很明显?通过此实现,可以创建不一致的数据集。方向对这些运算符重要还是我必须以其他方式阅读它们?

你的例子不是三元关系。

这是一个更准确的 ERD,可以正确地符合您的业务规则:

可以查看并解释三元关系的示例 here。来自该页面:

A course offering must have at least one person, but may have many (at least two one would think: the instructor and a lonely student), and a person may be involved in some way with many courses, or none at all. The role attribute of the relationship indicates that person's role in the offering: student, instructor, etc.

您的主要实体是。母、父、子是人与人之间的关系。

create table People(
    ID    int primary key auto_generated,
    Sex   Char( 1 ) check( Sex in( 'F', 'M' )),
    ...
);
create unique index UQ_People_Parent on People( ID, Sex );

如果 ID 本身是唯一的,为什么要使 ID 和性别的组合唯一?

create table Parents(
    MotherID   int,
    FemaleFlag char( 1 ) check FemaleFlag = 'F',
    FatherID   int,
    MaleFlag   char( 1 ) check MaleFlag = 'M',
    constraint FK_Parent_Mother foreign key( MotherID, FemaleFlag )
        references People( ID, Sex ),
    constraint FK_Parent_Father foreign key( FatherID, MaleFlag )
        references People( ID, Sex ),
    constraint PK_Parents primary key( MotherID, FatherID )
);

这将设置一个 m-n 交集 table。这很好,因为 parent 可以与不同的配偶有 children。 Flag 的添加允许我们强制执行限制,即母亲必须是女性,父亲必须是男性。

create table OffSpring(
    PersonID   int primary key,
    MotherID   int,
    FatherID   int,
    constraint FK_OffSpring_Parents foreign key( MotherID, FatherID )
        references Parents( MotherID, FatherID ),
    constraint FK_Offspring_Person foreign key( PersonID )
        references People( ID )
);

后代可以是女儿或儿子,所以没有性别之分。

如果我们有parent的ID,想找他们的后代,可以直接加入People到OffSpring中找,ID字段为MotherID字段或FatherID字段视情况而定。如果我们有一个 OffSpring 的 ID,我们可以直接将 OffSpring 连接到 MotherID 字段或 FatherID 字段到 ID 字段上的 People。 Parents table 主要是为了强制执行 1) 母亲和父亲被定义为一对,以及 2) 母亲必须是女性,父亲必须是男性。

对于所有阅读本文的人,请不要再说任何政治正确的性别歧义废话。这仅用于说明。如果有人想要Parent1和Parent2而不是Mother和Father,其中一个或两个都是男性或女性,那么就这样设计。