Mysql 连接两张表(FK)——数据库设计
如何以最佳方式将表project_user 与表project_terms 连接起来?
- 一个项目有不同的类别,如 1,2,3,4
- 类别存储在categories_table 中
- ,但一个用户可能仅分配给类别 1,2 而不是 3,4
- 我是否也应该将project_table 与categories_table 连接起来?
我该如何在数据库设计中解决这个问题?
[project_user]
id name
1 Tom
// But User Tom only assigned for the Project_id 1
// and the cat_id 2 in the project_terms table
[project_terms]
project_id (fk) | cat_id (fk)
1 | 2
1 | 3
[categorys_table]
cat_id | catname
2 | Lemon
3 | Apple
[project_table]
project_id | name
1 | FruitsProject
how can I connect the table project_user with the table project_terms in the best way?
- One Project has different Categorys like 1,2,3,4
- The Categorys are stored in the categorys_table
- But one User maybe are assigned only for the Categorys 1,2 and not 3,4
- Should I connect the project_table with the categorys_table too?
how can I solve that in my Database Design?
[project_user]
id name
1 Tom
// But User Tom only assigned for the Project_id 1
// and the cat_id 2 in the project_terms table
[project_terms]
project_id (fk) | cat_id (fk)
1 | 2
1 | 3
[categorys_table]
cat_id | catname
2 | Lemon
3 | Apple
[project_table]
project_id | name
1 | FruitsProject
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不知道我是否理解正确:一种可能的方法是将所有用户与一个项目相关联
这样,您就可以将一个用户分配给一个项目。
为了能够检查术语,您可以引入一个与用户匹配术语的表。
您也可以直接省略
[project_project_user]
表,因为可以通过[project_terms_user]
建立连接。对于第一个可用的表,某些查询可能会更有效,但结果应该是相同的。No idea if I understood you correctly: One possible approach could be to associate all your users with a project
With this, you have a user assigned to a project.
To be able to check the terms, you could introduce a table that matches terms to users.
You could also go as fas as leaving out the
[project_project_user]
table since the connection can be established via[project_terms_user]
. Some queires may be more efficient with the first table available, but the resuls should be the same.