vfkjdbsdih
vfkjdbsdih

Reputation: 11

SQL like button

So, I want to make an app that uses a voting system kinda like the like button on FB or the +1 button on Google. The one problem is that I have no idea how to design the database such that each of a large number of things has likes and dislikes from every user since it would be inefficient for each page that can be liked or disliked to have a like/dislike column for each user or for each user to have a like/dislike column for each page. How do I make the MySQL database process likes and dislikes?

Upvotes: 1

Views: 300

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 993173

Instead of creating multiple columns, you can use a link table:

user table

user_id | user_name
--------+----------
1       | alice
2       | bob
3       | charlie

page table

page_id | page_title
--------+--------------
1       | books
2       | food
3       | entertainment

user-page link table

user_id | page_id
--------+--------
1       | 1
1       | 2
2       | 3
3       | 3

The link table associates user_id values with page_id values. Note that one user can like more than one page, and one page can be liked by more than one user.

Upvotes: 3

Related Questions