开发者

How to guarantee uniqueness when N tables have 1:1 relationship with a common table?

开发者 https://www.devze.com 2023-03-27 02:27 出处:网络
Lets suppose I have a scenario with the following model: An Animal table which represents any开发者_StackOverflow中文版 animal, a Dog table and a Bird table, each one with a 1:1 relationship with the

Lets suppose I have a scenario with the following model: An Animal table which represents any开发者_StackOverflow中文版 animal, a Dog table and a Bird table, each one with a 1:1 relationship with the Animal table.

Animal

    INTEGER id (PK)
    STRING name

Bird

    INTEGER id (PK FK referencing `Animal.id`)

Dog

    INTEGER id (PK FK referencing `Animal.id`)                

(I'm giving only the keys just to be clear)

How can I guarantee that a given row in the Animal table will have JUST one referenced row in either the Dog or the Bird table? The model itself allows it...

An animal can't be a Dog and a Bird at the same time (not in mythology but that's not the case :P)

It would be better if this could be done just by playing with the model, without triggers...

Any tip would be appreciated :)


This may be what @Pranay meant, but the answer was incomplete. Add a column TYPE to all tables and then constrain it like this:

create table Animal (id integer, 
                     type string,
                     name string,
                     primary key (id),
                     unique (id, type)
                    );

create table Bird (id integer,
                   type string default 'BIRD' check (type='BIRD'),
                   primary key (id),
                   foreign key (id, type) references Animal (id, type)
                  );

create table Dog (id integer,
                  type string default 'DOG' check (type='DOG'),
                  primary key (id),
                  foreign key (id, type) references Animal (id, type)
                 );

See David Portas's blog fora good explanation of this.


In you databayse you can add one more column in you table like AnimalType

AnimalTable 
   Id
   AnimalType -- 1 = dog, 2= bird, 3= other


I don't think that this can be done without triggers / constraints whilst keeping the tables separate.


I feel that the database design here is wrong. The reason I say that is it seems that for every new animal you have to create new table. In general you want to create a database in the way so that every time you need to add data the schema does not change.

Here is one way to do it:
Table animals
animal_id PK
name

Table animailProperties
property_id PK
name

Table animalDecription
animail_id FK
property_id FK
property_data

Example:

Table animals
1 DOG
2 Cat
3 Bird

Table animailProperties
1 legs
2 wings
3 fly

Table animalDecription
1 1 4 (dog legs 4)
1 2 0 (dog wings 0)
1 3 0 (dog fly no)
2 1 4 (cat legs 4)
2 2 0 (cat wings 0)
2 3 0 (cat fly no)
3 1 2 (bird legs 2)
3 2 2 (bird wings 2)
3 3 1 (bird fly yes)

Something along these lines. So you can create any type of properties for every possible animal.
Every time you need to create a new animal, you just assign correct properties to it.

I hope it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号