Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a mantainable way of saving "star rating" in a database?

I'll use the jQuery plugin for presenting the user with a nice interface

The request is to display 5 stars, up to a total score of 10 (2 points per star).
By now I thought about using 7/10 as a format for that value, but what if at some point in the future I'll receive a request like

We would like to give users more choice, let's increase the total score to 20 (so that each star contributes with a maximum of 4 points)

I'll end up with a table with mixed values for the "star rating" column: some will be like 7/10 while others will be like 14/20.

Is it ok for you to have this difference in the database and deal with it in the logic layer to have it consistent? Or is preferred another way so that querying the table will not result in inconsistent results outside the application?
Maybe floating point values could help me, is it better to store that value as a number less than or equal to one? So in each of the two examples the resulting value stored in the database would be 0,7, as a number, not a varchar, which can be queried also outside the application.

What do you think?

like image 294
Alberto Zaccagni Avatar asked Apr 20 '10 13:04

Alberto Zaccagni


People also ask

What is the concept of star rating?

star rating in British English (stɑː ˈreɪtɪŋ ) a rating indicated by stars (usually 1–5), the highest number of stars indicating the best quality, highest amount etc. Some suncare products carry a star rating for UVA protection. Collins English Dictionary.


1 Answers

There are three options:

  1. Store the value as a fraction (which is, honestly, what I'd likely go with since it's the lowest maintenance
  2. Store the value in two columns, one for the rating and one for the maximum
  3. Store the value as a single column with an assumed maximum, then update all values when that maximum changes.

The first option is the most portable, since the maximum value and the representation of the rating are entirely client-side (client-side as far as the database is concerned, not client-side as in it's done at the user's machine).

The second just seems arbitrarily complex, unless you wish to continue to display a 7/10 rating as a 7 with 10 stars rather than a 14 with 20 stars.

The third requires the least up-front work, but requires a massive database update when changes are made to the rating scale.

Just to reiterate, I recommend storing the value as a float or double in the database and applying whatever scale is appropriate on the client side..

like image 166
Adam Robinson Avatar answered Oct 13 '22 23:10

Adam Robinson