【问题标题】:How to relate weak with weak entity如何将弱实体与弱实体联系起来
【发布时间】:2014-03-09 15:30:43
【问题描述】:

我如何将一个弱实体与另一个与强实体相关的弱实体转换为关系模型? 提前致谢!

我想将此 ER 模型转换为关系数据库结构。所以,我想知道这是否正确:

BOOK_STORE(idBookStore, address); 

SECTION(idBookStore, sectionNumber, surface) 
    WHERE idBookStore REFERS BOOK_STORE; 

SHELL(shellNumber, sectionNumber, idBookStore) 
    WHERE sectionNumber 
    REFERS TO SECTION and 
        idBookStore REFERS TO BOOK_STORE. 

所以我的问题是:这是正确的吗?

【问题讨论】:

  • 你能举个例子说明你想做什么吗?
  • 我已经插入了一张更具体的图片。提前致谢!
  • 这有帮助,但我仍然不完全清楚您的目标是什么。这张图是您的目标还是您的起点?您希望具体将哪些实体相互关联?
  • 我想把这个 ER 模型翻译成关系数据库结构。所以,我想知道这是否正确...... BOOK_STORE(idBookStore, address); SECTION(idBookStore,sectionNumber,surface)其中 idBookStore REFERS BOOK_STORE; SHELL(shellNumber, sectionNumber, idBookStore) where sectionNumber REFERS TO SECTION 和 idBookStore REFERS TO BOOK_STORE。所以我的问题是……这是正确的吗?

标签: database relational-database entity-relationship


【解决方案1】:

我会使用这个创建表语法来实现那个 ER 图

CREATE TABLE bookStore (
    idBookstore   integer,
    address       text,
    PRIMARY KEY(idBookstore)
);

CREATE TABLE section (
    idBookstore   integer,
    sectionNumber integer,
    surface       text,
    FOREIGN KEY (idBookstore) REFERENCES bookStore.idBookStore,
    PRIMARY KEY (idBookstore, sectionNumber)
);

CREATE TABLE shell (
    idBookstore   integer,
    sectionNumber integer,
    shellNumber   integer,
    FOREIGN KEY (idBookstore) REFERENCES bookStore.idBookStore,
    FOREIGN KEY (sectionNumber) REFERENCES section.sectionNumber,
    PRIMARY KEY (idBookstore, sectionNumber, shellNumber)
);

这里要考虑的关键点是定义弱实体的关键是什么。然后你可以替换相关实体的主键。

所以在这种情况下,section 有一个主键,即元组 (idBookstore,sectionNumber)。我们将用X 代替这个参数。

shell与section相关,是一个弱实体,所以它的key应该是(X, shellNumber)。这与section 的形式相同。我们可以扩展X来得到正确的外键(idBookstore, sectionNumber, shellNumber)

【讨论】:

    【解决方案2】:
    CREATE TABLE bookStore (
        idBookstore   integer,
        address       text,
        PRIMARY KEY(idBookstore)
    );
    
    CREATE TABLE section (
        idBookstore   integer,
        sectionNumber integer,
        surface       text,
        FOREIGN KEY (idBookstore) REFERENCES bookStore.idBookStore,
        PRIMARY KEY (idBookstore, sectionNumber)
    );
    
    CREATE TABLE shell (
        idBookstore   integer,
        sectionNumber integer,
        shellNumber   integer,
        FOREIGN KEY (idBookstore) REFERENCES bookStore.idBookStore,
        FOREIGN KEY (sectionNumber) REFERENCES section.sectionNumber,
        PRIMARY KEY (idBookstore, sectionNumber, shellNumber)
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 2016-12-21
      • 2017-05-22
      • 1970-01-01
      相关资源
      最近更新 更多