【问题标题】:How to translate a (0,N)(0,N)(0,N)ternary relationship in ER model in this situation?在这种情况下,如何在 ER 模型中转换 (0,N)(0,N)(0,N) 三元关系?
【发布时间】:2018-08-01 18:45:55
【问题描述】:

假设为实体: 在家工作, 学生, 回答

和约束(仔细看约束1):

    1)A STUDENT can give only 1 ANSWER for the same HOMEWORK
    2) A HOMEWORK can be solved by (0,N) STUDENT each giving their answer
    3)AN ANSWER can be submitted by (0,N) STUDENT 
    4)(Obviously it is possible to give the same answer
for different HOMEWORK 
and that different STUDENT can give the same answer for the same HOMEWORK)

示例:

HOMEWORK STUDENT ANSWER
 XXX       A        1
 XXX       B        1
 XXX       C        2
 YYY       B        1
 YYY       C        1
 ZZZ       A        3
 ZZZ       C        1

注意 学生不可能为同一个作业提交 2 个解决方案; 所以应该不允许插入第 XXX A 2 行

我会用三元关系来建模:

    STUDENT---------(0,N) <DO>(0,N)---------HOMEWORK
                           (0,N)
                            |
                            |
                          ANSWER

然后使用通常的翻译算法翻译成关系模型:

-- -- means FOREIGN KEY 
 _____ means PRIMARY KEY

 DO(HOMEWORK,STUDENT,ANSWER) 
    -- -- -- -- -- -- -- -- 
    _______________________ 
 HOMEWORK(with his attributes)
 STUDENT(with his attributes)
 ANSWER(with his attributes)

由于 ANSWER 是主键的一部分,这意味着学生可以解决相同的作业提交不同的答案; 这违反了预期的约束。

也许我会解决这个问题:

1)-transforming the ternary relationship DO in a binary relationship and adding an attribute ANSWER to DO
-then create a trigger to check that the value of answer in DO is a possible answer.

Or 

2)Keep ternary relationship but use another trigger

但我想知道您的意见。(例如,如果您在 ER 中以不同的方式对这个问题进行建模)。

PS -我使用 Postgres

【问题讨论】:

    标签: database postgresql database-design entity-relationship relational-model


    【解决方案1】:

    如果我正确理解了规范,我认为您的问题可以在不使用触发器的情况下建模,只需在表示三元关系的表上引入一个约束。

    让我们这样定义它(属性“fkT”代表表T的外键):

    ProposedSolution (fkAnswer, fkHomework, fkStudent)
      primary key (fkAnswer, fkHomework, fkStudent),
      unique (fkHomework, fkStudent)
    

    请注意,主键约束使学生、答案和作业的组合具有唯一性,同时允许以下事实,例如,不同的学生可以给出相同的解决方案(即同一个作业的答案相同),或者学生可以对不同的作业给出相同的答案。

    还有待执行的是约束 1:即学生不能对同一个作业给出多个答案。但这可以通过唯一约束来解决,它保证在表中我们不能有两个具有相同值的学生和作业的元组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 2012-12-10
      • 2020-03-06
      • 1970-01-01
      • 2021-07-25
      • 1970-01-01
      • 2019-07-21
      相关资源
      最近更新 更多