数据库设计 - 文章、博客文章、照片、故事
我正在为一个网站设计一个数据库,该数据库将至少包含 4 种不同的对象类型(文章、博客文章、照片、故事),每个对象都有不同的足够数据要求来保证自己的表。我们希望用户能够针对任何这些类型发表评论。评论的数据要求很简单,并且与评论所关注的事物类型无关(即只是评论正文和作者的电子邮件)。
我希望避免为评论创建和管理 4 个以上单独表的冗余,因此我希望能够将所有评论保存在一个表中,可能通过 2 列指定关系:一列用于指定父实体,一列用于指定父实体为父行 ID。
但我不明白如何实现外键,因为外键在 2 个且仅 2 个表之间建立了关系(对吗?)。
考虑到所有这些,最好的方法是什么?
I'm designing a database for a web site that will have at least 4 different object types represented (articles, blog posts, photos, stories), each of which have different enough data requirements to warrant their own tables. We want users to be able to post comments for any of these types. The data requirements for comments are simple and independent of the type of thing the comment regards (ie just a comment body, and the author's email).
I want to avoid the redundancy of creating and managing 4+ separate tables for the comments, so I'd like to be able to hold all comments in one table, possibly specifying the relation via 2 columns: one to designate the parent entity and one for the parent row Id.
but I don't understand how, then, I would implement foreign keys, since foreign keys establish a relation between 2 and only 2 tables (right?).
So with all that in mind, what would be the best approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是为您的应用程序实现超类型/子类型表的一种方法。
首先,超类型表。它包含所有子类型共有的所有列。
接下来是几个子类型表。
这些子类型表中的 CHECK() 和 FOREIGN KEY 约束可防止行引用超类型中错误类型的行。它有效地在子类型之间划分 pub_id 值,保证任何给定的 pub_id 只能出现在一个且仅一个子类型表中。这就是为什么您需要对列对 {publications.pub_id,publications.pub_type} 使用 PRIMARY KEY 或 NOT NULL UNIQUE 约束。
评论表很简单。鉴于所有子类型都具有相同的结构,您可以引用超类型。
添加一点数据。
现在您可以创建一个视图来显示所有文章并解析连接。您可以对每个子类型执行相同的操作。
您可能更喜欢“published_articles”这样的名称,而不是“articles_all”。
要选择一篇文章及其所有评论,您只需左连接这两个表即可。 (但是请参阅下面为什么您可能不会这样做。)
实际上您可能不会为 Web 界面执行此操作,因为 dbms 必须返回文章的“n”份副本,其中“n”等于文章的数量评论。但在某些应用中这样做确实有意义。在有意义的应用程序中,您将为每种子类型使用一个可更新视图,并且应用程序代码大多数时候都会使用可更新视图。
超类型/子类型更常见的业务应用涉及“各方”(超类型)、“组织”和“个人”(子类型,非正式的公司和人。地址与上例中的“注释”一样,与超类型相关,因为所有子类型(组织和个人)都有地址。
Here's one way to implement supertype/subtype tables for your app.
First, the supertype table. It contains all the columns common to all subtypes.
Next, a couple of subtype tables.
The CHECK() and FOREIGN KEY constraints in these subtype tables prevent rows from referencing the wrong kind of row in the supertype. It effectively partitions the pub_id values among the subtypes, guaranteeing that any given pub_id can appear in one and only one of the subtype tables. That's why you need either a PRIMARY KEY or NOT NULL UNIQUE constraint on the pair of columns {publications.pub_id, publications.pub_type}.
The table for comments is simple. Given that it is to have the same structure for all subtypes, you can reference the supertype.
Add a little bit of data.
Now you can create a view to show all articles and resolve the join. You'd do the same for each of the subtypes.
You might prefer names like "published_articles" instead of "articles_all".
To select one article and all its comments, you can just left join the two tables. (But see below why you probably won't do that.)
You'd probably not actually do that for a web interface, because the dbms would have to return 'n' copies of the article, where 'n' equals the number of comments. But it does make sense to do this in some applications. In applications where it makes sense, you'd use one updatable view for each subtype, and application code would use the updatable views most of the time.
The more common business application of a supertype/subtype involves "Parties" (the supertype), "Organizations" and "Individuals" (the subtypes, informally companies and people. Addresses, like "comments" in the example above, are related to the supertype, because all the subtypes (organizations and individuals) have addresses.
您可以在数据库设计中使用超类型/子类型来避免该问题。为图像、视频、注释创建一个超级类型,然后链接到该超级类型。将所有公共列保留在超类型表中。
以下是一些带有模型的类似问题/答案的链接:
You can use super-type/subtype in DB design to avoid that problem. Create a super-type for images, video, notes and then link to the super-type. Keep all common column in the super-type table.
Here are few links to several similar questions/answers with models:
在我看来,你最好有 4 个以上单独的表格来发表评论。或者你可以有连接表。一张表包含所有评论...例如:博客表、评论表、blog_comment 表。这将允许您拥有外键。
In my opinion you are better off having 4+ separate tables for the comments. Or you could have join tables. One table for all the comments... ex: blog table, comment table, blog_comment table. this would allow you to have your foreign keys.