It’s been a long time since I designed a database and just want to make sure that I am designing it properly.
I have an Entity which has a name and description.
This Entity can have many subEntities which are of type Entity.
So its like a recursive relationship. Now sure how to design the db properly.
Do I create a second table or what?
Update: One entity can only have a single parent entity or no parents.
Edit: Based on the update to the question, my suggested solution is in bold, below:
First, let’s be clear whether this is really a many-to-many situation because it’s not clear from your description whether that’s true or not.
You describe a tree structure in which an entity can have “nested” entities and, presumably, those nested entities can each have their own nested entities. But this is only a many-to-many question if a single entity can belong, simultaneously to multiple parents.
In either case, you will store all the entities in a single table. If you want to represent a “simple” tree of nested entities without multiple-parentage then you can add a
parent_entity_idcolumn to theentitiestable which points to theentity_idof the parent. Top level entities would have either a flag value (for instance, -1) or aNULLin this column. In this case, you need only one more column and no new tables to represent the relationship.If it’s really a many-to-many relationship with multiple-parentage then you will create a new table
entity_linkswith columnsparent_entity_idandchild_entity_id. You manage the relationships by inserting and deleting rows in this table.