I'd recommend creating a table which encompasses subnets, VLANs and VPLSs; you can then have a simple self-join table. If these three concepts each have a significant number of unique fields, you can use vertical partitioning and create a sister table for each of the three. Using pseudo-code:
* Networks
NetworkID PK
* Locations
LocationID PK
* SubNetworks -- This concept encompasses SubNets, VLANs, and VPLSs
SubNetworkID PK,
NetworkID NOT NULL REFERENCES Networks
* SubNetworkLocations
SubNetworkID NOT NULL REFERENCES SubNetworks,
LocationID NOT NULL REFERENCES Locations,
PRIMARY KEY (SubNetworkID, LocationID)
* SubNets
SubNetworkID PK REFERENCES SubNetworks
-- Additional fields specific for this type of sub-network
* VLANs
SubNetworkID PK REFERENCES SubNetworks
-- Additional fields specific for this type of sub-network
* VPLSs
SubNetworkID PK REFERENCES SubNetworks
-- Additional fields specific for this type of sub-network
* SubNetworkRelationships
SubNetworkA NOT NULL REFERENCES SubNetworks,
SubNetworkB NOT NULL REFERENCES SubNetworks,
PRIMARY KEY (SubNetworkA, SubNetworkB),
UNIQUE (SubNetworkB, SubNetworkA) -- For efficient querying from the other direction
As with any many-to-many table, be sure your sproc for inserting into SubNetworkRelationships
checks for the existence of both { A, B } and { B, A } before inserting a new row. You can use check constraints to prevent a sub-network from relating to itself.
In this model, there's no RDBMS-level enforcement of the rule "if two sub-networks are related, they must have all of the same locations." I'm going to go out on a limb and assume that all such tied-together sub-networks must belong to the same Network
, too. If you really want this:
* Networks
* Locations
* SubNetworkGroups
SubNetworkGroupID PK,
NetworkID NOT NULL REFERENCES Networks
* SubNetworkGroupLocations
SubNetworkGroupID NOT NULL REFERENCES SubNetworkGroups,
LocationID NOT NULL REFERENCES Locations,
PRIMARY KEY (SubNetworkGroupID, LocationID)
* SubNetworks
SubNetworkID PK
* SubNetworkGroupMemberships
SubNetworkGroupID NOT NULL REFERENCES SubNetworkGroups,
SubNetworkID NOT NULL REFERENCES SubNetworks,
PRIMARY KEY (SubNetworkGroupID, SubNetworkID)
* SubNets
* VLANs
* VPLSs
A group of SubNetwork
s belong to one Network
but zero or more Locations
. A SubNetwork
can belong to multiple groups. You'd still need to enforce that the various groups a sub-network belongs to all have the same locations, so I'm not sure this buys you much.