一个图像表可以服务两个外部表?
我有这个图像表
,
image_id
image_title
image_description
image_source
我希望这个图像表
与页表
关联,并且有时与关联 comment table
页表,
page_id
...
...
评论表,
comment_id
...
...
是否应该将page_id
和comment_id
的外键放在image<中/代码>表?
image_id
image_title
image_description
image_source
page_id
comment_id
或者我应该为每个目的创建一个 map
表?
一个用于图像和页面的 map
表
image_id
page_id
以及另一个用于图像和评论的 map
表
image_id
comment_id
对于这种场景,什么是最佳实践?
当image
表格行增加map idea,image
表格会变慢吗? (这是另一个主要问题)?
目前我这样做,
图像页面
表,
image_id
image_title
image_description
image_source
page_id
图像评论
表,
image_id
image_title
image_description
image_source
comment_id
但这看起来不太好,因为我重复这些列,而且它看起来不是动态的。 有什么想法吗
?
编辑:
有时图像与页面相关联,有时与评论相关联。上传图片有时是针对文章本身,有时是针对文章下方的评论。
image_id image_title page_id comment_id
1 a 1 NULL
2 b NULL 1
3 c 1 NULL
4 d 1 NULL
I have this image table
,
image_id
image_title
image_description
image_source
and I want this image table
to associate with page table
and sometimes with comment table
page table,
page_id
...
...
comment table,
comment_id
...
...
Should I put the foreign key of page_id
and comment_id
in image
table?
image_id
image_title
image_description
image_source
page_id
comment_id
Or should I create a map
table for each purpose?
a map
table for image and page
image_id
page_id
and another map
table for image and comment
image_id
comment_id
What is the best practice for this kind of scenario?
Will the image
table get slow if I use map
idea when the image
table row increase (this is another main concern)?
Currently I do this,
image page
table,
image_id
image_title
image_description
image_source
page_id
image comment
table,
image_id
image_title
image_description
image_source
comment_id
but this doesn't look good as I am repeating the columns and it does not appear to be dynamic...
Any thoughts?
EDIT:
Sometimes the image is associated with the page and sometimes with comment. The upload image sometimes is for the article itself and sometimes is for the comment under the article.
image_id image_title page_id comment_id
1 a 1 NULL
2 b NULL 1
3 c 1 NULL
4 d 1 NULL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您现在的设计存在局限性。例如,图像不能同时与页面和评论相关。除非它的数据在两个Image表中重复。其次,更重要的是(正如您所说,它看起来不太好),您有两个具有几乎相同的列和功能的表。
因此,拥有一个
Image
表和两个映射表看起来是更好的设计。如果您稍后想要存储有关这些映射的更多(和不同)信息 - 例如,您可能需要添加 Last_changed 时间戳和PageImage
的大小限制(每个图像最大 100KB)以及数量限制对于CommentImage
(每个评论最多 3 个)- 您不必更改整体设计,只需更改这 2 个特定的映射表即可。但是,您可能会发现自己创建了一堆映射表,例如一个用于
UserImage
,另一个用于MessageImage
,另一个用于EmailImage
,都具有相同(或几乎相同)的结构 - 这再次导致代码重复。为了消除这种重复,您可以使用超类型/子类型模式。创建一个超类型
Entity
,其中包含Page
、Comment
、User
、Message
、< code>Email 作为子类型(每个子实体都将有一个主键,该主键也是超类型Entity
的外键)。然后,您的映射可以轻松地合并到一个
EntityImage
表中(带有到Image
和Entity
的 FK)。There are limitations with the design you have now. For example, an image cannot be related to both a page and a comment. Unless its data is duplicated in the two Image tables. Second and more important, (and as you say, it doesn't look good), you have two tables with almost identical columns and functionality.
So, having one
Image
table and two mapping tables looks much better design. If you later want to store more (and different) information regarding these mappings - for example, you may want to add a last_changed timestamp and a size limitation forPageImage
(max 100KB per image) and a number limitation forCommentImage
(max 3 per comment) - you don't have to change the overall design, only these 2 specific mapping tables.You may, however, find yourself creating a bunch of mapping tables, say one more for
UserImage
, another forMessageImage
and yet another one forEmailImage
, all having identical (or almost identical) structure - and this again leads to duplication of code.To eliminate such duplication, you can use the supertype/subtype pattern. Create a supertype
Entity
, withPage
,Comment
,User
,Message
,Email
as subtypes (every one of those child entities will have a Primary Key that is also a Foreign Key to the supertypeEntity
).Then, your mappings can easily be combined in one
EntityImage
table (with FKs toImage
andEntity
).您的图像表没问题。您应该创建一个 Comment 表和一个 Page 表,并将 ImageId 存储在这些表中。
编辑:(没有阅读所有评论)
根据页面和评论可以有多个图像的要求,唯一的方法是创建两个链接表,一个将评论链接到图像 以及将页面链接到图像的链接。
Your Image table is fine. You should create a Comment table and a Page table and store the ImageId in those tables.
EDIT: (Didn't read all the comments)
Based on the requirement that Pages and Comments can have multiple images then the only approach is to creat two linking table one that links the Comments to Images and one that links the Pages to Images.