如何维护公共伟大父表的引用完整性?

发布于 2024-08-08 04:25:42 字数 1350 浏览 2 评论 0原文

假设我有一个包含“问题组”的表格

GroupID | value
--------+------
42      | How often do you
9071    | Other question
...

,并且对于每组问题我都有“问题”和“可能的答案”

Group | QuestionID | value
------+------------+------
42    | 5          | ... brush your teeth?
42    | 89         | ... go to the movies?
9071  | 709        | ... another question ...
...
Group | Answer | value
------+--------+------
42    | 134    | several times per day
42    | 135    | twice a day
42    | 71     | once a day
42    | 803    | every other day
42    | 8      | once a week
42    | 666    | once a month
...

现在,在 PHP/HTML 中,我对问题和可能的答案进行(虚拟)完整叉积第 42 组构建一个 2 条目表,用户将在其中选择他/她的答案(HTML 版本)

How often do you      | N/d | 2/d | 1/d | d*2 | 1/w | 1/m |
----------------------+-----+-----+-----+-----+-----+-----+
... brush your teeth? | ( ) | (X) | ( ) | ( ) | ( ) | ( ) |
... go to the movies? | ( ) | ( ) | ( ) | ( ) | (X) | ( ) |

我知道我需要
插入牙齿的答案(问题、答案...)值(5、135、...)

插入电影的答案(问题、答案...)值(89, 8, ...)

问题是:有没有办法强制执行问题和答案,在answers 表中,“属于”同一组问题吗?

我愿意
插入答案(问题、答案...)值(709、71、...)
被禁止,因为问题 709“属于”组 9071,答案 71 属于组 42。

Lets say I have a table with "Groups of Questions"

GroupID | value
--------+------
42      | How often do you
9071    | Other question
...

And, for each group of questions I have "questions" and "possible answers"

Group | QuestionID | value
------+------------+------
42    | 5          | ... brush your teeth?
42    | 89         | ... go to the movies?
9071  | 709        | ... another question ...
...
Group | Answer | value
------+--------+------
42    | 134    | several times per day
42    | 135    | twice a day
42    | 71     | once a day
42    | 803    | every other day
42    | 8      | once a week
42    | 666    | once a month
...

Now, in PHP/HTML, I do a (virtual) full cross product of questions and possible answers of group 42 to build a 2-entry table where the user will select his/her answers (HTML version)

How often do you      | N/d | 2/d | 1/d | d*2 | 1/w | 1/m |
----------------------+-----+-----+-----+-----+-----+-----+
... brush your teeth? | ( ) | (X) | ( ) | ( ) | ( ) | ( ) |
... go to the movies? | ( ) | ( ) | ( ) | ( ) | (X) | ( ) |

I know I'll need to
insert into answers (question, answer, ...) values (5, 135, ...) for teeth
and
insert into answers (question, answer, ...) values (89, 8, ...) for movies

The question is: is there a way to enforce that both question and answer, in the answers table, "belong" to the same group of questions?

I'd like that
insert into answers (question, answer, ...) values (709, 71, ...)
be disallowed, because question 709 "belongs" to group 9071 and answer 71 belongs to group 42.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮年 2024-08-15 04:25:42

好吧,答案与问题有关,而不是与小组有关(至少不是直接),所以我认为“可能的答案”表应该是:

Question | Answer | value
---------+--------+------
5        | 134    | several times per day

然后当你插入答案时,你给出QuestionId 和 AnswerId,它们一起引用“可能的答案”,从而间接引用该组。

Well, the answer relates to the question, not to the group (at least not directly), so I would have thought the "possible answers" table should be:

Question | Answer | value
---------+--------+------
5        | 134    | several times per day

Then when you insert an answer, you give the QuestionId and AnswerId, and together they reference the "possible answer" and thereby, indirectly, the group.

逆蝶 2024-08-15 04:25:42

您包含 GroupQuestionIDAnswer 列,然后创建两个外键,其中一个位于 (Group, QuestionID)另一个在(Group, Answer)上。

You include the columns Group, QuestionID and Answer then create two foreign keys, one on (Group, QuestionID) and the other on (Group, Answer).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文