【问题标题】:Database multiple choice Questi0n table relationship数据库多选Questi0n表关系
【发布时间】:2012-05-23 15:24:36
【问题描述】:

我有一个问答系统。一个问题有 4 个选项和 1 个答案(没有正确答案,只是用户的答案)。那么我应该如何建立这3个表之间的关系呢?我在下面所做的是否正确?就像问题表中缺少某些东西一样,因为我有 4 个选择(我将如何插入它们?)

Question Table: QuestionId, QuestionText
Choice Table: ChoiceId, ChoiceText, QuestionId
Answer Table: AnswerId, ChoiceId, QuestionId, UserId

提前谢谢你。这是我在stackoverflow中的第一个问题:)

祝你有美好的一天,

阿尔珀

【问题讨论】:

    标签: database database-design relationship


    【解决方案1】:

    由于ChoiceId 已经确定了QuestionId,因此如果您想对数据进行完全规范化,可以删除Answer 表中的QuestionId

    不过我会使用以下结构:

    create table questions (
        question_id number primary key,
        question_text text
    );
    create table questions_choices (
        question_id number references questions(question_id),
        choice_number number,
        choice_text text,
        primary key(question_id, choice_number)
    );
    create table answers (
        answer_id number primary key,
        user_id number references users(user_id),
        question_id number,
        choice_number number,
        foreign key (question_id, choice_number) references questions_choices(question_id, choice_number)
    );
    

    这与您所拥有的类似,但在 questions_choices 表上没有代理键。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-19
      • 2017-03-18
      • 1970-01-01
      • 2010-12-20
      • 2013-01-29
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多