Monday, February 13, 2012

Beginner question - sorry

Suppose I have a group of members and I want they to groups in to different categories. Each member has an ID and I would like each group to have an ID- I guess in programming it would typically done with a 2 dimensional array or something. How does SQL do this? Also each member may belong to more than one group. and also - I though of creating a new table for each group but then I am afraid of the maintenance of so many tables and the groups could quickly go into the thousands and ten thousands. And it really doesn't fit into my design plans also.

Any help or ideas would be appreciated.

Thanks,Hi,

There are a number of solutions to this problem, one would be to create a Groups table, a Users table and a UsersPerGroups table.

Users

UserID | UserName
--------
1 | echo88
2 | taylorza
3 | tmorton

Groups

GroupID | GroupName
--------
1 | Admin
2 | Power User
3 | User

Putting users in groups is as easy as populating the UsersPerGroups table, in this case
echo88 belongs to the Admin and Power User groups
taylorza belongs to the User group
tmorton belongs to no groups

UsersPerGroup

UserID | GroupID
-------
1 | 1
1 | 2
2 | 3

Hope this helps

No comments:

Post a Comment