Just looking for some suggestions on how to approach the database design for this.
On my site a user can get points for performing different activities. Currently there are 3 activities for which I award points – but the design has to be scalable where I can add other activities for awarding points as well.
So today – the user gets points
1) When he adds a new store he gets 10 points (Store information is stored in STORE table)
2) When he answers a question he gets 7 points (Questions/answers are stored in a ANSWERS table)
3) When he refers friends who join the site he gets 5 points
So this is what I have so far – but it doesnt look right 🙂
Points_Table
point_id
user_id
action (This will capture for what action the points are being given)
points
I should be able to deduce from the database that this user got xxxx points for creating this store or for referring these friends or for answering this question.
The above design obviously doesn’t take care of that.
Thanks for your tips
Don’t store the points at all. Just do queries on this view. That way it will be robust in the face of arbitrary changes.
Edited to add:
This follows the normal database design principles. All database normalization can be summed up with this: don’t repeat data.
The inclusion of a points table essentially just repeats information that the rest of the database already contains. Therefore, it should be omitted. This is made clear when considering the lengths to which one must go to maintain the consistency of the data. Without worrying about the points, if a referral is attributed to Alice, but later it is determined that Bob should receive the credit, this will require a change to a single field in a single row of a single table. If a points table is included, then it must also be updated in some way. If points summaries are being stored, then may God have mercy on your database.
Rather than storing the points, a view or stored procedure may be used instead, and then the points system is adjusted automatically when the data changes.
I think a view is perferrable to a stored procedure in this case, because there are many ways in which the points data may need to be analyzed, and this provides the most flexibility, and it gives the optimizer the best chance at identifying the optimal path.
You may select from the view by user_id or by action_type or sum the points column to get totals for a user or for all users. Look at the fun you can have, and it’s practically free!