user3927675
user3927675

Reputation:

How would I implement a star rating system into my Database design?

I am currently making an Android app where I want to allow users to rate posts, posted by other users.

ERD model of Database

This is my current design, however, would I include Rating as a separate table or included in the Media upload table?

If it is a separate table would it be something like this?

Rating table: (Rating, PostID, Rating_Count, Average_Rating)

If anything else looks wrong with my design that would also be appreciated!

Upvotes: 3

Views: 10841

Answers (3)

Amirex
Amirex

Reputation: 281

you can use stretcher like this

enter code here
        $table->bigIncrements('id');
        $table->bigInteger('user_id')->unsigned();
        $table->ipAddress('ip')->nullable();
        $table->integer('star');
        $table->string('type');           
        $table->string('title');
        $table->string('average')->nullable()->default(0);
        $table->integer('count_star')->default(0)->nullable();
        $table->float('star_avg')->default(0)->nullable();
        $table->float('unstar_avg')->default(0)->nullable();

Upvotes: 0

Big_Al_Tx
Big_Al_Tx

Reputation: 1016

It all depends on how you want the ratings to work, who gets to up-vote or down-vote, whether you track everyone that votes to keep from having multiple votes from the same person, etc.

This is what I would do: add a unique numeric (long?) RatingID field for each vote, link the PostID field to the Comments table, add a Rating (integer) field that is limited to values from 0 to 5 (or whatever range you prefer), and then calculate the average from all votes cast (delete the Rating_Count and Average_Rating fields). You could define a view to calculate the average ratings for each post to use for your routine for determining how to display it next to the Post.

Also, I would use an ID for each user, not use their names.

So my table would look like this:

RatingID, PostID, UserID, Rating

Also, to keep users from voting multiple times, the table would not allow multiple entries for the same PostID and UserID.

Upvotes: 4

Eric S
Eric S

Reputation: 1442

It depends on how you want to store the Rating.

If you want to track each individual rating. You will need a Rating table with the following:

Column
---------
RatingID (PK)
Username (FK) (UserDetailsID if you change the primary key of the UserDetails table)
PostID (FK)    
RatingScore
DateRated

In the app, you would then pull back every RankScore with that PostID and do the calculation based off a count of said PostID.

I would recommend the above method because it will allow you do more things with the Ratings in the future. Plus, you have a good structure to follow in place with your Comments table. ex. Show who Rated what and at what score.

If you want to attach it to the Post table, another table is not needed. You would simply add Rating_Count and Average_Rating to the table. In your app, you would than have to perform an update every time the Post is rated. You would have to pull the Rating_Count and Average_Rating, increment the Rating_Count by one, recalculate the Average_Rating and perform the update.

My second suggestion is less flexible if you ever want to enhance your Rating setup though.

Upvotes: 1

Related Questions