0

How would I setup a groups table with following relationship with a list of user:

For example, lets say I have table of users:

              user_list
-----------------------------------
first_name | Last Name  | user_id |
-----------------------------------
   john    | Appleseed  |  4      |
   Jasmin  | Applejuice |  6      |

Now I want to create a groups table. How can I create a table where users can be included in a group, or multiple groups. For example, John is included in the Apple_group but also included in the Orage_group. Jasmin is only included in the Orange_group:

                        groups
----------------------------------------------------
   group_name    | included_users_ids  | group_id  |
----------------------------------------------------
   Apple_group   |       4             |   1       |
   Orange_group  |       4, 6          |   6       |

Assuming the user_list table already exists, how would I setup the groups table? Furthermore I've read (see link below) that I shouldn't include multiple values in a single column.

Inserting a foreign key with multiple values

So, my questions are:

  1. What would be the appropriate way to setup the groups table?

  2. What would the SQL be for doing so?

  3. How would this then be handled with PHP when wanting to set a user to a group?

Community
  • 1
  • 1
user3143218
  • 1,738
  • 5
  • 32
  • 48

1 Answers1

1

You need to create a third table, referred to as a "join table".

users (
  id,
  name
);

groups (
  id,
  first_name,
  last_name
);

groups_to_users (
  user_id,
  group_id
);

Then create an entry in the groups_to_users table for every group a user is a member of (or, you could say it another way, one entry for every user that's in a group). This is an n:n relationship (many-to-many). If you search for "database normalization", as I mentioned to you in an answer to a previous question, you'll learn all about this stuff.

If you want to get all of a user's groups, you'd do something like:

SELECT g.* FROM groups_to_users gtu
LEFT JOIN groups g ON gtu.group_id=g.id
WHERE gtu.user_id = :UserId

If you wanted to get a group's users, you'd do:

SELECT u.* FROM groups_to_users gtu
LEFT JOIN users u ON gtu.user_id=u.id
WHERE gtu.group_id = :GroupId
Cully
  • 6,427
  • 4
  • 36
  • 58
  • Would the third group be foreign key based or entry? – user3143218 May 08 '14 at 03:24
  • Any time you have a key to another table in your table, it's a foreign key. So, group_id and user_id are both foreign keys (i.e. Keys foreign to "this" table). – Cully May 08 '14 at 03:25
  • you mean foreign keys to group table id and the userlist id? It should auto-populate when creating a group with a user id then no? I've tried and it doesn't update. – user3143218 May 08 '14 at 03:40
  • It does not auto-populate. There is no group with a user_id. The only place that Users and Groups are associated is in the groups_to_users table. You can create a User without a Group knowing. You can create a Group without a User knowing. If you want to put a user in a group, you need to create an entry in groups_to_users. – Cully May 08 '14 at 03:48
  • @user3143218 You need to go read some tutorials on database normalization, foreign keys, and join tables. There are a ton out there. – Cully May 08 '14 at 03:49
  • That's what I was saying, so they're not foreign keys. they're just normal entry columns that I would populate when needed. – user3143218 May 08 '14 at 03:50
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/52249/discussion-between-user3143218-and-cully-larson) – user3143218 May 08 '14 at 03:50
  • They are foreign keys. You can even define them as such in MySQL using FOREIGN KEY. However, they don't auto-populate. Go read tutorials :) – Cully May 08 '14 at 03:51