假设为实体: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