简体   繁体   English

层次结构类别子类别(parentId)论坛数据库设计

[英]Hierarchy category subcategory (parentId) forum database design

I am making a forum database. 我正在建立一个论坛数据库。 The needed tables are the questions and the comments tables. 所需的表是问题和注释表。 The question table has an IsDeleted flag and the comments table has an IsDeleted flag (PS the comments table is based on the category - sub category hierarchy - each comment has a ParentId (if it's null then it's on the top of the hierarchy)). 问题表具有IsDeleted标志,注释表具有IsDeleted标志(PS注释表基于类别-子类别层次结构-每个注释都具有ParentId (如果为null,则位于层次结构的顶部))。

I am wondering what would be suitable when I am deleting a comment and deleting a question. 我想知道删除评论和删除问题时适合什么。 For example when I delete a question it's obvious that I will mark the question as IsDeleted (should I delete the comments also as IsDeleted ?). 例如,当我删除问题时,很明显,我会将问题标记为IsDeleted (我也应该将注释也删除为IsDeleted吗?)。 But when I am deleting a comment should I mark only the current comment as IsDeleted or the current comment and all it's descendants? 但是,当我删除评论时,我应该仅将当前评论标记为IsDeleted还是将当前评论及其所有后代标记为?

PS: Please support your advice with some sample code :) PS:请通过一些示例代码支持您的建议:)

You can use a database trigger and update posts under the thread (ParentId) as deleted easily. 您可以使用数据库触发器并更新线程(ParentId)下的帖子,因为它们很容易删除。 Just use an AFTER UPDATE trigger and check if IsDeleted column updated or not. 只需使用AFTER UPDATE触发器,然后检查IsDeleted列是否已更新。 So you can update posts as deleted too under that thread 因此您也可以在该线程下将帖子更新为已删除

But if you have analysis like how many posts are deleted because of spam, etc. it is better to keep a reason for deletion column. 但是,如果您进行了分析,例如由于垃圾邮件等原因删除了多少帖子,则最好保留一个删除原因列。 Or simply do not update child posts but to check their statu, always read the parent beside (which is not performing good always) 或者干脆不更新子帖子,而是检查其状态,请始终阅读旁边的父帖子(这并不总是表现良好)

Here is a sample trigger code for a two table solution 这是两张桌子解决方案的示例触发代码

create trigger post_delete on thread after update 
as
begin

update post
set isDeleted = 1
where threadId in (
    select d.threadId
    from inserted i
    inner join deleted d
        on i.threadId = d.threadId
    where i.isDeleted = 1 and
          d.isDeleted is null
)
end

I am adding meta data for the second case where all posts are stored in single table and adding recursive CTE within modified trigger below I hope it helps 我正在为所有情况都存储在单个表中的第二种情况添加元数据,并在下面的修改后的触发器中添加递归CTE,希望对您有所帮助

create table Posts (
    PostId int,
    ParentId int,
    Title nvarchar(256),
    Text nvarchar(max),
    IsDeleted bit
)

Following AFTER UPDATE SQL trigger works for SQL table like above 以下AFTER UPDATE SQL触发器适用于上述SQL表

create trigger posts_delete on posts after update 
as
begin

with tbl as (
    select d.PostId
    from inserted i
    inner join deleted d
        on i.PostId = d.PostId
    where i.IsDeleted = 1 and
            d.IsDeleted is null
), cte as (
    select * from Posts where PostId in (select PostId from tbl)
    union all
    select p.* 
    from Posts p
    inner join cte
        on cte.PostId = p.ParentId
)
update posts
set isDeleted = 1
where PostId in (
    select PostId from (
        select * from cte
    ) t
)
end
go

I tested the SQL script using following sample data 我使用以下示例数据测试了SQL脚本

insert into Posts select 1, null, 'Triggers','',NULL
insert into Posts select 2, null, 'Databases','',NULL
insert into Posts select 3, 1, 'DDL Trigger','SQL',NULL
insert into Posts select 4, 3, 'What is DDL?','DDL',NULL

select * from Posts
update Posts set IsDeleted = 1 where PostId = 3
select * from Posts

Results are as follows 结果如下

在此处输入图片说明

We currently have a similar model in our database and I would suggest only setting IsDeleted for the thing the user deleted and not subordinate objects. 当前我们的数据库中有一个类似的模型,我建议只为用户删除的对象设置IsDeleted,而不是从属对象。 Why? 为什么? Restoration. 恢复。 If you mark comments as deleted when the user deletes a question, how do you properly restore the proper comments if you restore the question? 如果在用户删除问题时将注释标记为已删除,那么如果恢复问题,如何正确恢复正确的注释?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM