用于将不同表中的行连接在一起的列的名称是什么?

What is the name of a column used to join together rows from different tables?

我有一个 table 这样的:

name,height
Alice,1.75
Bob,1.74

然后我有一个像这样的 table:

name,DOB
Bob,1965-04-04
Alice,1972-09-27

我想要这样的 table:

name,DOB,height
Alice,1972-09-27,1.75
Bob,1965-04-04,1.74

在数据库术语中,name 列的名称是什么?那是主键吗?

不知道他们有没有标准的名字;我一直将它们称为 "the join columns" 作为 "the columns participating in the join" 的缩写,或者,如果谈论它们包含的布尔运算,"join conditions" / "join predicates"

主键是另一回事。它是一列或一组列,可以保证在 table 中唯一标识一行。 table 不必有 PK,不必有 PK 即可参与联接,并且联接不必使用作为主键一部分的 ny 列。 table 必须有主键才能作为外键的目标;确保一个 table 中的数据在另一个数据

中具有相关数据的机制

在您的示例中,您可能会将所有这些数据都放在同一个 table 中。不要在 table 之间不必要地拆分数据,尤其是当 table 之间的关系是 1:1

因为一个人的生日永远不会改变,但他们的身高会改变,所以将 table 的生日作为主要 table 和 table 更有意义高度也有一个数据列,指示高度的获取时间。这个 table 然后将外键到主 table

Person
Name, DOB
Alice, 2000-01-01
Bob, 2001-01-01

Measurement
Name, Height, ReadingDate
Alice, 100, 2002-01-01
Alice, 110, 2003-01-01
Bob, 101, 2002-01-01
Bob, 112, 2003-02-01
Bob, 118, 2004-01-01

Measurement.Name 不能是 PK,因为它不是唯一的,但我们可以说 Name+ReadingDate 可以是(如果我们有规则 "no two readings on the same day")。使用两列作为 PK 不会阻止 Meansurement.Name 成为引用 Person.Name(这是主键)

的外键

注意;我并不是说这种关系必须是这样的。您当然可以使 DOB table 成为高度 table 的外键 - 只是这样并没有真正意义,但这样更有意义。此结构可以回答 "how tall was Bob on date X"、"what is the average growth rate per month of boys vs girls" 或 "based on extrapolation how tall might Alice be now?"

等问题