Mysql 连接两张表(FK)——数据库设计

发布于 2024-09-26 03:16:41 字数 616 浏览 1 评论 0原文

如何以最佳方式将表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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤单情人 2024-10-03 03:16:41

不知道我是否理解正确:一种可能的方法是将所有用户与一个项目相关联

[project_project_user]
project_id  |   user_id
    1              1

这样,您就可以将一个用户分配给一个项目。

为了能够检查术语,您可以引入一个与用户匹配术语的表。

[project_terms_user]
user_id   |    term_id
    1              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

[project_project_user]
project_id  |   user_id
    1              1

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.

[project_terms_user]
user_id   |    term_id
    1              2

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文