2010-02-15 103 views
19

投票的最佳數據庫模式是什麼?一對多的關係對此有好處嗎?我想有兩個表:投票數據庫模式

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 

你有什麼想法?我在想它嗎?

+0

是您提交自己的自定義答案的特定模式的用戶嗎?或來自預先生成的一組答案? – cwiggo 2014-10-22 14:28:21

+0

如果您想要訂購答案 – WoLfPwNeR 2017-02-10 21:44:27

回答

11

模式看起來不錯,是的,你需要track the user votes as well

+0

我可以在poll_answers中添加一個'訂單'列我用第三個表更新了我的問題:) – 2010-02-15 08:51:45

5

注意:poll_answers表中的「votes」列是不必要的。投票可以通過查詢Poll_voting_history表來確定。

2

我認爲poll_voting_history中的question_id也不是必須的,因爲每個答案都指向一個問題,所以根據answer_id可以得到它所屬的question_id。