質問

投票に最適なデータベース スキーマは何ですか?これには 1 対多の関係が適していますか?テーブルを2つ用意しようと考えています。

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

次に、誰が回答に投票したかを追跡するための 3 番目のテーブルもあり、ユーザーは 1 回だけ投票できます。

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

どう思いますか?私はそれについて正しく考えていますか?

役に立ちましたか?

解決

スキーマは良さそうですが、そうする必要があります ユーザーの投票も追跡します.

他のヒント

注:poll_answersテーブルの「票」の欄は必要ありません。投票Poll_voting_historyテーブルを照会することによって決定することができる。

私はそうanswer_idに基づいて、あなたはそれが属するquestion_idを得ることができ、質問に対する各回答のポイント以来、poll_voting_historyでquestion_idが同様に必要ではないと思われる。

を追加したいと思います 状態 指定された日時に現在のアンケートの質問を有効または無効にする必要がある場合に備えて、poll_questions と vote_answers も同様に作成します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top