带有问题和答案的数据库结构

发布于 2024-10-12 03:59:15 字数 281 浏览 1 评论 0原文

我正在设计一个包含问题、答案和类别的数据库。

一个问题可以属于多个类别,一个类别可以有多个问题。 所以这个关系是多对多的。

一张问题表
一类表
一张表包含上面两个表的主键

所以现在我的问题是,是否有必要或“更好”将答案(a,b,c,d,e)存储在名为answers的自己的表中?那么答案表就有一个外键指向问题表的主键。

或者我应该在问题表中只包含 5 列,其中包含答案以及正确答案吗?

根据我的阅读,一对一的关系是不正常的。

提前致谢!

I am designing a database that contains questions, answers and categories.

One question can belong to many categories, and one category can have many questions.
So this relationship is a many-to-many.

One question table
One category table
One table containing primary keys of the two tables above

So now my question is, is it necessary or "better" to store answers (a, b, c, d, e) in a own table named answers? Then the answer table has a foreign key to the primary key in the question table.

Or should I just have 5 columns in the question table containing the answers as well with the correct answer?

After what I read it is not normal to have a one - to - one relationship.

Thanks in advance!

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

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

发布评论

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

评论(2

沧桑㈠ 2024-10-19 03:59:15

制作一个单独的 answers 表。有两个好处:

  1. 它适合关系范式——一个问题与多个答案相关。
  2. 如果答案选项超过 5 个,您将具有灵活性。

答案表应如下所示:

ANSWERS
  answer_id (PK)
  answer_text
  is_correct
  serial_number
  question_id (FK)

现在,您将一个答案存储在一行中...以及序列号(a、b、c.. 或 1、 2,3.. 或 i, ii, iii,... 无论如何)您使用 serial_number 字段。 is_ Correct 指向正确答案。

您可以通过以下方式选择答案列表

  select * from `answers` where question_id = 123 order by serial_number asc

Make a separate answers table. There are two benefits:

  1. It fits in relational paradigm -- one question linked to many answers.
  2. You will have flexibility, in case, if options for answers goes beyond 5.

The answers table should look like this:

ANSWERS
  answer_id (PK)
  answer_text
  is_correct
  serial_number
  question_id (FK)

Now, you store one answer in one row... and for serial_number (a,b,c.. or 1,2,3.. or i, ii, iii,... whatever) you use serial_number field. is_correct points to correct answer/s.

You can select list of answers by

  select * from `answers` where question_id = 123 order by serial_number asc
橘香 2024-10-19 03:59:15

如果您确定总是会给出准确的答案(不多也不少),那么将它们作为问题表中的列是合理的。一般来说,它会简化您的查找过程并提高效率。但是,如果您有时或将来可能有不同数量的答案,那么您的答案应该有自己的表格。

If you know for certain that there will always be exactly give answers (neither more nor less), then it's reasonable to make them columns in the questions table. It'll simplify your look-up process and be more efficient, generally. However, if you sometimes have varying numbers of answers or might in the future, then your answers should have their own table.

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