[英]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的书):
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.