Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database design - how to implement user group table?

Tags:

database

mysql

I want to make user group system that imitates group policy in instant messengers.

Each user can create as many as groups as they want, but they cannot have groups with duplicate names, and they can put as many friends as they want into any groups.

For example, John's friend Jen can be in 'school' group of John and 'coworker' group of John at the same time. And, it is totally independent from how Jen puts John into her group.

I'm thinking two possible ways to implement this in database user_group table.

1.

user_group (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT,
group_name VARCHAR(30),
UNIQUE KEY (user_id, group_name)
)

In this case, all groups owned by all users will have a unique id. So, id alone can identify which user and the name of the group.

2.

user_group (
user_id INT,
group_id INT AUTO_INCREMENT,
group_name VARCHAR(30),
PRIMARY KEY (user_id, group_id),
UNIQUE KEY (user_id, group_name)
)

In this case, group_id always starts from 0 for each user, so, there could exist many groups with same group_id s. But, pk pair (user_id, group_id) is unique in the table.

which way is better implementation and why? what are advantages and drawbacks for each case?

EDIT: added AUTO_INCREMENT to group_id in second scenario to insure it is auto-assigned from 0 for each user_id.

EDIT: 'better' means... - better performance in SELECT/INSERT/UPDATE friends to the group since that will be the mostly used operations regarding the user group. - robustness of database like which one will be more safe in terms of user size. - popularity or general preference of either one over another. - flexibility - extensibility - usability - easier to use.

like image 487
Joon Avatar asked Apr 14 '11 17:04

Joon


People also ask

What is User Group in database?

User Groups are database items that represent a collection of user accounts. They are designed to make it easier to allocate security permissions to multiple user accounts. Typically, you create a User Group to represent a group of users.

What should be in a user table?

Typically, a user table contains data about your company's customers, prospects, or products. For example, a user table might contain columns for customer account data such as Account ID, Account Type, and Balance.


1 Answers

Personally, I would go with the 1st approach, but it really depends on how your application is going to work. If it would ever be possible for ownership of a group to be changed, or to merge user profiles, this will be much easier to do in your 1st approach than in the 2nd. In the 2nd approach, if either of those situations ever happen, you would not only have to update your user_group table, but any dependent tables as well that have a foreign key relation to user_group. This will also be a many to many relation (there will be multiple users in a group, and a user will be a member of multiple groups), so it will require a separate joining table. In the 1st approach, this is fairly straightforward:

group_member (
  group_id int,
  user_id int
)

For your 2nd approach, it would require a 3rd column, which will not only be more confusing since you're now including user_id twice, but also require 33% additional storage space (this may or may not be an issue depending on how large you expect your database to be):

group_member (
  owner_id int,
  group_id int,
  user_id int
)

Also, if you ever plan to move from MySQL to another database platform, this behavior of auto_increment may not be supported. I know in MS SQL Server, an auto_increment field (identity in MSSQL) will always be incremented, not made unique according to indexes on the table, so to get the same functionality you would have to implement it yourself.

like image 52
Joel C Avatar answered Sep 30 '22 15:09

Joel C