1

I know, there already is similar questions, but I can't find a good answer at my problem.

I'm developing a social network, I have all my users in a table, but each users must be able to save multiple post from the website.

My Question is:

How can I store all the saved post for each user without creating a new table for each users. I know this is bad to do this, so I'm looking for an alternative

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
lvminia
  • 13
  • 3

2 Answers2

1

If I understand you correctly, you just need a middle table (for example names user_post) that have 2 columns : user_id and post_id

this is a simple implementation of a many-to-many relationship

Hamid Ghasemi
  • 880
  • 3
  • 13
  • 32
  • You're right, I didn't though to do only one middle table with all users and posts just for the saved posts, thanks for your help ! – lvminia Mar 06 '17 at 13:37
0

You only need a table to store all posts call it posts table with structure like this:

|post_id|user_id|title|content|etc..|

So you can do something to save posts and identify them by user_id. It's called one-to-many relationship, you can search more about database relationships.

Đào Minh Hạt
  • 2,742
  • 16
  • 20
  • I already have a table like this one, but how can I say that a particular user, has saved 5 posts from my posts table on his profile for example, and retrieve these only 5 posts – lvminia Mar 06 '17 at 13:33
  • That is `many-to-many` relationship, you can create `saved_posts` table with structure like this `user_id|post_id`. So if user save 5 posts just insert 5 records there – Đào Minh Hạt Mar 06 '17 at 13:36