What's the difference between identifying and non-identifying relationships?

前端 未结 15 1644
迷失自我
迷失自我 2020-11-22 12:02

I haven\'t been able to fully grasp the differences. Can you describe both concepts and use real world examples?

相关标签:
15条回答
  • 2020-11-22 12:21

    Here's a good description:

    Relationships between two entities may be classified as being either "identifying" or "non-identifying". Identifying relationships exist when the primary key of the parent entity is included in the primary key of the child entity. On the other hand, a non-identifying relationship exists when the primary key of the parent entity is included in the child entity but not as part of the child entity's primary key. In addition, non-identifying relationships may be further classified as being either "mandatory" or "non-mandatory". A mandatory non-identifying relationship exists when the value in the child table cannot be null. On the other hand, a non-mandatory non-identifying relationship exists when the value in the child table can be null.

    http://www.sqlteam.com/article/database-design-and-modeling-fundamentals

    Here's a simple example of an identifying relationship:

    Parent
    ------
    ID (PK)
    Name
    
    Child
    -----
    ID (PK)
    ParentID (PK, FK to Parent.ID) -- notice PK
    Name
    

    Here's a corresponding non-identifying relationship:

    Parent
    ------
    ID (PK)
    Name
    
    Child
    -----
    ID (PK)
    ParentID (FK to Parent.ID) -- notice no PK
    Name
    
    0 讨论(0)
  • 2020-11-22 12:28

    If you consider that the child item should be deleted when the parent is deleted, then it is an identifying relationship.

    If the child item should be kept even though the parent is deleted, then it is a non-identifying relatioǹship.

    As an example, I have a training database with trainees, trainings, diplomas and training sessions :

    • trainees have an identifying relationship with training sessions
    • trainings have an identifying relationship with training sessions
    • but trainees have a non-identifying relationship with diplomas

    Only training sessions should be deleted if one of the related trainee, training or diploma is deleted.

    0 讨论(0)
  • 2020-11-22 12:29

    There is another explanation from the real world:

    A book belongs to an owner, and an owner can own multiple books. But, the book can exist also without the owner, and ownership of it can change from one owner to another. The relationship between a book and an owner is a non-identifying relationship.

    A book, however, is written by an author, and the author could have written multiple books. But, the book needs to be written by an author - it cannot exist without an author. Therefore, the relationship between the book and the author is an identifying relationship.

    0 讨论(0)
提交回复
热议问题