【问题标题】:er model -> relational model (database)er 模型 -> 关系模型(数据库)
【发布时间】:2012-05-06 19:25:41
【问题描述】:

我有一个包含以下实体的 e-r 模型:Doctor, Patient, HumanDoctor -> HumanPatient -> Human 之间存在生成关系。我正在尝试创建一个关系模型。那么,哪个模型是正确的:第一个还是第二个?

1)

Human (Name, Surname, Sex, Address)
Doctor(License number, specification)
Patient(Insurance number, diagnosis)

2)

Doctor(Name, Surname, Sex, Address, License number, specification)
Patient(Name, Surname, Sex, Address, Insurance number, diagnosis)

实体 Human 不是必需的。

附:刚接触关系模型。

【问题讨论】:

    标签: relational-database entity-relationship


    【解决方案1】:

    这在很大程度上取决于您的要求。医生也可以是病人吗?如果是,您需要像human 这样的基表。在这种情况下,doctorpatient 应该有一个指向 human 的外键。

    如果您的 DBMS 支持,另一种选择是使用表继承。在 PostgreSQL 中,你可以例如做:

    create table human 
    (
       name text,
       surname text,
       sex char(1),
       address text
    );
    
    create table doctor 
    (
       license_number integer,
       specifiction text
    )
    inherits (human);
    
    create table patient
    (
       insurance_number integer,
       diagnosis text
    )
    inherits (human);
    

    【讨论】:

      【解决方案2】:

      两种模型都是“正确的”。但是插入和查询数据是有区别的。

      当您希望将数据进行规范化持久化时,第一个是一个不错的选择(有关规范化的更多信息,请参阅http://en.wikipedia.org/wiki/Database_normalization)。但是,例如,当您要查询所有医生或所有患者时,您必须连接两个表。
      此外,当您插入一个新对象时,您必须插入两行(人类和医生/患者)。
      当您使用 OR-Mapper,它将您的数据转换为对象时,您可以使用多态性,因为您有一个共同的基础(人类)。

      当速度很重要时,第二种可能性是一个不错的选择。
      当您想要查询所有医生/患者时,它会更快,因为不需要连接。
      但是当你想查询时它会更慢,例如医生和患者的所有地址

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-26
        • 1970-01-01
        • 1970-01-01
        • 2013-03-27
        • 1970-01-01
        • 2021-06-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多