• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Many-to-one and many-to-many relationships.

 
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, sorry for posting in the wrong forum. I absolutely don't which one is the best forum post this question. It's moderators chance to move this to an appropriate forum for me.

I know what are the one-to-one and one-to-many relationships in table records. But I couldn't understand the concept of many-to-one and many-to-many. Can anyone please explain this for me, please?

 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It tells you how many objects can be related together. It may be easier to understand with examples.

  • Husband to wife: 1 . . . 1. This is the classic example of a bijection: there is always 1 at each end of the relationship (as long as polygamy is excluded). I have 1 wife and she has 1 husband.
  • Father to child. 1 . . . * This is a one-to-many relationship. One father can have several children, but each child has only 1 father (unless he has died). I used to have 1 daughter, then her sister was born and I had 2 children; they both have 1 father.
  • Parent to child: 2 . . . * Parents can have several children, but each child has two parents, unless they have died. My wife and I used to have 1 daughter, who had a mother and a father. Then another girl was born, and she too had mother and father.
  • Brother and Sister. 0..* . . . 0..* A brother can have any number of sisters (even no sisters) and a sister can have any number of brothers but might have no brothers. My daughters constitute 2 sisters and 0 brothers.


  • I hope that helps

    You are right about moving: this looks like a database design question, so I shall move you to the JDBC forum.

    Remember in database design always avoid many-to-many relationships. 1-to-1 and 1-to-many and many-to-1 are permitted.
     
    Treimin Clark
    Ranch Hand
    Posts: 757
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Dear Campbell,

    Thanks a lot for your great explanation.

    By the way, as you said with the child-to-parent example, it seems that every many-to-one relationship has an opposite one-to-many relationship too. Am I correct on this?
     
    lowercase baba
    Posts: 13089
    67
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Campbell,

    I would argue that even though my father has passed away, he is still my father, and the only one I ever had.

    Now, you left out an example of many-to-one.

    This is the opposite of one-to-many. for example, children to biological father. There is a one-to-many relationship between a father and his children, but there is a many-to-one relationship between those children and their father.
     
    fred rosenberger
    lowercase baba
    Posts: 13089
    67
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Treimin Clark wrote:Dear Campbell,

    Thanks a lot for your great explanation.

    By the way, as you said with the child-to-parent example, it seems that every many-to-one relationship has an opposite one-to-many relationship too. Am I correct on this?



    I think you have to be a little careful here... a bank has a one-to-many relationship to it's account holders. But any of those account holders may have accounts at other banks. So it's a one-to-many one way, but it could be many-to-many going back.

    at least, that's what I think.
     
    Treimin Clark
    Ranch Hand
    Posts: 757
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Many thans to Fred and Campbell.

    However it seems that Campbell forgot to move this to the JDBC forum.
     
    Campbell Ritchie
    Marshal
    Posts: 79180
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I do have this tendency to forget. Sorry. I have now moved it. And you are right that you can (sometimes) make a many-to-1 by reversing a 1-to-many.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic