设计评论表

1 sql-server database-design

基本上我想创建一个评论系统,其中评论可能有父母也是评论但我也希望他们可能有父母可能是其他东西,如用户或产品(即,我希望能够评论产品,用户,其他评论,或几乎任何资源)

我该怎么办?

目前的表格:

标签,产品,用户,评论

编辑 - 这将是一个有点高流量的网站,所以我不能让它做各种疯狂:-)

Jee*_*Bee 8

您想要对产品,用户,评论等发表评论吗?或者查找评论所指的产品,用户,评论等?

对于前者,我会有表格将事情与他们的评论联系起来:

create table join_products_comments (
   product_id int (unique, i.e., one thread of comments per product),
   comment_thread_id int
);

create table join_users_comments (
   user_id int (unique, i.e., one thread of comments per user),
   comment_thread_id int
);
Run Code Online (Sandbox Code Playgroud)

其中comment_thread只是对每个注释引用的线程的引用:

create table comment_threads (
    thread_id int (PK),
    thread_name nvarchar2(256),
    created datetime
);

create table comments (
    comment_id int (PK),
    comment_thread_id int (FK),
    parent_comment_id int (FK),
    user_id int (FK), -- person who posted the comment
    comment text,
    created datetime
);
Run Code Online (Sandbox Code Playgroud)

因此系统中的每个可评论实体都会有一个连接表和一个comment_thread,只等待急切的用户添加注释.或者你可以直接链接到根注释,而不用那个间接.