【问题标题】:advice regarding the database design关于数据库设计的建议
【发布时间】:2014-04-03 10:29:23
【问题描述】:

我正在编写一个应用程序来跟踪用户阅读的博客

当前我在数据库中有两张表,一张用于博客可以属于的不同类别,例如技术、音乐、电影等。

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

id -- > primary key

name -- > string (name of the category)

第二个表存放博客

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

id -- > key

category -- > foreign key referencing CATEGORY table

现在我需要设计一个表格来存储用户的信息,例如姓名、电子邮件、密码和他/她阅读的博客列表

问题是一个用户可以点赞任意数量的博客,而一个博客可以被任意数量的用户点赞。

我需要设计用户表,以便特别方便地进行以下查询。

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

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

请给我一些有价值的建议

【问题讨论】:

    标签: database-design database-schema


    【解决方案1】:

    在这种情况下我会做如下:

    不要将用户喜欢的博客放在主用户表中。只需保留用户表中的用户信息(如电子邮件、地址、姓名等)。因为您的用户和博客之间可能存在多对多关系,所以有一个单独的表来保存用户 ID 和博客 ID:

    CREATE TABLE User_Blog_Link
    (
        userId int,
        blogId int
    )
    

    这将允许您执行上述查询。

    SELECT userId
    FROM User_Blog_Link
    WHERE blogId = <x>
    
    SELECT blogId
    FROM User_Blog_Link
    WHERE userId = <y>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多