连接表(join table)也可以用于一对多关系吗?

发布于 2024-07-19 12:21:57 字数 1200 浏览 5 评论 0原文

根据定义,Junction Table(桥接表/链接表)用于多对多关系,当这样使用时:

CREATE TABLE Users
(
UserLogin varchar(50) PRIMARY KEY,
UserPassword varchar(50) NOT NULL,
UserName varchar(50) NOT NULL
)


CREATE TABLE Permissions
(
PermissionKey varchar(50) PRIMARY KEY,
PermissionDescription varchar(500) NOT NULL
)


--This is the junction table.
CREATE TABLE UserPermissions
(
UserLogin varchar(50) REFERENCES Users (UserLogin),
PermissionKey varchar(50) REFERENCES Permissions (PermissionKey),
PRIMARY KEY (UserLogin, PermissionKey)
)

但是它不能同样轻松地用于一对多关系,就像在这个例子中,一个用户与许多订单相关联:

(我不太了解数据库,如果我误解了什么,请纠正我。)

CREATE TABLE Users
(
UserLogin varchar(50) PRIMARY KEY,
UserPassword varchar(50) NOT NULL,
UserName varchar(50) NOT NULL
)


CREATE TABLE Orders
(
OrderKey varchar(50) PRIMARY KEY,
OrderDescription varchar(500) NOT NULL
)


--This is the junction table.
CREATE TABLE UserOrders
(
UserLogin varchar(50) REFERENCES Users (UserLogin),
OrderKey varchar(50) REFERENCES Orders (OrderKey),
PRIMARY KEY (UserLogin, OrderKey)
)

According to the definition, a Junction Table (bridge table/link table) is used for many-to-many relationships, when used like this:

CREATE TABLE Users
(
UserLogin varchar(50) PRIMARY KEY,
UserPassword varchar(50) NOT NULL,
UserName varchar(50) NOT NULL
)


CREATE TABLE Permissions
(
PermissionKey varchar(50) PRIMARY KEY,
PermissionDescription varchar(500) NOT NULL
)


--This is the junction table.
CREATE TABLE UserPermissions
(
UserLogin varchar(50) REFERENCES Users (UserLogin),
PermissionKey varchar(50) REFERENCES Permissions (PermissionKey),
PRIMARY KEY (UserLogin, PermissionKey)
)

But couldn't it also be used just as easily for a one-to-many relationships, as in this example in which one user is associated with many orders:

(I don't understand databases well so please correct me if I have misunderstood something.)

CREATE TABLE Users
(
UserLogin varchar(50) PRIMARY KEY,
UserPassword varchar(50) NOT NULL,
UserName varchar(50) NOT NULL
)


CREATE TABLE Orders
(
OrderKey varchar(50) PRIMARY KEY,
OrderDescription varchar(500) NOT NULL
)


--This is the junction table.
CREATE TABLE UserOrders
(
UserLogin varchar(50) REFERENCES Users (UserLogin),
OrderKey varchar(50) REFERENCES Orders (OrderKey),
PRIMARY KEY (UserLogin, OrderKey)
)

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

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

发布评论

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

评论(6

月竹挽风 2024-07-26 12:21:57

是的,仍然可以在联结表中存储和强制执行一对多关系。

在您的示例中,您没有对 UserOrders 连接表施加任何约束,因此单个订单可以属于两个用户(假设这是不正确的)。 要强制执行此操作,您可以将 OrderKey 设为 UserOrders 连接表的主键(或对该列具有唯一约束)。 从技术上讲,这将成为 UserOrdersUsers 之间的多对一关系,同时具有一对一关系em> OrdersUserOrders 之间的关系。

我只能想到使用联结表设计多对一关系的一个原因 - 如果您打算将来允许多对多关系并且不这样做不想处理数据迁移。 但与此同时,您将支付存储和连接附加表的成本。

Yes, it is still possible to store and enforce one-to-many relationship in a junction table.

In your example you are not enforcing any constraints on the UserOrders junction table, so a single order can belong to two users (assuming that's incorrect). To enforce that you could make OrderKey be the primary key of the UserOrders junction table (or have a unique constraint on that column). Technically that will just become a many-to-one relationship between UserOrders and Users, while having one-to-one relationship between Orders and UserOrders.

I can only think about one reason for designing the many-to-one relationship using junction table - if you plan to allow the many-to-many relationship in future and don't want to deal with data migration. But in the mean time you will pay the cost of storing and joining with additional table.

冷心人i 2024-07-26 12:21:57

没有任何理由不能将联结表用于一对多关系。 问题通常是性能问题。 为什么在不需要的时候让数据库连接一个额外的表?

There is not any reason why a junction table couldn't be used for a one-to-many relationship. The question is usually one of performance. Why make the database join an additional table when it is unnecessary?

唱一曲作罢 2024-07-26 12:21:57

这将是多对多:

CREATE TABLE UserOrders
(UserLogin varchar(50) REFERENCES Users (UserLogin),
OrderKey varchar(50) REFERENCES Orders (OrderKey),
PRIMARY KEY (UserLogin, OrderKey));

这将是一对多(一个用户有多个订单):

CREATE TABLE UserOrders
(UserLogin varchar(50) REFERENCES Users (UserLogin),
OrderKey varchar(50) REFERENCES Orders (OrderKey),
PRIMARY KEY (OrderKey));

请注意 PRIMARY KEY 约束中的差异。

This would be many-to-many:

CREATE TABLE UserOrders
(UserLogin varchar(50) REFERENCES Users (UserLogin),
OrderKey varchar(50) REFERENCES Orders (OrderKey),
PRIMARY KEY (UserLogin, OrderKey));

This would be one-to-many (one user has many orders):

CREATE TABLE UserOrders
(UserLogin varchar(50) REFERENCES Users (UserLogin),
OrderKey varchar(50) REFERENCES Orders (OrderKey),
PRIMARY KEY (OrderKey));

Note the difference in the PRIMARY KEY constraint.

漫漫岁月 2024-07-26 12:21:57

一旦你构建了一个表,它实际上就没有“Junction”表、“associative”表、“join”表的类型——它只是一个表。

我们使用这些术语来描述最初创建实体(和结果表)的特定原因。 最初创建关联实体是为了解决多对多的情况。 但这些表通常有自己的属性(例如关联时间、关联原因等)。 因此,SQL Server、Oracle 或您的代码没有理由知道为什么创建一个表……只是知道它是一个表。

从技术角度来看,关联表和任何其他表之间确实没有任何区别。

因此,这些表可以充当任何其他表可以履行的任何角色。 没有关于其他表如何与它们相关的规则。

Once you've built a table, it really doesn't have a type of "Junction" table, "associative" table, "join" table -- it's just a table.

We use these terms to describe a specific reason why an entity (and resulting table) was initially created. Associative entities are created, initially, to resolve a many-to-many situation. But these tables quite often have attributes of their own (such as the time of the association, a reason for the association, etc.). So SQL Server, Oracle or your code has no reason to know why a table was created...just that it's a table.

From a technical point of view, there really isn't any difference between an associative table and any other table.

So these tables can fill any role that any other table can fulfill. There are no rules around how other tables can also be related to them.

明月松间行 2024-07-26 12:21:57

您可以在连接/连接表中强制执行“一个”约束,向作为“外键”的列添加唯一约束(或使其成为连接表的主键,因为该属性本身标识关系)。许多”方面。 这是因为您希望多方中的 rwo 仅具有一种关系,并且关系在联接/联结表中声明。

You can enforce de "one" constraint in thee join/junction table adding a unique constraint (or making it the primary key of the join table, because just that atribute itself identifies the relationship) to the column that is a foreign key to the "many" side. That is because you want rwos in the many side have only one relationship and relationships are stated in the join/junction table.

晨曦÷微暖 2024-07-26 12:21:57

我认为您的概念是错误的 - 这是简单的解释(如果有帮助的话):
要在两个表(例如 A 和 B)之间实现多对多关系,我们需要借助连接表(例如表 c),它与两个表都具有一对多关系A 和 B。

I think you got the concept wrong - Here is the simple explanation if it could help:
To achieve a Many-Many relationship between two tables(say, A and B), we need to take the help of a junction table(say, table c) which will have one-many relationship with both tables A and B.

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