简体   繁体   English

用于记录多个答案的数据库设计

[英]Database Design for logging multiple answers

I am designing a database for a project I am working on. 我正在为我正在从事的项目设计数据库。 I have 3 tasks (which the user can choose from) 我有3个任务(用户可以选择)

First task gives 10 questions Second task gives 20 questions Third task gives 30 questions 第一项任务给10个问题第二项任务给20个问题第三项任务给30个问题

I have come up with an improved design but I am still struggling. 我已经提出了改进的设计,但我仍在努力。

It looks like : 看起来像 :

User (user_id, Name, reg_date) User (user_id,名称,reg_date)

Test (test_id, task_type [either 1,2,3], category [there are 5 different categories] ) Test (test_id,task_type [1,2,3],类别[有5个不同类别])

Response (Response_id, user_id, test_id, response_date, response_1a, response_1b, response_2a, response_2b, ... , response_30a, response_30b) Response (Response_id,user_id,test_id,response_date,response_1a,response_1b,response_2a,response_2b,...,response_30a,response_30b)

For each question, there are going to be two answers. 对于每个问题,将有两个答案。 Answer 1 and Answer 2. So if there are 10 questions, there are going to be total of 20 answers logged. 答案1和答案2。因此,如果有10个问题,总共将记录20个答案。 Each answer will be a decimal value from 0~1.0. 每个答案将是一个从0到1.0的十进制值。

But, I run in to the problem of having +120 columns for the Response table. 但是,我遇到了响应表有+120列的问题。

Notice that if the test_id is 1, there would be only 10 questions, so the columns in Response table from response_11a to response_30b would be null. 请注意,如果test_id为1,那么将只有10个问题,因此Response表中从response_11a到response_30b的列将为空。

An example record would be: 记录示例为:

Response (1, 1, 1, 2015/04/30, 0.5, 0.5, 0.7, 0.7, 0.8, 0.8, ... (a lot of records)... , 1.0, 0.3, null, null, null, (all nulls because there are only 10 questions for test_id 1. ) ) 响应(1,1,1,2015/04/30,0.5,0.5,0.7,0.7,0.8,0.8,...(很多记录)...,1.0,0.3,null,null,null,(全部为null,因为test_id 1只有10个问题。

I cannot think of another way of doing this. 我想不出另一种方法。 :/ :/

Is this a good way of designing the database? 这是设计数据库的好方法吗?

EDIT: 编辑:

I had an idea of doing this. 我有个想法。

User (user_id, Name, reg_date) User (user_id,名称,reg_date)

Test (test_id, task_type [either 1,2,3], category [there are 5 different categories] ) Test (test_id,task_type [1,2,3],类别[有5个不同类别])

Response (resp_id, user_id, test_id, resp_date) Response (resp_id,user_id,test_id,resp_date)

answers (resp_id (links to response table), current_question_number, ans1, ans2) answers (resp_id(指向响应表的链接),current_question_number,ans1,ans2)

So if there are 10 questions, in the answers table, there would be 10 records that look like: 因此,如果answers表中有10个问题,那么将有10条记录,如下所示:

(1, 1, 0.0, 0.0)
(1, 2, 0.0, 0.5)
(1, 3, 0.3, 0.6)
(1, 4, 0.5, 0.5)
...
(1, 10, 0.3, 0.3)

How is this design? 这个设计怎么样?

Later on, I will be using this data to plot out a graph so I need each responses to be easily accessible. 稍后,我将使用这些数据绘制图表,因此我需要每个响应都易于访问。

In database design you regularly have to consider more fields or more records and how normalised the database needs to be what you have suggested above isn't entirely normalised. 在数据库设计中,您通常必须考虑更多字段或更多记录,并且如何完全规范化数据库才能达到您上面建议的水平。

DB101 Normalisation http://en.wikipedia.org/wiki/Database_normalization DB101规范化http://en.wikipedia.org/wiki/Database_normalization

I'd suggest a 4th table and maybe 5th Table 我建议第四张桌子,也许第五张桌子

  • User: PK UserID, Attributes such as address.. 用户:PK用户名,属性,如地址。
  • Test: PK TestID, Some extra attributes 测试:PK TestID,一些额外的属性
  • Question (2xPrimary fields one is foreign key) TestID , QuestionID, 问题(2xPrimary字段之一是外键)TestID,QuestionID,
  • Response (3xPrimary fields two are foreign key) TestID , QuestionID, ResponseID, Some extra attributes 响应(3xPrimary字段中的两个是外键)TestID,QuestionID,ResponseID以及一些其他属性

I have one suggestion for the database design. 我对数据库设计有一个建议。

i have made a few modification in Response Table and Create a new Table. 我在“响应表”中做了一些修改并创建了一个新表。

  • USER Table (User_ID [PK],USER_NAME,another Extra fields) USER表(User_ID [PK],USER_NAME和其他Extra字段)
  • TEST Table (Test_ID [PK],some other fields for test details) TEST表(Test_ID [PK],其他一些字段用于测试详细信息)
  • Quest_Set Table (Set_ID ,Que_ID,Question,some other fields if required) Quest_Set表(Set_ID,Que_ID,问题,其他一些字段(如果需要))
  • RESPONSE Table (Res_id ,User_ID [FK],Test_ID [FK], Set_ID , Que_ID, Ans_1 , Ans_2 , Flag) 响应表(Res_id,User_ID [FK],Test_ID [FK],Set_ID,Que_ID,Ans_1,Ans_2,标志)

Please review it and let me know if you have any confusions. 请对其进行审核,如果您有任何疑问,请告诉我。

Regards. 问候。

Please look the New_Response Table 请查看New_Response表

(Response_id, user_id, test_id,Trans_id,Ans1,Ans2,Flag) (Response_id,user_id,test_id,Trans_id,Ans1,Ans2,标志)

You need to further normalise your table. 您需要进一步规范化表。 You would need another table 'questions' to prevent redundant data storage. 您将需要另一个表“问题”以防止冗余数据存储。

在此处输入图片说明

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

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