`

Identifying Relationship vs. Non-identifying Relationship

 
阅读更多

Identifying vs non-identifying has nothing to do with identity. Simply ask yourself can the child record exist without the parent? It has to do with whether the primary key of the child (can include more than one coumn) includes the foreign key of the parent. In the non-identifying relationship the child's primary key(PK) MUST NOT include the foreign key(FK).

Ask yourself this question

  • Can the foriegn key from the parent be included in the primary key of the child?

or asked in the opposite way

  • Can the child record exist without the parent record?

These are the same question, just put differently. If the FK must not be included in the PK meaning the child can exist without the parent, then it is non-identifying. Otherwise it is identifying. As noted in the comments more clearly: If a child can exist without the parent, then it is non-identifying.

One-to-one identifying relationship

Social security numbers fit nicely in to this category. SSNs cannot exist with out a person. The person_id would be the PK for the person table, including columns such as a name and address. (let's keep it simple). The social_security_number table would include the ssn column and the person_id column as a foreign key. Since this FK could be the PK for the social_security_number table it is an identifying relationship.

One-to-one non-identifying relationship

At a large office complex you might have an office table that includes the room numbers by floor and building number with a PK, and a separate employee table. The employee table (child) has a FK which is the office_id column from the office table PK. While each employee has only one office and (for this example) every office only has one employee this is a non-identifying relationship since offices can exist without employees, and employees can change offices or work in the field.

One-to-many relatoinships

One-to-many relationships can be categorized easily by asking the same question. 

Many-to-many relationships

Many-to-many relationships are always identifying relationships. This may seem counter intuitive, but bear with me. Take two tables libary and books, each library has many books, and a copy of each book exists in many libraries.

Here's what makes it and identifying relationship: In order to implement this you need a linking table with two columns which are the primary keys of each table. Call them the library_id column and the ISBN column. This new linking table has no separate primary key, but wait! The foreign keys become a multi-column primary key for the linking table since duplicate records in the linking table would be meaningless. The links cannot exist with out the parents; therefore, this is an identifying relationship. I know, yuck right?

Most of the time the type of relationship does not matter.

All that said, usually you don't have to worry about which you have. Just assign the proper primary and foreign keys to each table and the relationship will discover itself.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics