Question

i am writing an application that keep tracks of the blogs read by a user

Currenlty I have two tables in the database one is for different categories a blog can belong to like technology, music, cinema etc.

Schema of the table is - CATEGORIES (id , name)

id -- > primary key

name -- > string (name of the category)

The second table stores the blogs

Schema - BLOGS (id, blog_name, blog_url,category)

id -- > key

category -- > foreign key referencing CATEGORY table

Now i need to design the table for storing the user's info like name, email, password, and the list of blogs he/she reads

The problem is that a user can like any number of blogs and a single blog can be liked by any number of users.

I need design the user's table in such way as to easily facilitate the following queries in particular.

-> given a blog the list of all users that like the blog

-> given a user the list of all blog that user likes

please provide me some valueble suggesitions

Était-ce utile?

La solution

What I would do in this situation is the following:

Do not put the blog a user likes in the main User table. Just keep the user table for the user information (so email, address, name, etc). Because you will have potentially many-to-many relationships between your users and blogs, have a separate table which holds the user id and the blog id:

CREATE TABLE User_Blog_Link
(
    userId int,
    blogId int
)

This will allow you to do the queries you mention above.

SELECT userId
FROM User_Blog_Link
WHERE blogId = <x>

SELECT blogId
FROM User_Blog_Link
WHERE userId = <y>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top