设计数据库结构

发布于 2024-10-29 02:44:42 字数 392 浏览 1 评论 0原文

我想创建带有测试的网站。测试包含不同的问题。它可以是文本框、复选框或单选按钮。我想在数据库中存储问题、答案和用户。

这是我对表结构的想法。

Users
{
   Id
   Name
}
Questions
{
   Id
   Description
   Type // textbox, checkbox, etc
}
Answers
{ 
    Id
    UserId
    QuestionId
    AnswerValue
}

但是我如何存储有关不同答案的信息?我可以在 AnswerValue 中写入“Carrot”(文本框)、“1011”(复选框)、“3”(单选按钮)等内容,不幸的是,我认为这不是一个好的解决方案。在我的情况下设计数据库结构的最佳方法是什么?

I want to create web site with tests. Test contains different questions. It could be textbox, checkbox or radiobutton. I want to store in db questions, answers and users.

Here is my thought about tables structure.

Users
{
   Id
   Name
}
Questions
{
   Id
   Description
   Type // textbox, checkbox, etc
}
Answers
{ 
    Id
    UserId
    QuestionId
    AnswerValue
}

But how i can store information about DIFFERENT answers? I can write in AnswerValue something like "Carrot" (textbox),"1011" (checkbox),"3" (radiobutton) etc Unfortunally it's not good solution i think. What is best way to design DB structure in my case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

揽清风入怀 2024-11-05 02:44:42

也许设计还不错......
由于您知道答案的 QuestionId,因此您可以查找问题的类型。

例如:

SELECT 
   Q.Type, A.* FROM Question Q, Answer A
WHERE
   Q.Id = A.QuestionId

然后在您的代码中,当您显示答案时,您将同时拥有答案值及其类型,因此您的代码可以根据 Q.Type 进行处理。

要解决如何存储具有多个值(复选框或单选按钮)的问题的答案的问题,您应该考虑将它们存储在另一个具有一对多关系的表中。

例如,复选框问题可以有许多项目(可检查的事物),而复选框答案可以有许多项目(已检查的项目)。在您当前的架构中,似乎没有任何方法可以存储每个项目的描述。因此,我建议使用新表 Items 和 Answers_Items

Items{
    ItemId
    QuestionId   
    Description
}

Answers_Items{
    ItemId
    AnswerId
    AnswerValue
}

Maybe the design isn't too bad...
Since you know the QuestionId of the Answer, you can lookup which type of question it was.

For example:

SELECT 
   Q.Type, A.* FROM Question Q, Answer A
WHERE
   Q.Id = A.QuestionId

Then in your code, when you're displaying answers, you'd have both the answer value and its type, so your code could process based on Q.Type.

To address the issue of how to store answers to questions with multiple values (checkboxes or radiobuttons) you should consider storing those in another table with a one-to-many relationship.

For example, a Checkbox question can have many Items (check-able things), and a Checkbox answer can have many items (the items which were checked). In your current schema, there doesn't seem to be any way to store the descriptions of each item. So, I suggest a new tables Items and Answers_Items

Items{
    ItemId
    QuestionId   
    Description
}

Answers_Items{
    ItemId
    AnswerId
    AnswerValue
}
溺深海 2024-11-05 02:44:42

这是一个相当大的问题,所以我想我会回答我所理解的核心问题。在我看来,您正在寻找一种方法来根据答案的类型确定要显示的控件类型。我在这方面使用了两种模式并取得了一些成功。其中之一涉及存储 AnswerType,我通常可以将其映射到 .Net 类型。然后,根据该类型,确定要显示哪种类型的控件(即字符串 -> TextBox、布尔 -> 单选按钮、多选/数组 -> 复选框列表等)。另一个涉及存储元数据(很像上面描述的那样)。

就您而言,我将探索前一种解决方案,因为它通常“更容易”实现,并且需要较少的预知和围绕元数据的编程。一组答案类型的示例可能包括:字符串、是/否、数字、列表。正如您所看到的,这些通用类型相当容易地映射到 .Net 类型。对于“列表”,您可能需要添加一个附加表用于查找(或AllowedValues)。

我希望这就是您正在寻找的。否则,我可能会建议研究类似的开源系统。 搜索可能是一个好的开始。

This is a rather large question, so I think I'll answer what I interpret as the core of it. It seems to me that you are looking for a way to, based on the kind of answer, determine what kind of control to display. There are two patterns that I have used with some success on this. One involves storing an AnswerType that I can generally map to a .Net type. Then, based upon that type, determine which kind of control to display (i.e. string -> TextBox, bool -> radiobutton, multiselect/array -> checkbox list, etc.). The other involves storing metadata (much like you described above).

In your case, I would explore the former solution as it is generally "easier" to implement and requires less foreknowledge and programming around metadata. An example of a set of answer types might include: String, Yes/No, Number, List. As you can see, these general types map to .Net types fairly easily. In the case of "List" you may want to add an additional table for lookups (or AllowedValues).

I hope this is what you are looking for. Otherwise, I might suggest looking into similar, open source, systems. This search might be a good start.

抚你发端 2024-11-05 02:44:42

您可能只想进一步扩展您的答案表,为每种类型的答案提供单独的值:
文字回答
RadioAnswer(将是所选单选按钮的索引)
CheckboxAnswer(检查值的可怕位图)

这种方法的问题是它们都必须可以为空,以便允许另一种类型的值作为给定问题的答案...如果您希望数据库强制执行它。

You might just want to extend your Answers table a bit more with a separate value for each type of answer:
TextAnswer
RadioAnswer (would be an index to the selected radio button)
CheckboxAnswer (the dread bitmap for checked values)

The problem with this approach is that they each have to be nullable in order to allow for another type of value being the answer to a given question...makes data integrity a bit more complicated if you want the db to enforce it.

痴情 2024-11-05 02:44:42

您的结构可以很好地存储用户对单个问题的多个答案。但是,您可能需要添加一个日期时间字段,以便了解给出特定答案的时间(相对于同一问题的其他答案)。

Your structure is fine to store a user's multiple answers for a single question. However, you may want to add a datetime field so you know when a particular answer was given (in relation to the other answers for the same question).

苍风燃霜 2024-11-05 02:44:42

我发现使用 string/varchar (对二进制数据进行 base64 编码)是最简单的解决方案,并且我已经尝试/看到了生产环境中的所有变化。我特别不喜欢有多个列,每个列对应一种数据类型。这对澳元来说是一个真正的痛苦。

I've found that using string/varchar (with base64 encoding for binary data) to be the simplest solution and I've tried/seen all the variations in a production environment. I particularly dislike having multiple columns, one for each datatype. This is a real pain in the A$$ to work with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文