I’m currently working on a blog for a college news organization. Each post, though, will represent a full show, with multiple contributors and multiple titles.
For example, a post might have three news stories, each with its own title and some contributors for each:
- “Story 1” by (id1) and (id2)
- “Story 2” by (id3)
- “Story 3” by (id4) and (id5)
So for each post, there would be an index (1, 2, 3…) for each individual story, a VARCHAR for the title, and id’s that represent contributors, whose details are stored in another “contributors” table. The problem is that I don’t know how many stories there will be, or how many contributors there will be per story. It could range from ~3 at the least to up to 6. In case our show expands in the future, I’d like to have the capability to scale up to even more than 6 posts, too.
I want to represent this structure concisely in a mySQL column, but I’m not sure how to do that. One solution would be to create another mySQL table to save the details for each individual story, but I’d prefer to avoid that hassle. The ideal solution would be if I could somehow create an “array” within a mySQL column, which could store (for each story) an index, a string, and multiple id’s to show who the contributors are.
Is this possible, or will I have to create a new table to keep track of each story?
Don’t use a column – use a table. It can be a simple InnoDB table which doesn’t really hurt performance at all. Define a combined primary key
(story_id, contributor_id)and insert all contributions in that table.What you name in your question is called a M:N table. Don’t ever go there – it’s a very bad thing to do and is, in fact, nearly impossible in relational databases.