简体   繁体   English

多项选择题的数据库结构

[英]Database structure for multiple choice examination

I am developing a demo application for an online multiple choice examination.我正在开发一个用于在线多项选择考试的演示应用程序。 Each question will have multiple options, of course.当然,每个问题都有多个选项。 On the question screen, candidate will select one of the option, submit it and will be navigated to the next question.在问题屏幕上,候选人将选择一个选项,提交并导航到下一个问题。

I have worked out following table structure.我已经制定了以下表格结构。

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)
 );

The database will be MySql.数据库将是 MySql。 But please ignore the syntax since i just want to convey the idea.但是请忽略语法,因为我只是想传达这个想法。 Please suggest if there can be a better approach.请建议是否有更好的方法。
Just to add I will be using spring & hibernate on the server side.补充一下,我将在服务器端使用 spring & hibernate。

Once you clean things up a bit, biggest one being the user_answers.username (which is de-normalized), by trying something like this:一旦你清理了一些东西,最大的就是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)
);

Then the only problem that immediately jumps out at me is that the user_answers.answer could technically be saved as an answer to a different question.然后立即跳出来的唯一问题是 user_answers.answer 从技术上讲可以保存为不同问题的答案。

The biggest takeaway for you is to never do that with the username in the answers table.对您来说最大的收获是永远不要使用答案表中的用户名这样做。 Can't have more than one real user with the same name that way (big problem).同名的真实用户不能超过一个(大问题)。 Also, if the user name was a typo, it changes in one place in users table.此外,如果用户名是拼写错误,它会在用户表中的一个地方发生变化。 Data integrity.数据的完整性。 Join on an id as such.加入一个id。

Note: an auto_increment must be a primary key, and a primary key can never be NULL, so it is redundant to type NOT NULL next to it.注意:auto_increment 必须是主键,并且主键永远不能为 NULL,因此在它旁边键入 NOT NULL 是多余的。 Just saying.就是说。

I recently had the need to write a questionnaire system that was fully database driven and changeable by the client.我最近需要编写一个完全由数据库驱动且可由客户更改的问卷系统。

I ended up storing the possible answers as a JSON object in the database and then storing the answer given against the question ID, however this adds a few levels of complexity.我最终将可能的答案作为 JSON 对象存储在数据库中,然后根据问题 ID 存储给出的答案,但这增加了一些复杂性。

If every question is a multi-choice answer then your approach would be a logical approach如果每个问题都是多项选择答案,那么您的方法将是合乎逻辑的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM