如何实现多个多对多关系?

发布于 2024-10-21 03:06:40 字数 159 浏览 1 评论 0原文

我的模型中有多个多对多关系,包括客户端、订阅、课程:

  1. 客户端有零个或多个订阅
  2. 订阅允许客户端访问一个或多个 课程

我已经有三个表,列出了所有客户、订阅计划和课程。在无需复制大量数据的情况下实现多对多关系的最佳方法是什么?

I have several many to many relations in my model consisting of a client, a subscription, a course:

  1. A client has zero or more subscriptions
  2. A subscription allows the client to access one or more
    courses

I already have three tables that list all the clients, subscription plans and courses. What would be the best method to implement the many-to-many relations without having to duplicate a lot of data?

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

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

发布评论

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

评论(4

初心 2024-10-28 03:06:40

使用 4 个表:

Client  (PK: ClientID)
Subscription (PK: SubscriptionID, FK: ClientID)
Course (PK: CourseID)
Subscription_Course (PK: Subscription_Course, FK: SubscriptionID, CourseID)

PK=主键,FK=外键。

关系如下:

Client -> Subscription (1:n)
Subscription -> Subscription_Course (1:n)
Course -> Subscription_Course (1:n)

说明:每个订阅专门针对一个客户端,因此两者之间存在 1:n 关系。但同一课程可以被不同的客户通过不同的订阅多次预订,因此课程和订阅之间存在:m关系,这是通过链接表Subscription_Course解决的。

如果需要,您可以在该模型上添加其他约束,例如,在 Subscription_Course 中的 (SubscriptionID, CourseID) 上设置唯一键约束。

Use 4 tables:

Client  (PK: ClientID)
Subscription (PK: SubscriptionID, FK: ClientID)
Course (PK: CourseID)
Subscription_Course (PK: Subscription_Course, FK: SubscriptionID, CourseID)

PK=Primary Key, FK=Foreign Key.

Here are the relations:

Client -> Subscription (1:n)
Subscription -> Subscription_Course (1:n)
Course -> Subscription_Course (1:n)

Explanation: each subscription is specificially for one client, so there is a 1:n relationship between those two. But the same course can be booked more than once by different clients via different subscriptions, so there is a n:m relationship between courses and subscriptions, which is resolved by a link table Subscription_Course.

You can add additional constraints on that model if you want, for example, put a unique key constraint on (SubscriptionID, CourseID) in Subscription_Course.

可是我不能没有你 2024-10-28 03:06:40

在此处输入图像描述

注意

ClientSubscriptionNo 是订阅号每个客户端(1,2,3..);在为客户创建新订阅时可以轻松生成它,

select coalesce(max(ClientSubscriptionNo), 0) + 1
from Subscription
where ClientID = the_client_id

您可以决定也可以不决定:

alter table SubscriptionItem
  add constraint uq1_SubscriptionItem unique (ClientID, CourseID);

enter image description here

Note:

ClientSubscriptionNo is a subscription number for each client (1,2,3..); it can be easily generated when creating a new subscription for a client using

select coalesce(max(ClientSubscriptionNo), 0) + 1
from Subscription
where ClientID = the_client_id

You may or may not decide to:

alter table SubscriptionItem
  add constraint uq1_SubscriptionItem unique (ClientID, CourseID);
我早已燃尽 2024-10-28 03:06:40

一张表包含 clientIdsubscriptionId,另一张表包含 subscriptionIdcourseId

One table with clientId, subscriptionId and another table with subscriptionId and courseId

小嗲 2024-10-28 03:06:40

在两个表之间存储多对多的常见方法是将两个表中的键放入第三个表,如下所示

Common approach to store many-to-many between two tables is to put keys from both tables to the third table like this

enter image description here

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