简体   繁体   English

类似于抽象/超/子类的数据库设计结构

[英]Abstract/super/sub class-like structure for database design

I've run my head against the wall a couple of times on this one. 我已经把头撞在墙上几次了。 So I'm hoping for a little help in the right direction. 因此,我希望在正确的方向上有所帮助。

I have a table with ORDERS, one with TRAINS, one with FLIGHTS and one with BUSES. 我有一张桌子,上面有ORDERS,一张桌子有TRAINS,一张桌子有FLIGHTS,一张桌子有BUSES。 Each order should have a single way of transportation. 每个订单应有单一的运输方式。 My design has up until now consisted of a field in the ORDERS table stating the type of transport (train, flight, bus) and a field containing the forreign key to the stated type of transport. 到目前为止,我的设计包括一个在ORDERS表中说明运输类型(火车,航班,公共汽车)的字段和一个包含上述运输类型的外键的字段。

Is there any better way to do this? 有什么更好的方法吗?

The best description I've seen of this topic is in Craig Larman's book "Applying UML and patterns" - though he writes from an object oriented, rather than database point of view. 我见过的关于该主题的最好描述是在Craig Larman的书“ Applying UML andpatterns”中-尽管他是从面向对象而不是从数据库的角度编写的。

There are 3 alternatives in the relational world (this is based on the Larman book): 关系世界中有3种选择(基于Larman的书):

  • subtype per variant. 每个变体的子类型。 So, you create a "order_flight" table with airline, seat choice etc, and a "order_train" with from_station, to_station etc. This keeps the tables nice and self-describing, but makes your SQL into a huge mess - it has to change for every subtype. 因此,您将创建一个包含航空公司,座位选择等的“ order_flight”表,以及一个具有from_station,to_station等的“ order_train”表。这使表保持美观和自我描述,但是却使您的SQL陷入混乱-它必须更改对于每个子类型。
  • single table with all possible columns: in this case, you have a single table with all the possible fields for all subtypes. 具有所有可能列的单个表:在这种情况下,您具有包含所有子类型的所有可能字段的单个表。 This way, your SQL stays far simpler - but the table becomes a huge mess, and 这样,您的SQL保持简单得多-但表变得一团糟,并且
    you depend on your client application to "know" that flights have 您依靠客户应用程序“知道”航班有
    airlines, but trains don't. 航空公司,但火车没有。
  • table for common attributes with subtypes storing their unique values in their own tables. 常见属性的表,其子类型将其唯一值存储在自己的表中。 This is basically what you have chosen to date; 基本上,这是您到目前为止所选择的。 the relationship can be set either at the "order" table, or in the subclass table. 可以在“订单”表或子类表中设置该关系。

Each option has benefits and drawbacks - especially in a situation where you don't know in advance which subtypes you're going to need, the first option is the simplest on the database end, but creates a bit of a mess for the client code. 每个选项都有其优点和缺点-特别是在您事先不知道需要哪种子类型的情况下,第一个选项在数据库端最简单,但是会给客户端代码带来混乱。

You can use the same parent object Id for all items if they have object structure and subtype-supertype relation. 如果所有项目具有对象结构和subtype-supertype关系,则可以对所有项目使用相同的父对象ID。 do not treat them like different objects like the tree model. 不要将它们像树模型一样对待不同的对象。 This thread shows an example 该线程显示了一个示例

if you are using object oriented modeling, you would use the object oriented structure in the database, too. 如果使用的是面向对象的建模,则也将在数据库中使用面向对象的结构。 so if you are extending a class, you are also extending a data row, joining them on the same id numbers. 因此,如果要扩展一个类,则还要扩展一个数据行,将它们连接到相同的ID号上。 that's why I say "use same id's for supertype and subtype, because you would want to join the supertype and subtype to create a subtype object. 这就是为什么我说“对父类型和子类型使用相同的ID,因为您希望将父类型和子类型连接在一起以创建子类型对象。

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

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