2015-10-17 107 views
2

我正在開發在線多選題考試的演示申請。 當然,每個問題都有多個選項。在問題屏幕上,候選人將選擇其中一個選項,提交併導航到下一個問題。多選題考試數據庫結構

我制定了以下表格結構。

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。但請忽略語法,因爲我只是想傳達這個想法。請建議是否可以有更好的方法。
剛剛添加我將在服務器端使用彈簧&休眠。

+0

就個人而言,我會在答案表 – Strawberry

+0

中標記正確的答案請詳細說明。 '你的意思'問題'表中的'right_option'欄應該在'user_answers'表中嗎?或者'user_answers'表中的'answer'列不應該引用'options'表,而只是將答案標識爲true/false? – ivish

+0

第一個 - 但不是user_answers。只是答案表。 – Strawberry

回答

2

我最近有必要編寫一個完全由數據庫驅動並可由客戶端更改的問卷系統。

我最終將可能的答案作爲JSON對象存儲在數據庫中,然後存儲針對問題ID給出的答案,但是這增加了一些複雜性級別。

如果每一個問題是一個多選擇的答案,然後你的方法將是一個合乎邏輯的做法

+0

是的,每個問題都是一個多選題。謝謝 – ivish

3

一旦你乾淨了一點東西,最大的一個是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可以技術上被保存爲一個回答不同的問題。

最大外賣給你是永遠不會用答案表中的用戶名做到這一點。不能有一個以上的同名真實用戶(大問題)。另外,如果用戶名是拼寫錯誤,它會在用戶表中的一處發生變化。數據的完整性。加入一個身份證。

注意:auto_increment必須是主鍵,且主鍵不能爲NULL,因此在其旁邊鍵入NOT NULL是多餘的。只是說。

+0

沒錯,同意你對'用戶名'字段的意見。並同意「在主鍵上鍵入NOT NULL是多餘的」。另外,在java(服務器端)代碼中,我將確保'答案'總是屬於它應該考慮的'問題'。我正在整理你的建議。謝謝 – ivish

+0

不客氣。祝你好運ivish – Drew

+0

謝謝,德魯。 :-) – ivish