How to relate 3 tables depending on event

假装没事ソ 提交于 2019-12-01 01:16:36

In this can, you cannot set up a foreign key because you have multiple parent. In order to do fast search or to avoid full table scan define an index on column person on table event and join the table using LEFT JOIN. eg,

SELECT  ....,
        COALESCE(b.name, c.name) AS personname
FROM    event a
        LEFT JOIN civil b
            ON a.person = b.civil_id
        LEFT JOIN worker c
            ON a.person = c.worker_ID

Adding INDEX

ALTER TABLE event ADD INDEX (person)
Branko Dimitrijevic

Generally, there are to ways to model this type of situation...

Using Exclusive Foreign Keys

In the event, both civil_id and worker_id are NULL-able, but there is also a constraint ensuring exactly one of them is non-NULL at any given time:

CHECK (
    (civil_id IS NOT NULL AND worker_id IS NULL)
    OR (civil_id IS NULL AND worker_id IS NOT NULL)
)

Using Inheritance1

For more on inheritance, take a look at "Subtype Relationships" chapter in the ERwin Methods Guide and at this post.


1 Aka. category, subtyping, subclassing, generalization hierarchy...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!