I have some data that looks like this:
1: Events
2: Event 1
3: Event 1.1
4: Event 2
5: Event 2.2
6: Event 2.2.1
7: Event 2.2.1.1
8: Event 2.2.1.2
9: Event 2.2.1.3
10: Event 2.2.1.3.1
11: Event 3
12: Event 4
13: Event 4.1
Event 1, 2, 3 and 4 would all be nested under ‘Events’
Event 1.1 would be nested under Event 1
etc. etc.
How should I store this in the same table in the database? Could I just have another field for which ids to include?
That would look like this:
Fields:
id (id of event)
name (name of event)
ids_to_include (events that happened within this event)
The most that this (example) data is nested, is by 4 levels (2.2.1.3.1). But in my mind I’m thinking historical data, where every single event in history has many more sub-events, so the nesting needs to be limitless. I don’t want to create a new table for each “type” of historical event, I’d rather just a table for long-term periods (like WW2:1939-1945) which would have no nesting, and events which would have as much nesting as needed.
Or am I going about this the entirely wrong way? Thanks.
There are some models to store hierarchical data in relational tables, the most used two of them being
nested setsandadjacency list.Depending on the types of the queries you want to run against these models, either can be more efficient.
In
MySQL, adjacency list is somewhat harder to query, since it requires set recursion whichMySQLlacks, but still possible.See this entry in my blog for the comparison of these two models: