How would I model the relationship between several objects, where one entity could be part of two separate, unrelated hierarchies, and each object could be connected with one or more other objects in a hierarchical order? I would like to do this in just 2 or 3 tables in the database.
Currently I have modeled it in two tables:
Entities
----------------------------------------------------------------
ID bigint identity(1, 1) PK
ParentID bigint null FK
Name varchar(100) not null
Description varchar(256) null
EntityRelationships
----------------------------------------------------------------
LEntityID bigint not null PK, FK
REntityID bigint not null PK, FK
EntityRelationshipTypeID int not null PK, FK
Two columns: LEntityID and REntityID are the FKs in the Entities.ID column, and ParentID is the FK in the ID column. This model is likely to work fine if the object never has more than one parent. I need to be able to allow entities to have more than one parent.
The natural keys on the tables are:
Entities: ParentID, Name
EntityRelationships: LEntityID, REntityID, EntityRelationshipTypeID
, .
.