My database (version: 10.5.15-MariaDB-o+deb11u2) has a table "positions". Every position is either a "parent", "child" or none of both (so i do not need a hierarchical database, since the hierachy of my case has only a level of 2). If it is a parent, then this position has a connection to 1 to n "child" positions. If a position is a "child", it has a connection to exactly one "parent" position. If the position is none of both, it is just on its own. Should i split this table in three tables and create a relation of parent/child? Later i will want to filter a query and every time a position is kept AND a parent (or a child), then i want to keep all its children (or the parent). Basically i want to keep the whole group of parent/children if only one of all of these is kept (kept after filtering e.g. if the position_id is prime or what ever). My problem is, that i can only think about a very bad filter method. Example: I filtered the positions and kept all prime position_id positions. (Problem comes now...) If a position is child (or parent) i need the parent (children). So i would need to query again. Plus i would have to make sure, that i don't get duplicate positions. In the same example i might already kept the parent position (or children positions). How can i get an effective selection? I think my just described procedure is not effective. Also it seems overcomplicated, can i maybe filter all positions with one "clean" filter at once? Such that also the parent/children are kept after filtering?
My last post (about the same problem) was associated with this post: What are the options for storing hierarchical data in a relational database? What i found there, didn't seem to fit.
This is how an example table looks like:
| position_id | example_value | parent | children |
| ----------- | -------------- | -------- | -------------- |
| 1 | 4 | null | [ ] |
| 2 | 5 | 3 | [ ] |
| 3 | 12 | null | [2, 4] |
| 4 | 5 | 3 | [ ] |
| 5 | 12 | 6 | [ ] |
| 6 | 11 | null | [5] |
If i filter with example_value > 10
, then i want to return positions with id 2, 3, 4, 5 and 6, while positions with id=2 and id=4 are added, because they are children of position with id = 3.