SQL Schema 允许用户向各种表添加注释 [英] SQL Schema to allow users add comments to various tables

查看:40
本文介绍了SQL Schema 允许用户向各种表添加注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在构建一个网站,我将拥有标准的 CMS 表格,例如文章、博客、投票等.我想让用户对这些项目中的任何一个发表评论.所以我的问题是,我是否需要为每个表创建单独的评论表(例如 ArticleComment、BlogComment、PollComment),或者我可以制作一个可以与任何表一起使用的通用评论表吗?什么对人们有用?

So, I'm building a website and I'm going to have standard CMS tables like Article, Blog, Poll, etc. I want to let users post comments to any of these items. So my question is, do I need to create separate comments tables for each (e.g. ArticleComment, BlogComment, PollComment), or can I just make a generic Comment table that could be used with any table? What has worked for people?

方法一:多评论表

  • 文章{ArticleID [PK]、标题、FriendlyUrl}
  • ArticleComment {ArticleCommendID [PK]、ArticleID [FK]、Comment}
  • 博客{BlogID、标题、发布日期、类别}
  • BlogComment {BlogCommendID [PK]、BlogID [FK]、Comment}
  • 投票{PollID, Title, IsClosed}
  • PollComment {PollCommentID [PK]、PollID [FK]、Comment}

方法二:单条评论表

  • 文章{ArticleID [PK]、标题、FriendlyUrl}
  • 博客{BlogID、标题、发布日期、类别}
  • 投票{PollID, Title, IsClosed}
  • 评论{CommentID [PK]、ReferenceID [FK]、评论}

推荐答案

似乎有两种主要的方式将 OO 继承映射到关系数据库:

There seem to be two major ways of mapping OO-inheritance to relational databases:

  1. 从父类和所有子类中获取所有属性并将它们放入表中,并附上这是哪个类?"场地.每个对象被序列化为一张表中的一行.

  1. Take all the attributes from the parent class and all the child classes and put them in the table, together with a 'which class is this?' field. Each object is serialized as one row in one table.

为父类创建一张表,为每个子类创建一张表.父类表的表包含这是哪个类?"场地.子类表包含一个指向父类表的外键.每个对象序列化为父类表中的一行,子类表中的一行.

Create one table for the parent class and one table for each child class. The table for the parent class table contains the 'which class is this?' field. The child class table contains a foreign key pointing to the parent class table. Each object is serialized as one row in the parent class table and one row in the child class table.

方法一并不能很好地扩展:它很快就会出现许多可为空的字段,几乎总是空的,以及可怕的CHECK约束.但对于小类层次结构来说,这相当简单.

Method one doesn't really scale well: it quickly winds up with lots of nullable fields, almost always null, and scary CHECK constraints. But it is fairly simple for small class hierarchies.

方法二的规模要好得多,但工作量更大.它还会在您的架构中生成更多的表.

Method two scales much better, but is more work. It also results in many more tables in your schema.

我建议您查看文章/投票/博客表的方法二 - 对我来说,它们听起来像是 Content 或其他东西的子表.然后,您将有一个非常清晰且易于添加注释的位置:Content.

I suggest taking a look at method two for your Articles/Polls/Blogs tables — to me, they sound like child tables of a Content or something. You will then have a very clear and easy place to attach comments: to the Content.

这篇关于SQL Schema 允许用户向各种表添加注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆