How to implement a super class, sub class relationship in the database?

前端 未结 3 1538
-上瘾入骨i
-上瘾入骨i 2020-12-29 11:06

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\

3条回答
  •  天命终不由人
    2020-12-29 11:16

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

提交回复
热议问题