I am creating a voting system for a Q&A site project in which if a user asks a question, he/she losses -5 points; answer a question +5, vote a question +1, etc. (kind of like SO and yahoo answers)
–>To create the basic arithmetic, I have a “users_points” table that relates the user_id and their total points.
+---+---------+
| 1 | 100 |
+---+---------+
| 2 | 54 |
+---+---------+
–>Basically if the users does certain task, it would + or – the points. How do I prevent users from say voting an answer up 100 times. ex: I want a user to be only able to vote once per question, etc.
You should create a votes table that has the columns:
user_id, question_id, delta. Delta is the value of the vote, which should be 1 or -1 (this way you can just do aSUM(delta)to find a question’s point value).To get the uniqueness, create a unique index on
(user_id, question_id).