Database 表关系中的数据库多项选择问题
我有一个问答系统。一个问题有4个选项和1个答案(没有正确答案,只是用户的答案)。那么我应该如何建立这三个表之间的关系呢?我下面所做的正确吗?这就像问题表中缺少了什么,因为我有4个选择(我将如何插入它们?) 先谢谢你。这是我在stackoverflow中的第一个问题:) 祝你今天愉快Database 表关系中的数据库多项选择问题,database,database-design,relationship,Database,Database Design,Relationship,我有一个问答系统。一个问题有4个选项和1个答案(没有正确答案,只是用户的答案)。那么我应该如何建立这三个表之间的关系呢?我下面所做的正确吗?这就像问题表中缺少了什么,因为我有4个选择(我将如何插入它们?) 先谢谢你。这是我在stackoverflow中的第一个问题:) 祝你今天愉快 Alper由于ChoiceId已经识别了QuestionId,如果您想在数据中实现完全标准化,那么您可以在Answer表中去掉QuestionId 不过,我会使用以下结构: create table question
Alper由于
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)
);
这与您拥有的类似,但在问题\u选项
表上没有代理键
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)
);