PDA

View Full Version : Table Structure Advice


DerekNutile
11-16-2007, 09:14 PM
I have two tables - Users and Groups. Pretty straightforward layout where the Users table has ID, last_name, first_name, etc... The Groups table is simply ID, name, description, etc... I want to include in my Users table a group_ids field. What makes this complex, is one user could be a member of just one or many groups, so I can't simply say User1 is in Group1. User1 may be in Group2, Group17,etc...

How does one (or how would you) apply this? I've thought about making the group_ids field a 'text' datatype and separating the group ids by comma or space., but for several reasons this doesn't seem to be the right way to do this.

Let me know if I haven't explained this well or if I should give an example of the data for clarification.

Thanks in advance. Derek.

PappaJohn
11-16-2007, 09:22 PM
I would create a third table to hold the user-group associations. This table would contain:

1. an index field (probably auto_increment)
2. the User's ID
3. The Group's ID

This way the user could be a member of any number of groups.

DerekNutile
11-16-2007, 09:24 PM
Wow. Stunningly simple PappaJohn, thank you!

guelphdad
11-17-2007, 12:16 AM
the index field isn't necessary, only the userid and groupid fields are necessary. the two combined are the primary key of the table.