I am creating a databsse for my site, which requires that multiple attachments be uploaded by users in different pages. I wish to keep all those attachments in a single table and reference that table from other tables, where the attachment would be required.
Now the issue is how do I do this? I have not been able to work out the logic of it as yet. What I have in mind is:
attachment_tbl:
attach_id
file_name
description
item1_id -> FK
item2_id -> FK
item1_tbl:
item1_id
attachment1
attachment2
item2_tbl:
item2_id
attachment1
attachment2
attachment3
Now how do I save all these attachments in the same table, without having multiple FK in the attachment_tbl, as if I add another item3_tbl, which has attachments, would it mean another FK to be added to the attachments_tbl?
Is there some other simpler and better way to do the same?
Add a relationship table, which connects items and attachments, with a one to many relation:
For each attachment, there will be a row in the item_attach table:
When you want to see the attachments of an item, you must join the tables:
See also SQL Fiddle