简体   繁体   English

SQL多对多关系

[英]SQL Many-To-Many relationships

Question

Is there a way to have a many-to-many relationship among 3 tables without the use of automatic incrementers (usually ID), or are ID's required for this? 有没有办法在不使用自动增量器(通常为ID)的情况下在3个表之间建立多对多关系,或者是否需要ID?

Why I ask 为什么我问

I have 3 relative tables. 我有3个相对表。 Since one-to-one relationships seem to can't happen directly, I made a 4th to do one-to-many relationships to the other 3 tables. 由于一对一的关系似乎不能直接发生,所以我做了第4次与其他3个表做一对多的关系。 However, since there's still a primary key to each table, a value can only be used once in a table, which I don't want to happen. 但是,由于每个表仍然有一个主键,因此一个值只能在表中使用一次,我不希望这样做。

What I have 我有的

There is no requirement that a table have an "automatic incrementer" as a primary key. 不要求表具有“自动增量器”作为主键。

But, a familiar pattern is to add a surrogate ID column as primary key on entity tables. 但是,熟悉的模式是将代理ID列添加为实体表的主键。 The "ideal" primary key will be "anonymous" (carry no meaningful information), "unique" (no duplicate values), "simple" (single column, short simple native datatype), ... “理想”主键将是“匿名”(不携带有意义的信息),“唯一”(无重复值),“简单”(单列,短简单本机数据类型),...

There are a couple of schools of thought on whether it's a good idea to introduce a surrogate key. 关于引入替代密钥是否是一个好主意,有几种思想流派。 I will also note that there are those who have been later burned by the decision to use a natural key rather than a surrogate key. 我还要注意的是,有些人后来被决定使用自然密钥而不是代理密钥而被烧毁。 And there are those that haven't yet been burned by that decision. 还有那些尚未被这个决定所焚烧的东西。


In the case of "association" tables (tables introduced to resolve many-to-many relationships), the combination of the foreign keys can be used as the primary key. 在“关联”表(为解决多对多关系而引入的表)的情况下,外键的组合可以用作主键。 I often do this. 我经常这样做。

BUT, if the association table is itself turns out to be entity table, with it's own attributes, I will introduce a surrogate ID column. 但是,如果关联表本身证明是实体表,并且具有它自己的属性,我将引入一个代理ID列。 As an example, the association between person and club , a person can be a member of multiple clubs, and a club can have multiple members... 例如, personclub之间的关联,一个人可以是多个俱乐部的成员,俱乐部可以有多个成员......

club +--< membership >--+ person

When we start adding attributes to membership (such as status , date_joined , office_held , etc... at that point membership isn't just an association table; it's turning into an entity. When I suspect that an association is actually an entity, so I'll add the surrogate ID column. 当我们开始向membership添加属性时(例如statusdate_joinedoffice_held等...)此时membership不仅仅是一个关联表;它变成了一个实体。当我怀疑一个关联实际上是一个实体时,所以我将添加代理ID列。

The other case where I will add a surrogate ID column to an association table is when we want to allow "duplicates", where we want to allow multiple associations. 我将向代理表添加代理ID列的另一种情况是我们想要允许“重复”,我们希望允许多个关联。 In that case, I will also introduce a surrogate ID column. 在这种情况下,我还将介绍一个代理ID列。

Yes you can but, 是的,你可以,但是,

It is customary to represent a table row by a unique identifier which is the number, its becomes more efficient. 习惯上用唯一标识符表示表行,该标识符是数字,它变得更有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM