如何在数据库中实现超类,子类关系? [英] How to implement a super class, sub class relationship in the database?

查看:1123
本文介绍了如何在数据库中实现超类,子类关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个类叫动物,狗和鱼是子类。
动物有属性称为颜色。
狗有属性叫尾长,鱼没有这个属性。
鱼有属性称为重量,狗没有这个属性。

If I have a class called animal, dog and fish is the subclass. The animal have attribute called "color". Dog have the attribute called "tail length", and the fish don't have this attribute. Fish have the attribute called "weight", the dog don't have this attribute.

所以,我想设计一个数据库来存储这个信息。我该怎么办?这里有一些想法:

So, I want to design a database to store this information. What should I do? Here is some ideas:

想法1:
制作动物表,并且表格有类型,找到什么样的动物,如果它是一只狗,只是从狗桌得到结果。

Idea 1: Making an animal table, and the table have type, to find what kind of animal, if it is a dog, just get the result from dog table.

动物:
color:String
type:int

Animal: color:String type:int

$ b狗:0
鱼:1

Type: Dog:0 Fish:1

狗:
TailLength:int

Dog: TailLength:int

鱼:
重量:int

Fish: Weight:int

想法2:
在数据库中只存储Dog表和Fish表,删除动物表。

Idea 2: Store only Dog table and Fish table in the database, remove the animal table.

狗:
颜色:String
TailLength:int

Dog: Color: String TailLength: int

鱼:
颜色:String
重量:int

Fish: Color: String Weight: int

推荐答案

您提到的两种方法:


  • 一个表表示整个继承层次结构中的对象,包含整个层次结构所需的所有列,以及一个类型列,告诉您对象是

  • 继承层次结构中每个具体类的一个表,具有重复的模式。

  • One table representing objects in the entire inheritance hierarchy, with all the columns you'd need for the entire hierarchy plus a "type" column to tell you which subclass a particular object is.
  • One table for each concrete class in your inheritance hierarchy, with duplicated schema.

可以由另外两个补充:


  • 继承层次结构中每个类的一个表 - 具有动物表,子类具有指向动物中常见数据集的外键。

  • 通用模式用于存储对象的表和用于支持附加到该对象的任何属性集的属性表。

  • One table for each class in your inheritance hierarchy – you now have an Animal table, and subclasses have foreign keys that point to the common set of data in Animal.
  • Generic schema – have a table to store objects, and an attribute table to support any set of attributes attached to that object.

。这里有很好的解释:

Each approach has pros and cons. There's a good rundown of them here:

  • http://www.agiledata.org/essays/mappingObjects.html#ComparingTheStrategies

另请查看这些SO主题:

Also take a look at these SO topics:

  • Something like inheritance in database design
  • Help me to connect inheritance and relational concepts
  • Object-oriented-like structures in relational databases
  • How to do Inheritance Modeling in Relational Databases?
  • How do you effectively model inheritance in a database?

最后,需要注意的是有面向对象的数据库它们在数据库中更自然地表示对象,并且可以容易地解决这个问题,尽管它们在工业中几乎不常用。以下是一些链接,描述这些DB与关系数据库相比,虽然他们不会给你一个完全客观(heh)的意见:

Finally, it should be noted that there are object-oriented databases out there that represent objects more naturally in the database, and could easily solve this problem, though they are not nearly as frequently used in the industry. Here are some links that describe such DBs as compared to relational DBs, though they won't give you an entirely objective (heh) view on the matter:

这篇关于如何在数据库中实现超类,子类关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆