【问题标题】:Database structure for multiple choice examination多选题数据库结构
【发布时间】:2016-01-16 09:21:30
【问题描述】:

我正在为在线多项选择考试开发一个演示应用程序。 当然,每个问题都有多个选项。在问题屏幕上,候选人将选择其中一个选项,提交它并导航到下一个问题。

我已经制定了以下表格结构。

CREATE  TABLE users (
  username VARCHAR(45) NOT NULL ,
  password VARCHAR(45) NOT NULL ,
  enabled TINYINT NOT NULL DEFAULT 1 ,
);

CREATE TABLE questions (
    id int(10) NOT NULL auto_increment
    question varchar(800) NOT NULL,
    right_option int(10) NOT NULL references options(id)
);

CREATE TABLE options (
    id int(10) NOT NULL auto_increment,  
    question_id int(10) NOT NULL references questions(id),
    option varchar(150) NOT NULL,              
);

CREATE TABLE exam_details (
    id  int(10) NOT NULL,
    username varchar(45) NOT NULL references users(username),
    date_of_exam date,
    exam_result varchar(10) NOT NULL, -- PASS/FAIL
    exam_score int(10) NOT NULL,      -- e.g. 40 
    no_of_questions int(10) NOT NULL  -- total no. of questions in the test
);     

CREATE TABLE user_answers (
    id  int(10) NOT NULL,
    username varchar(45) NOT NULL references users(username),
    question_id int(10) NOT NULL references questions(id),
    answer int(10) NOT NULL references options(id)
 );

数据库将是 MySql。但是请忽略语法,因为我只是想传达这个想法。请建议是否有更好的方法。
补充一下,我将在服务器端使用 spring & hibernate。

【问题讨论】:

  • 就个人而言,我会在答案表中标记正确答案
  • 请详细说明。您的意思是“问题”表中的“right_option”列应该在“user_answers”表中吗?或者“user_answers”表中的“答案”列不应该引用“选项”表,而只是将答案标识为真/假?
  • 第一个 - 但不是 user_answers。只是答案表。
  • 与“user_answers”相比,表名“answers”似乎是一个不错的命名选择。谢谢

标签: mysql database hibernate


【解决方案1】:

一旦你清理了一些东西,最大的一个是user_answers.username(它是非规范化的),尝试这样的事情:

CREATE  TABLE users (
  id int(10) auto_increment primary key,
  username VARCHAR(45) NOT NULL ,
  password VARCHAR(45) NOT NULL ,
  enabled TINYINT NOT NULL DEFAULT 1
);

CREATE TABLE questions (
    id int(10) auto_increment primary key,
    question varchar(800) NOT NULL,
    right_option int(10) NOT NULL references options(id)
);

CREATE TABLE options (
    id int(10) auto_increment primary key,
    question_id int(10) NOT NULL references questions(id),
    `option` varchar(150) NOT NULL
);

CREATE TABLE exam_details (
    id int(10) auto_increment primary key,
    username varchar(45) NOT NULL references users(username),
    date_of_exam date not null,
    exam_result varchar(10) NOT NULL, -- PASS/FAIL
    exam_score int(10) NOT NULL,      -- e.g. 40 
    no_of_questions int(10) NOT NULL  -- total no. of questions in the test
);     

CREATE TABLE user_answers (
    id int(10) auto_increment primary key,
    userId int(10) NOT NULL references users(id),
    question_id int(10) NOT NULL references questions(id),
    answer int(10) NOT NULL references options(id)
);

那么我立即想到的唯一问题是 user_answers.answer 技术上可以保存为不同问题的答案。

对您来说最大的要点是永远不要使用答案表中的用户名这样做。这样,同名的真实用户不能超过一个(大问题)。此外,如果用户名是拼写错误,它会在 users 表中的一个位置更改。数据的完整性。像这样加入一个 id。

注意:auto_increment 必须是主键,并且主键永远不能为 NULL,因此在其旁边键入 NOT NULL 是多余的。只是说说而已。

【讨论】:

  • 对,同意您对“用户名”字段的观察。并同意“在主键旁边键入 NOT NULL 是多余的”。此外,在 java(服务器端)代码中,我将确保“答案”始终属于它应该属于的“问题”。我正在努力采纳您的建议。谢谢
【解决方案2】:

我最近需要编写一个完全由数据库驱动且可由客户更改的问卷调查系统。

我最终将可能的答案作为 JSON 对象存储在数据库中,然后根据问题 ID 存储给出的答案,但这会增加一些复杂性。

如果每个问题都是多选答案,那么您的方法将是一种合乎逻辑的方法

【讨论】:

  • 是的,每个问题都是选择题。谢谢
猜你喜欢
  • 2013-01-20
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 2015-09-03
  • 2016-10-06
  • 2012-04-27
  • 1970-01-01
相关资源
最近更新 更多