MYSQL:如何创建继承表设计

时间:2015-04-24 02:18:15

标签: mysql inheritance

我想设计一个名为'post'的超类型和两个子类型'blog_post''image_post'

  • 超类型包含idtitletime_posted
  • 'blog_post'持有thumbnailbody
  • 'image_post'持有image_pathcategory

如何在MySQL中实现此模型?
如何在此模型中插入数据以建立每个子类型的完整帖子?

2 个答案:

答案 0 :(得分:1)

我认为,数据库设计中的继承只是告诉引用列的属性,如下所示:

[Shape] table:
Id bigint^
Name varchar(20)

[Circle] table:                       [Square] table
Id bigint^                            Id bigint^
ShapeId bigint*                       ShapeId bigint*
Radius int                            Width int

转到:

{{1}}

答案 1 :(得分:0)

不幸的是,表没有继承权。有两种常见的方法可以解决这个问题:

1)只有一个表并将列保留为null:

  • 表格帖子 id_post ,title,time_posted,thumbnail,body,image_path,category)

您将有一个约束来确保填充缩略图和正文或image_path和类别。 (MySQL缺少检查约束,因此您可以在此之前编写插入/更新触发器。)

2)拥有您加入的父表和子表:

  • 表格帖子 id_post ,title,time_posted,id_blog_post,id_image_post)
  • 表blog_post id_blog_post ,缩略图,正文)
  • table image_post id_image_post ,image_path,类别)

还有其他方法可以解决此问题,例如只使用blog_post和image_post,两者都包含列标题和time_posted。或者采取解决方案2并将id_post添加到blog_post和image_post以双重链接记录。或者将id_post添加到blog_post和image_post,并从帖子中删除id_blog_post和id_image_post。

您选择的模型取决于您希望使用表格和个人偏好的方式。

相关问题