开发者

Email groups in MySQL

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 ca开发者_如何学Gon 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 think that what user911882 means is to have a schema like this:

Contacts
- ID
- name
- privateemail1
- privateemail2
- office
- email

Groups
- ID
- Groupname
- ParentGroupID **foreign key which references Groups.ID**

Memberships
- ID
- GroupID
- ContactID


I've solved this with a dual-use table:

Table:groupMemberships

id
groupID
linktype
linkID

linktype can 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:

SELECT linkID as userID FROM groupMemberships 
WHERE groupID=2 AND linktype='user';

To find all sub-groups:

SELECT linkID as groupID FROM groupMemberships 
WHERE groupID=2 AND linktype='group';

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:

SELECT linkID as userID FROM groupMemberships
WHERE groupID=2 and linktype='user'
UNION
SELECT sg.linkID as userID FROM groupMemberships g,groupMemberships sg
WHERE g.groupID=2 AND g.linktype='group' 
  AND sg.groupID=g.linkID AND sg.linktype='user'

If you have two tiers of sub-group, you'd need two unions and so on. When you have n sub groups you need N unions which is a pain to write and not efficient.

An alternative solution is to keep your contacts and groups tables the same, and simply link contacts to groups - table contactsGroups

ID
groupID
userID

Now 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 contactsGroups table. 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.


By understanding your question, you have to link a table to itself, then You can link to that table by adding one more column in Groups , lets say Pid*, which will be of same datatype* as that of Id in the groups, then make it as the foriegn key referencing ID in the same table. I think it will solve your issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜