Database Normalization and Nested Lists -- Cannot Think of a Solution
I am trying to implement a system on my website similar to that of Facebook's "Like" feature. Where users can click a button which counter++
's. However, I have run into a problem in terms of efficiently storing data into my DB.
Each story has it's own row in the stories
table in my DB with the columns like
and users_like
.
I want each person to only be able to like the story once. Therefore I need to somehow store data that shows that the user has, in fact, like++'
d the post.
All I could thing of was to have a column named users_like
and then add each user, fol开发者_JAVA百科lowed by a comma, to the column using CONCAT
and then using the php function to explode
the data.
However, this method, as far as I know, is in the opposite direction of database normalization.
What is the best way to do this and I understand "best" is subjective.
I cannot add a liked
flag to the user
table because there will be a vast number of stories the person could 'like.'
Thanks
You need a many to many table in your database that will store a foreign key to the stories
table and a foreign key to the user
table. You put a constraint on this table saying that the story fk - user fk combo must be unique.
You now don't even have to have a like
column, you just count the number of rows in the many to many table corresponding to your story.
精彩评论