Difference between one-to-many and many-to-one relationship

后端 未结 11 503
心在旅途
心在旅途 2020-12-12 10:52

What is the real difference between one-to-many and many-to-one relationship? It is only reversed, kind of?

I can\'t find any \'good-and-easy-to-understand\' tutoria

相关标签:
11条回答
  • 2020-12-12 11:43

    One-to-Many and Many-to-One are similar in Multiplicity but not Aspect (i.e. Directionality).

    The mapping of Associations between entity classes and the Relationships between tables. There are two categories of Relationships:

    1. Multiplicity (ER term: cardinality)
      • One-to-one relationships: Example Husband and Wife
      • One-to-Many relationships: Example Mother and Children
      • Many-to-Many relationships: Example Student and Subject
    2. Directionality : Not affect on mapping but makes difference on how we can access data.
      • Uni-directional relationships: A relationship field or property that refers to the other entity.
      • Bi-directional relationships: Each entity has a relationship field or property that refers to the other entity.
    0 讨论(0)
  • 2020-12-12 11:45

    One-to-many and Many-to-one relationship is talking about the same logical relationship, eg an Owner may have many Homes, but a Home can only have one Owner.

    So in this example Owner is the One, and Homes are the Many. Each Home always has an owner_id (eg the Foreign Key) as an extra column.

    The difference in implementation between these two, is which table defines the relationship. In One-to-Many, the Owner is where the relationship is defined. Eg, owner1.homes lists all the homes with owner1's owner_id In Many-to-One, the Home is where the relationship is defined. Eg, home1.owner lists owner1's owner_id.

    I dont actually know in what instance you would implement the many-to-one arrangement, because it seems a bit redundant as you already know the owner_id. Perhaps its related to cleanness of deletions and changes.

    0 讨论(0)
  • 2020-12-12 11:45

    one-to-many has parent class contains n number of childrens so it is a collection mapping.

    many-to-one has n number of childrens contains one parent so it is a object mapping

    0 讨论(0)
  • 2020-12-12 11:46

    Answer to your first question is : both are similar,

    Answer to your second question is: one-to-many --> a MAN(MAN table) may have more than one wife(WOMEN table) many-to-one --> more than one women have married one MAN.

    Now if you want to relate this relation with two tables MAN and WOMEN, one MAN table row may have many relations with rows in the WOMEN table. hope it clear.

    0 讨论(0)
  • 2020-12-12 11:47
    • ---One to Many--- A Parents can has two or more children.
    • ---Many to one--- Those 3 children can have a single Parents.

      Both are similar. This can be used belongs to the need. If you wanna find children for a particular parents, then u can go with One-To-Many. or else, wanna find parents for a twins, you may go with Many-To-One. Likewise....,

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