在 2 个表之间创建关系时应该连接什么?
What should I connect when creating a relationship between 2 tables?
例如我有2个table,即parent table和children table
Parent table
parent_id
name
那我有Childrentable
children_id
name
然后关系Table
relationship_id
child_id
father_id
或 father_name?
mother_id
或 mother_name?
如果我想在 2 个 table 之间创建一个关系,表示 children
的 parents
我应该 link?
我应该创建一个名为 father_id
的字段来获取 parent 的唯一 ID 还是 father_name
来直接获取 link 的名称关系中的父亲 table 如果我想得到父亲的名字?
您应该(至少在某些(如果不是全部)数据库中,这是您唯一的选择)始终使用主键来建立关系。
关系的至少一侧需要连接到 table 的主键。
但是,对于你写的例子,我会和一个人一起去table:
TblPerson
(
Person_Id, -- Primary key
Person_FirstName,
Person_LastName,
Person_Mother_Id, -- Self reference FK
Person_Father_Id -- Self reference FK
)
例如我有2个table,即parent table和children table
Parent table
parent_id
name
那我有Childrentable
children_id
name
然后关系Table
relationship_id
child_id
father_id
或 father_name?
mother_id
或 mother_name?
如果我想在 2 个 table 之间创建一个关系,表示 children
的 parents
我应该 link?
我应该创建一个名为 father_id
的字段来获取 parent 的唯一 ID 还是 father_name
来直接获取 link 的名称关系中的父亲 table 如果我想得到父亲的名字?
您应该(至少在某些(如果不是全部)数据库中,这是您唯一的选择)始终使用主键来建立关系。
关系的至少一侧需要连接到 table 的主键。
但是,对于你写的例子,我会和一个人一起去table:
TblPerson
(
Person_Id, -- Primary key
Person_FirstName,
Person_LastName,
Person_Mother_Id, -- Self reference FK
Person_Father_Id -- Self reference FK
)