1

假设为实体:HOMEWORK、STUDENT、ANSWER

和约束(仔细看约束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

4

1 回答 1

2

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

让我们按以下方式定义它(属性“fkT”代表表 T 的外键):

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

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

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

于 2018-08-02T06:45:44.223 回答