识别和非识别关系有什么区别? [英] What's the difference between identifying and non-identifying relationships?
问题描述
我没有能够完全掌握差异。
-
标识关系是指子表中的行的存在取决于父表中的行。这可能令人困惑,因为现在常见的做法是为子表创建一个伪键,但不会将外键设置为子键的父键的父部分。正式地,正确的方式做到这一点是使外键的部分孩子的主键。
示例:A
Person
有一个或多个电话号码。如果他们只有一个电话号码,我们可以将它存储在Person
的列中。由于我们要支持多个电话号码,我们创建了第二个表PhoneNumbers
,其主键包括person_id
Person
表。
我们可能认为电话号码属于某人,即使他们被建模为单独表的属性。这是一个强有力的线索,这是一个识别关系(即使我们在字符上不包括
person_id
在主键PhoneNumbers
)。 -
非标识关系是父级的主键属性成为子的主键属性。一个很好的例子是查找表,例如
Person.state
上引用States.state $ c>的主键的外键$ c>。
Person
是关于States
的子表。但是Person
中的一行不是由其状态
属性标识的。也就是说
p>非标识关系可以是可选或强制,这意味着外键列分别允许NULL或disallows NULL。
另请参阅我对仍然困惑关于识别与非识别关系
I haven't been able to fully grasp the differences. Can you describe both concepts and use real world examples?
An identifying relationship is when the existence of a row in a child table depends on a row in a parent table. This may be confusing because it's common practice these days to create a pseudokey for a child table, but not make the foreign key to the parent part of the child's primary key. Formally, the "right" way to do this is to make the foreign key part of the child's primary key. But the logical relationship is that the child cannot exist without the parent.
Example: A
Person
has one or more phone numbers. If they had just one phone number, we could simply store it in a column ofPerson
. Since we want to support multiple phone numbers, we make a second tablePhoneNumbers
, whose primary key includes theperson_id
referencing thePerson
table.We may think of the phone number(s) as belonging to a person, even though they are modeled as attributes of a separate table. This is a strong clue that this is an identifying relationship (even if we don't literally include
person_id
in the primary key ofPhoneNumbers
).A non-identifying relationship is when the primary key attributes of the parent must not become primary key attributes of the child. A good example of this is a lookup table, such as a foreign key on
Person.state
referencing the primary key ofStates.state
.Person
is a child table with respect toStates
. But a row inPerson
is not identified by itsstate
attribute. I.e.state
is not part of the primary key ofPerson
.A non-identifying relationship can be optional or mandatory, which means the foreign key column allows NULL or disallows NULL, respectively.
See also my answer to Still Confused About Identifying vs. Non-Identifying Relationships
这篇关于识别和非识别关系有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!