Home >Database >Mysql Tutorial >N-ary relationship in database
Relationship is an important part of any entity relationship diagram as it shows the relationship between two different entities. In an n-ary relationship, n represents the number of entities in the relationship. It can be any value, but the most common relationships are unary, binary, and ternary relationships, where the number of entities is one, two, and three respectively.
More information about unary, binary and ternary relationships is as follows:
When there is a relationship between two entities of the same type, is called a unary or recursive relationship. This means that relationships are between different instances of the same entity type. Some examples of uniary relationships are as follows:
A person can only be married to one person at a time. So this is a recursive relationship between the entity person and itself. This is a one-to-one recursive relationship because one person is married to one person.
One employee can supervise multiple employees. So this is a recursive relationship between the entity employee and itself. This is a one-to-many recursive relationship because one employee supervises multiple employees.
When there is a relationship between two different entities, it is called a binary relationship. Some examples of binary relationships are as follows:
Each employee has only one ID card. So this is a one to one binary relationship where 1 employee has 1 ID card.
Many employees work in one department. So this is a many to one binary relationship where many employees work in one department.
A book can have multiple authors, or multiple authors may write a book. Therefore, there is a many-to-many relationship between books and authors, i.e. many books have many authors.
When there is a relationship between three different entities, it is called a ternary relationship. An example of a ternary relationship is as follows:
In this example, there is a ternary relationship between doctor, patient, and drug.
The above is the detailed content of N-ary relationship in database. For more information, please follow other related articles on the PHP Chinese website!