数据库设计确保一对一关系
Database design ensure one to one relationship
我试图了解 class table 继承在数据库设计中的局限性。例如,如果我有这样的模式,我如何确保学校字段 table 或非营利组织 table 中的不止一行不引用相同的联系人 ID
Contact
--------
id - PK
fname - String
lname - String
email - String
School Field
--------------
id - PK
contact_id - FK
notes - String
Non Profit Field
-----------------
id - PK
contact_id - FK
donation - unsigned int
我会推荐一种不同的方法。
Entity
--------------
id - PK
Field_Type_ID - FK
contact_id - FK
notes - String
donation - unsigned int (nullable)
Contact
--------------
id - PK
fname- String
lname- String
email- String
Field_Type
--------------
id - PK
Description- FK
notes - String
通过这样做,如果您最终得到更多 "field" 类型,则无需添加更多 table。您只需将新记录添加到 Field_Type table。然后,为了强制执行您想要的行为,您在实体上添加一个唯一约束,该约束同时查看 contactID 和 FieldTypeID。这将确保每个联系人只能链接到一个字段类型一次。
我试图了解 class table 继承在数据库设计中的局限性。例如,如果我有这样的模式,我如何确保学校字段 table 或非营利组织 table 中的不止一行不引用相同的联系人 ID
Contact
--------
id - PK
fname - String
lname - String
email - String
School Field
--------------
id - PK
contact_id - FK
notes - String
Non Profit Field
-----------------
id - PK
contact_id - FK
donation - unsigned int
我会推荐一种不同的方法。
Entity
--------------
id - PK
Field_Type_ID - FK
contact_id - FK
notes - String
donation - unsigned int (nullable)
Contact
--------------
id - PK
fname- String
lname- String
email- String
Field_Type
--------------
id - PK
Description- FK
notes - String
通过这样做,如果您最终得到更多 "field" 类型,则无需添加更多 table。您只需将新记录添加到 Field_Type table。然后,为了强制执行您想要的行为,您在实体上添加一个唯一约束,该约束同时查看 contactID 和 FieldTypeID。这将确保每个联系人只能链接到一个字段类型一次。