n:n表之间的数据库设计关系?

发布于 2025-01-22 15:08:05 字数 653 浏览 1 评论 0原文

我在DB设计方面的技能差,我需要有关关系的一些帮助。
因此用例是:

User which can be Coach or Client.
Client can have many coaches and coaches can have many clients.
Coach can create many workouts for client.
Client can also have many workouts assigned from coach.
Workout have many sets, sets have many exercises and reps etc...

所以我在图像上创建了设计。
但是让我觉得我在做错一切的是双键。
在此表中client_has_coach_has_workout我有两个键引用n:n表,它使我感到困惑,因为当我需要导入数据时,它将始终从n:n表中都有两个键。


请任何帮助。

I have poor skills for db design and I need some help about relations setup.
So use case is:

User which can be Coach or Client.
Client can have many coaches and coaches can have many clients.
Coach can create many workouts for client.
Client can also have many workouts assigned from coach.
Workout have many sets, sets have many exercises and reps etc...

So I created design on image.
But what make me feel that I am doing everything wrong is double keys.
In this table client_has_coach_has_workout I have two keys referencing n:n table and it confuses me as when I need to import data it will always have both keys from n:n table.
Any help please.
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

雪若未夕 2025-01-29 15:08:05

如果您愿意,此设计可以正常工作,但是我将考虑逆转Coach客户端 <代码>用户之间的链接。

  • 删除client_id用户
  • remove coach_id 从用户添加
  • 添加user_id作为外国密钥客户端
  • 添加user_id作为Coach> Coach的外

键导入您总是需要查找client_idcoach_id

要简化模型逻辑,您可以将任意ID键列添加到client_has_coach然后client_has_has_coach_has_has_has_workout仅需要一个forefer键client> client_has_has_coach_id < /代码>链接到client_has_coach表,这迫使我们从client_has_coach中查找特定的链接记录,并帮助我们实施其存在。

如果您确实使用了复合键,那么我们通常可以跳过查找client_has_coach假设已经存在的,但这可以导致孤立的行或场景。 client_has_coachclient> client_has_coach_has_workout中没有记录。

  • 复合键在我们不需要保持client_has_coachclient_has_has_coach_has_has_has_workout之间的完整性的模型中很好地工作。但是您的命名惯例表明,这在您的模型中不是理想的方面。

无论哪种方式,在导入数据时,您都需要先查找coach记录,然后查找client记录要检查或创建记录或在client_has_coach中创建记录,然后您将相应的行导入client_has_coach_has_workout

This design can work if you want it to, but I would consider reversing the link between coach and client to user.

  • remove client_id from user
  • remove coach_id from user
  • Add user_id as a foreign key to client
  • Add user_id as a foreign key to coach

Ultimately yes you are correct that now there are composite keys, but this is not a problem because during importing you would always need to lookup both the client_id and the coach_Id anyway.

To simplify the model logic, you can add an arbitrary Id key column to client_has_coach then client_has_coach_has_workout only needs a single foreign key client_has_coach_id that links back to the client_has_coach table, this forces us to lookup the specific linking record from client_has_coach and helps us enforce it's existence.

If you do use a composite key, then we can generally skip the lookup for client_has_coach and assume that it already exists, but this can lead to orphaned rows or scenarios where there is no record in client_has_coach corresponding to a combination of keys in client_has_coach_has_workout.

  • Composite keys work well in models where we do not need to maintain integrity between client_has_coach and client_has_coach_has_workout. But your naming conventions suggests that this is not a desirable aspect in your model.

Either way, on import of data you would need to first lookup the coach record, then lookup the client record to check for or create the record in client_has_coach, you would then import corresponding rows into client_has_coach_has_workout.

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