Mysql 轮询数据库模式

Mysql 轮询数据库模式,mysql,database-schema,Mysql,Database Schema,轮询的最佳数据库模式是什么?一对多关系对这有好处吗?我想有两张桌子: poll_questions int id varchar body datetime created_at datetime updated_at poll_answers int id varchar body int votes default 0 int question_id (foreign key to poll_questions.id)

轮询的最佳数据库模式是什么?一对多关系对这有好处吗?我想有两张桌子:

poll_questions
    int id
    varchar body
    datetime created_at
    datetime updated_at

poll_answers
    int id
    varchar body
    int votes default 0
    int question_id (foreign key to poll_questions.id)
    datetime created_at
    datetime updated_at
然后还有第三个表格,用于跟踪谁对答案投了票,因此用户只能投票一次:

poll_voting_history
    int id
    int question_id (foreign key to poll_questions.id)
    int answer_id (foreign key to poll_answers.id)
    int user_id (foreign key to the id in the users table)
    datetime created_at
    datetime updated_at

你的想法是什么?我想得对吗?

模式看起来不错,是的,您需要这样做。

注意:民意测验答案表的“投票”列不是必需的。投票可以通过查询投票历史记录表来确定。

我认为投票历史记录中的问题id也不是必需的,因为每个答案都指向一个问题,因此,根据答案id,您可以获得它所属的问题id。

我想添加投票问题和投票答案的状态,以防我们需要在给定日期和时间内激活或禁用当前投票问题

此特定架构的用户是否提交自己的自定义答案?或者从预先生成的一组答案中?如果您想让答案排序,您可以在poll\u答案中添加一个
order
列。选票列基本上是计算字段。投票结束后,我们从poll\u voting\u history表中统计选票,并将其存储在poll\u answers表中的votes列中。如果我们删除选票列并将特定的投票显示给然后,用户每次点击数据库一次又一次地计算投票(性能问题),所以删除投票列不是一个好主意。在第三张表中加入已确定的投票,则成本将适用。您完全正确。我认为我们使用它们是因为我们需要显示投票/问题和他们的投票历史,然后我们可以在投票/投票历史中使用投票id/问题id以汇总或简单的方式显示他们的历史。我们可以很容易地提及这项投票。