I have design problem, which I would love to get a hand with. I normally don't have problems with designing databases, but this one gives me some trouble. :) I'm making a program which is basically a task-manager, where you can create Activities and Events. An Activity may or may not have an Event associated with it, and an Event will have one to many associations. How do I design the Event database? Since every Event can have more than one association, will I need to make a table for each Event? Where the tablename is it's unique name? I'm completely blank. Thanks
3 Answers
Since every Event can have more than one association, will I need to make a table for each Event?
No. You need some version of:
Activity
--------
ActivityID
ActivityDescription
Event
-----
EventID
EventDescription
EventActivity
-------------
EventID
ActivityID
Then, to list an Event with all its Activities:
SELECT * from Event
LEFT OUTER JOIN EventActivity ON Event.EventID = EventActivity.EventID
LEFT OUTER JOIN JOIN Activity ON Activity.ActivityID = EventActivity.ActivityID
You need the LEFT OUTER JOIN instead of the usual INNER JOIN in case there are no Activities for the event.
3 Comments
Well starting with the obvious:
Event(evid PK, event details)
Activity(actid PK, activity details)
EventActivityRelationship(evid FK, actid FK) -- both make the primary key together
-- where PK is primary key and FK is foreign key
Then to make a link between an event and an activity, all you need to do is to add the two ID's together to the relationship table.
And keep in mind, relational databases (basically anything that's not NoSQL) grow downward (they gain rows), never horizontally (never in columns or tables) as you add more data. That will help you make intuitive database designs.