[英]How to structure this relational database
Basically I'll be having 3 tables that have relation. 基本上,我将拥有3个具有关联关系的表。 They are:
users
, departments
and company
. 他们是:
users
, departments
和company
。
The issue I have is this: 我的问题是这样的:
company
can have many departments company
可以有很多部门 department
can only be attached to one company department
只能隶属于一个公司 user
can only be part of one company user
只能是一个公司的一部分 user
can be part of many departments user
可以是多个部门的一部分 This is essentially what the table relation would look like: 这实际上就是表关系的样子:
____________________
| | | | |
| | | | |
-------- -------------- -----------
| user | | department | | company |
-------- -------------- -----------
| | | | | | | |
| | | | | | | |
| ___________________ |
| |
| |
| |
_______________________________________
The above multiple |
以上倍数
|
lines show an option, so the "company" above has 4 departments and so on. 行显示一个选项,因此上面的“公司”有4个部门,依此类推。
Now my question is this, How should I structure the relation tables? 现在我的问题是,我应该如何构建关系表?
Should I have user_departments
, user_company
and company_departments
tables? 我应该有
user_departments
, user_company
和company_departments
表吗?
That would essentially look like this: 基本上看起来像这样:
--------------------
| user_departments |
--------------------------------
| id | user_id | department_id |
--------------------------------
----------------
| user_company |
-----------------------------
| id | user_id | company_id |
-----------------------------
-----------------------
| company_departments |
-----------------------------------
| id | company_id | department_id |
-----------------------------------
Or are there any other alternatives for me to consider/implement instead of the path I'm going as it seems it'll just keep growing complex? 还是有其他选择让我考虑/实施,而不是我要走的路,因为它似乎只会变得越来越复杂?
You are essentially making redundant relationships. 您实际上是在建立多余的关系。 You should have no need for a company_departments, company_id will just be a field of the departments table to reference the company a department is a part of.
您应该不需要company_departments,company_id只会是Departments表的一个字段,以引用一个部门所属的公司。 Likewise, you won't need a user_company table, but you will need the user_departments one;
同样,您将不需要user_company表,但是将需要user_departments一个。 that is due to the user-department relationship actually being many-to-many.
这是因为用户与部门之间的关系实际上是多对多的。
With the example you've given, you should only need four tables. 在给出的示例中,您只需要四个表。
company: company_id, other company info (such as name)
department: department_id, company_id (referencing the company record), other department info
user: user_id, company_id (referencing the company record), other user info
user_departments: user_id, department_id, perhaps information such as user's role in department, or if you want historical data preserved dates assigned to and removed from department
Here is the layout in the format you used: 这是您使用的格式的布局:
---------
| users |
--------------------------------
| id | name | company_id | ... |
--------------------------------
-----------
| company |
-------------------
| id | name | ... |
-------------------
-----------
| departments |
--------------------------------
| id | name | company_id | ... |
--------------------------------
--------------------
| user_departments |
--------------------------------
| id | user_id | department_id |
--------------------------------
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.