Question

I am working on a program where I would like to like create a Group with Users which is then always there and saved in a MySQL Database. I just don't have any clue how to do this. I mean I could create a Table like:

GROUPID INT PRIMARY KEY
GROUPNAME TEXT
USER1ID INT
USER2ID INT
USER3ID INT

Then I would have to create A table which can take like lets say in this case 3 Users but isn't that a not performant and no possibility to dynamically make the group bigger. If I would like to make a friends list out of the Group then I also can't say that the User can have only 3 Friends. How can I solve this problem?

Was it helpful?

Solution

You need 2 tables:

GROUPS - GroupId, GroupName
USERS - UserId, UserName, Password, GroupId

The GroupId in USERS links user entries to GROUPS. You can (should) also impose the link using a foreign key.

If a user can belong to more than one group, you will need a many-to-many relationship. This is achieved with a 3rd table that looks like this:

GROUPS - GroupId, GroupName 
USERS - UserId, UserName, Password
USERS_GROUPS - UserId, GroupId
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top