如何设计这个数据库部分? [英] How to design this Database part?

查看:121
本文介绍了如何设计这个数据库部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHP和MySQL。

I'm using PHP and MySQL.

我有一个users表和一个groups表。
我的问题是,如何实现一个系统的组,以便我可以访问作为该组的一部分的所有用户ID。

I have a users table and a groups table. My question is, how can I implement a system in groups so that I can acces all the users id's which are part of that group.

示例:I在用户表中有John,Mary和Alex。 John是id 1,Mary是2,Alex是3。

Example: I have John, Mary and Alex in the users table. John is id 1, Mary is 2 and Alex is 3.

现在如何将这些用户添加到群组用户?我不能只添加user_1,user_2,user_3等字段。

Now how can I add these users to the group users? I can't just add fields like "user_1", "user_2", "user_3". What if the group has 1000 members?

推荐答案

您可以拥有一个组表和用户表。在用户表中添加groupid作为外键
Like:

You can have a groups table and users table. add groupid as foreign key in users table Like:

tbl_group : id: int(11) Auto Increment - P.K
          : group_name: varchar(500)

tbl_users : id: int(11) Auto Increment - P.K
          : user_name: varchar(20)
          : group_id:  int(11) - Foreign Key - // Foreign Key to groups Table.    

如果您想让许多用户加入多个组,

If you want many users want to join many groups then

tbl_group : id: int(11) Auto Increment - P.K
          : group_name: varchar(500)

tbl_users : id: int(11) Auto Increment - P.K
          : user_name: varchar(20)

tbl_user_group : id: int(11) Auto Increment - P.K
               : group_id:  int(11) - Foreign Key - // Foreign Key to groups Table.  
               : user_id:  int(11) - Foreign Key - // Foreign Key to usersTable.      

这篇关于如何设计这个数据库部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆