问题
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
回答1:
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 for PageImage
(max 100KB per image) and a number limitation for CommentImage
(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 for MessageImage
and yet another one for EmailImage
, 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
, with Page
, 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 supertype Entity
).
Then, your mappings can easily be combined in one EntityImage
table (with FKs to Image
and Entity
).
回答2:
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.
来源:https://stackoverflow.com/questions/9381261/an-image-table-to-serve-two-foreign-tables