我正在研究一個Java web應用程序中的一個小功能,它非常類似於此處在stackoverflow.com上的投票上/下功能。我有一個如下所示的數據庫:確保數據庫中每個用戶,每個頁面的單一投票數
VOTE TABLE
id (bigint): surrogate primary key
question_id (bigint):: the question the vote is for
vote_type (int): whether the vote is up or down
user_id (varchar): the username of the person who the vote belongs to
我想確保每個人每個問題只有一個投票。執行此操作的最佳方式是什麼?如何用我上面描述的數據庫模式強制執行它?
我目前遇到的問題是,當用戶只需要一次投票時,用戶關閉兩張投票請求,然後數據庫包含該用戶的2張「投票」。
從併發安全和效率的角度來看,這是一個非常糟糕的主意。 – 2011-05-08 19:38:53