Jump to content

Restricting many-to-many relationships


MmmVomit

Recommended Posts

I've got a basic many-to-many relationsip set up in my database

 

RECORD
-------------
ID
RecordData


TYPE
-------------
ID
TypeDescription


RECORD_TYPE_JUNCTION
--------------
ID
RecordID  => RECORD.ID
TypeID    => TYPE.ID

 

What I'd like to do is restrict the specific combinations of types are allowed.  In general each RECORD will only get one TYPE, but there are certain combinations of types that I need to make possible.

 

I've come up with a solution by adding a GROUP table.

 

GROUP
-----------------
ID
Group
TypeID  => TYPE.ID

 

The data in this table would look something like this.

 

GROUP
-----------------
ID   Group   TypeID
1     1       1
2     1       4
3     1       5
4     2       2
5     2       4
6     3       2
7     3       5
8     3       7

 

A valid combination of types would be (1, 4, 5), (2, 4), or (2, 5, 7).  If a type does not appear in the GROUP table, it is a singleton and is not allowed in combination with any other types, e.g. 6.  Any subset of a valid group is allowed, so (4, 5) is allowed, because it is a subset of group 1.

 

Does anyone see drawbacks to this?

Is there a better way of doing this?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.