I am working on a Review System and I am having doubts about how to handle storing data in the database.
For this review system, there are three different ratings the users can input, I will call them ratingOne, ratingTwo and ratingThree. My current solution is having three different columns in the database called, respectively one_rating, two_rating and three_rating (where one, two and three are business informations that I prefer not to disclose).
Is there a better way for storing this data? For example, in the API request, this is handled as a sub-object in the JSON i.e. inside the POST json there is a second object that is as follows
[...],
"ratings": {
"one": 1,
"two": 2,
"three": 3,
},
[...]
Best Answer
I would create two extra tables,
ratings
andrating_types
.The
rating_types
table has aname
andid
field. It would have just a few records:The
ratings
table would simply tie things together, having foreign keys torating_types
andreviews
:This makes it easier to add extra "things" on which you can rate. It also makes calculations easier.
For more details, you can read up on database normalization
If you need to be able to handle different "rating types" in different ways in your code, you could expand
rating_types
with acode
field that could have a value you can check against.Just a side note, but I would also re-structure the JSON API like so: