我想设计一个名为'post'
的超类型和两个子类型'blog_post'
和'image_post'
。
id
,title
,time_posted
。 'blog_post'
持有thumbnail
,body
。 'image_post'
持有image_path
,category
。 如何在MySQL中实现此模型?
如何在此模型中插入数据以建立每个子类型的完整帖子?
答案 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:
您将有一个约束来确保填充缩略图和正文或image_path和类别。 (MySQL缺少检查约束,因此您可以在此之前编写插入/更新触发器。)
2)拥有您加入的父表和子表:
还有其他方法可以解决此问题,例如只使用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。
您选择的模型取决于您希望使用表格和个人偏好的方式。