简体   繁体   English

当一个表引用多个表时的关系数据库设计

[英]Relational database design when one table referes to several others

For exemple let's imagine an " appointments " table that keeps track of several Types of appointments (eye, teeth ..) This table could have an ID, date, doctor_id, Appointment_type and other columns that are commun to all appointment types. 例如,让我们想象一个“ appointments ”表,该表跟踪几种约会类型(眼睛,牙齿..)。该表可能具有一个ID, date, doctor_id, Appointment_type和其他所有约会类型ID, date, doctor_id, Appointment_type列。 Then each type of appointment will has it's own table to hold data specific to this appointements type. 然后,每种类型的约会都有其自己的表来保存特定于此约会类型的数据。

I chose this design as we have a significant amount of data commun to all appointments so i don't want to repeat it and each appointments holds very different type of data, so having only one appointent table that has lots of NULL field seemed unhandy. 我之所以选择这种设计,是因为我们对所有约会都有大量的数据共享,所以我不想重复它,并且每个约会都拥有非常不同的数据类型,因此仅拥有一个包含大量NULL字段的指定表似乎不方便。

Knowing that I will mostly need to query for all appointment type at once what would be the best way to organize these tables ? 知道我几乎需要立即查询所有约会类型,什么是组织这些表的最佳方法?

There're two approaches: 有两种方法:

One table per hierarchy (aka Single Table Inheritance) You put all the attributes of all appointment types in one table and use a "type" filed" to distinguish the type of appointments. This is fast because there's no join. But this table may have many columns. 每个层次结构一个表(又称“单表继承”),您将所有约会类型的所有属性都放在一个表中,并使用“类型”字段”来区分约会的类型。这是快速的,因为没有联接。但是此表可能具有许多列。

One table per subclass (aka Multiple Table Inheritance) This way you have a base class table including the common fields, and use polymorphic association to link to the records in the subclass tables. 每个子类一个表(又名多表继承)这样,您就可以拥有一个包含公共字段的基类表,并使用多态关联来链接到子类表中的记录。 This is slower, requires more tables. 这比较慢,需要更多表。 Joins can be difficult to write if you want to query all appointment types. 如果要查询所有约会类型,联接可能很难编写。

I think for your application (query all appointment types), one table per hierarchy is better. 我认为对于您的应用程序(查询所有约会类型),每个层次结构一个表更好。

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

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