I have a little project and I have some SQL design issue with my data.
Let say I have 2 table in my DB:
Contacts
ID
name
privateemail1
privateemail2
office email
and I have Groups
ID
Groupname
What is the best way to link together them for distributions list that I would be able to use Groups in Groups as well?
If I do a members table with
ID
GroupID
ContactID
I can’t see how to fit the embedded subgroups to the main.
Groups such as
Main groups e.g. Orchestra players; Singers
Sub groups e.g. Wind players, String players, male singers, female singers etc.
Revised question: the examples above can’t cover all the variations… any groups can become sub or main, like Outlook Distribution list?
Just looking the comments, which I am very grateful, it would be nice to be able to see an CREATE, SELECT with all the groups and a DELETE example. That would bring more attention and hopefully more result.
I hope it isn’t too dummy question but as a beginner I have spent some hours before I post this… Thanks for the helps in advance
I’ve solved this with a dual-use table:
Table:groupMemberships
linktypecan either be a text field (‘user’,’group’) which is nice and readable (if you’re doing your own SQL access it may be worth the small performance hit) or you could use an INT field where (1,2) where each mean something to you.To find all users in group 2:
To find all sub-groups:
One of the problems with a dual table like this is when you want to -say- get all group users, but you have no idea how many sub-groups their might be. If you can be sure there’ll only be one level of sub-groups you can do:
If you have two tiers of sub-group, you’d need two unions and so on. When you have
nsub groups you need N unions which is a pain to write and not efficient.An alternative solution is to keep your
contactsandgroupstables the same, and simply link contacts to groups – tablecontactsGroupsNow code side (outside of the database) when you add a user to a group (windplayers) you also add them to any parent groups (orchestra) automatically so the user actually gets two entries in the
contactsGroupstable. This has the same stored information, but makes data-retrieval much easier. You can even store this sub-group information in the database, although you already have it code-side with object inheritance or similar.