如何在数据库中实现超类、子类关系?

发布于 2024-09-08 13:47:35 字数 365 浏览 6 评论 0 原文

如果我有一个名为动物的类,那么狗和鱼就是子类。 动物有一种属性,叫做“颜色”。 狗有一个叫做“尾巴长度”的属性,而鱼没有这个属性。 鱼有一个属性叫“体重”,狗没有这个属性。

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

想法 1: 制作一个动物表,表中有类型,要查找是什么动物,如果是狗,就从狗表中获取结果。

动物: 颜色:绳子 类型:int

类型: 狗:0 鱼:1

狗: 尾长:int

鱼: 重量:int

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

狗: 颜色: 细绳 尾长:int

鱼: 颜色: 细绳 重量:整数

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:

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.

Animal:
color:String
type:int

Type:
Dog:0
Fish:1

Dog:
TailLength:int

Fish:
Weight:int

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

Dog:
Color: String
TailLength: int

Fish:
Color: String
Weight: int

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小镇女孩 2024-09-15 13:47:35

您提到的两种方法:

  • 一张表代表整个继承层次结构中的对象,其中包含整个层次结构所需的所有列以及一个“类型”列来告诉您特定对象是哪个子类。
  • 继承层次结构中的每个具体类都有一个表,具有重复的架构。

可以用另外两个来补充:

  • 继承层次结构中的每个类都有一个表 - 现在您有一个 Animal 表,子类有带有外键的表,这些外键指向 中的公共数据集动物。
  • 通用模式 – 有一个表来存储对象,以及一个属性表来支持附加到该对象的任何属性集。

每种方法都有优点和缺点。这里有一个很好的概述:

另外看看这些SO主题:

最后,应该需要注意的是,有一些面向对象的数据库(又名对象数据库,或 OODBMS)可以更自然地在数据库中表示对象,并且可以轻松解决这个问题,尽管我不认为它们是这样。 re 在行业中经常使用。以下是一些链接,这些链接描述了此类数据库与关系(和其他)数据库的比较,尽管它们不会为您提供关于此事的完全客观(呵呵)观点:

The two approaches you mentioned:

  • 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.

can be supplemented by two others:

  • One table for each class in your inheritance hierarchy – you now have an Animal table, and subclasses have tables with 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:

Also take a look at these SO topics:

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

客…行舟 2024-09-15 13:47:35

你可以这样尝试:

Animal
    PK animal_id
    FK animal_type
    STRING animal_name (eg. 'Lassie')

AnimalTypes
    PK animal_type
    STRING animal_type_name (eg. 'Dog')

AnimalAttributes
    PK attribute_id
    STRING attribute_name (eg. 'tail length')

AnimalToAttributes
    PK id
    FK animal_id
    FK attribute_id
    INTEGER value (eg. 20)

这样你就可以为每只动物拥有一个或多个属性(由你选择)。

You could try it like this:

Animal
    PK animal_id
    FK animal_type
    STRING animal_name (eg. 'Lassie')

AnimalTypes
    PK animal_type
    STRING animal_type_name (eg. 'Dog')

AnimalAttributes
    PK attribute_id
    STRING attribute_name (eg. 'tail length')

AnimalToAttributes
    PK id
    FK animal_id
    FK attribute_id
    INTEGER value (eg. 20)

This way you can have one or many attributes per animal (it's up to you to choose).

凉城已无爱 2024-09-15 13:47:35

使用一对零或一对一的关系 正如您所注意到的,在数据​​库模式设计语言中,表被称为类 - 子类或超类

   Create Table Animal
    (animalId Integer Primary Key Not null,
     Other columns generic to all animals)

   Create Table Birds
    (BirdId Integer Primary Key Not Null 
     references Animal(AnimalId),
     -- other columns)

Use a one to zero or one relationship As you note, In database schema design language the tables are called class - sub-class or superclass

   Create Table Animal
    (animalId Integer Primary Key Not null,
     Other columns generic to all animals)

   Create Table Birds
    (BirdId Integer Primary Key Not Null 
     references Animal(AnimalId),
     -- other columns)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文