Reputation: 118
I have got a website where users can post photo's, video's, links, articles and events.
But now comes the hard part (for me): I want the users to be able to attach photo's, videos and links to events and/or articles.
What is best practice for this? Should i create another table where i link them to each other:
[media2articles]
m_id, m_type, media_id, m_article_id
[media2events]
m_id, m_type, media_id, m_events_id
or should i add some fields to my current tables like this:
[videos]
v_id, v_file, **v_article_id, v_event_id**
Or is there any other way ?
Upvotes: 0
Views: 295
Reputation: 51197
It depends on the type of relationship your data has. Also keep in mind that you want to think about the relationship it'll have in the future because changing the schema can be painful.
This is basic relational modeling; reading up on it will greatly aid you in database design.
Upvotes: 1
Reputation: 63970
I would create the "media" tables for this and link them. Adding columns to the existing table is a terrible design. You won't be able to support 1:n relationships unless you continue adding more columns to the table.
Upvotes: 0
Reputation: 48387
If one media item might be associated with different events, and different events might share the same media item, then you'd need an additional table to decompose the N:M relation between the 2 current tables.
Upvotes: 0
Reputation: 414006
Generally you'd use a "link" table, with "to_" and "from_" references (or whatever is appropriate to your data model). That will allow a photo to be linked to more than one article, or vice-versa.
Upvotes: 0