Skip to content Skip to sidebar Skip to footer

Sql Database Structure For Like And Dislike

I am new to web programming and need some expert advise. I am using SQL database for one of my website project. My website will display a list of topics with a Like and Dislike but

Solution 1:

If you have a table of Topics and a table of Users, you would add a table of Likes which links to both. Something like this:

User----------
ID (int, PK)
Name (string)
etc...

Topic
----------
ID (int, PK)
Title (string)
etc...

Like----------
ID (int, PK)
UserID (int, FK to User.ID)
TopicID (int, FK to Topic.ID)
IsLike (boolean)
etc...

So any time a user "likes" something you add a record to that table setting IsLike to true. If they "dislike" something then you add a record to that table setting IsLike to false. You can change around the terminology/names/types/etc. but the general idea is the same. A "like" becomes a linking record between a User and a Topic.

So when displaying the topic, you just select the count of records from the linking table which are associated with that topic. And when displaying a user you select the records from the linking table which are associated with that user.

Solution 2:

enter image description here

This is a simple schema you could use. isLike is a boolean, true if it's a like, false if it's a dislike.

To query the total likesdislikes by topic

SelectCount(*), Topic,isLike FROM LikesDislikes GROUPBY Topic,isLike

For all of a user's liked topics

SELECT topic FROM likesdislikes WHERE userName ='user'AND isLike =true;

And so forth.

Solution 3:

You could have three SQL tables:

Topics Ratings Users

You could then link the Users table via the Ratings table to the Topics table and do a query:

select * from Topics where RatingsUserID = UsersUserID

(pseudo code)

Post a Comment for "Sql Database Structure For Like And Dislike"