-1

Hi i am researching a way to create a relation db with a structure as follows

Parent
 -Child 1
  -Grand Child 1
 -Child 2
  -Grand child 2

But grand child 2 can be also part of child 1

If a parent can have n children and the child can have n grand children that is fine it is pretty simple using RMDBS.

The problem comes up when you want to go 'n' deeper with grand grand children and grand children can be part of multiple children.

A good example is ebay categorization of their products.

What is the right way to create a dynamic tree/web like this in the database?

Rick James
  • 135,179
  • 13
  • 127
  • 222
Combinu
  • 882
  • 2
  • 10
  • 31
  • Does this answer your question? [What are the options for storing hierarchical data in a relational database?](https://stackoverflow.com/questions/4048151/what-are-the-options-for-storing-hierarchical-data-in-a-relational-database) – philipxy Feb 15 '20 at 23:03
  • This is a faq. Before considering posting please always google any error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names & site:stackoverflow.com & tags; read many answers. If you post a question, use one phrasing as title. See [ask] & the voting arrow mouseover texts. – philipxy Feb 15 '20 at 23:04

1 Answers1

3

From my answer to a similar question:

In a more complex model, e.g. different car models sharing the same motor model (and it's not clear whether you are talking about car models, or specific cars), the ParentEntityId column would be replaced by a relation table:

TABLE Entity (Id, Name, EntityTypeId)
TABLE EntityHierarchy (Id, ParentEntityId, ChildEntityId)

The entries in the EntityHierarchy table would be constrained (on application level) by the tree of EntityTypes

devio
  • 36,858
  • 7
  • 80
  • 143
  • sorry but i think i didn't understand it completely even after reading you original post. can you please elaborate a little bit more – Combinu Feb 15 '20 at 07:39