如何存储变量字段?
我有一个数据库,其中有评论、投票、画廊、图像等...... 例如,可以使用相同的表单对画廊和图像进行评论,然后我将其存储在单个表中,评论。
我想知道如何在评论表中存储这种类型信息? 使用 ENUM 类型、针对类型表的外键、插入时硬编码等。?
I've a database where I've comments, votes, galleries, images, etc...
For examples, galleries and images can be commented using the same form, then I'll store it in a single table, comment.
I'm wondering how would store this type information in the comment table?
Using a ENUM type, a foreign key against a type table, hardcoded on insert, etc.. ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
针对类型表的外键将获得我的投票。像这样的东西:
Foreign key against a type table would get my vote. Something like this:
可能有几种方法可以在保留外键的同时进行设计。
第一个是基本上为每种类型都有一个注释表;将它们分开。对于每个评论,都有一个指向父图库或图像的外键,具体取决于表。
第二个是拥有一个表(例如可评论的表),其中包含所有可评论的内容的通用数据。每个图库或图像都有一个外键,指向可评论中唯一对应的行。评论表中的每一行都有一个可评论的外键,以及评论文本、日期等。
无论如何,您可能会发现这很有用,因为您最终会需要集合的元数据 评论作为一个整体...例如,指定评论是禁用、仅对朋友启用还是对所有人启用。
编辑选项 3 与选项 1 类似,但不是为每个可评论类型拥有完整的单独评论表,而是为每个可评论类型拥有单独的关联表。
我认为比选项 1 好得多。
There are probably a couple of ways you could design this while retaining foreign keys.
The first is to basically have a comment table for each type; keep them separate. For each comment have a foreign key to the parent gallery or image, depending on the table.
The second is to have a table (say commentables) that contains common data for anything that is commentable. Each gallery or image has a foreign key to a unique corresponding row in commentables. Each row in the comment table has a foreign key to the commentable, along with the text of the comment, date, etc.
You'll probably find this useful anyway since you'll eventually want metadata for the collection of comments as a whole...e.g. to specify whether commenting is disabled, enabled for friends only, or enabled for everybody.
Edit Option 3 is like option 1 but instead of having full separate comment tables for each commentable type, just have separate associative tables for each commentable type.
Much better than option 1 in my opinion.