aspose file tools*
The moose likes JDBC and the fly likes Many-to-one and many-to-many relationships. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Many-to-one and many-to-many relationships." Watch "Many-to-one and many-to-many relationships." New topic
Author

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

Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
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?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
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

    Joined: Nov 12, 2008
    Posts: 757
    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?
    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11312
        
      16

    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.


    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11312
        
      16

    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

    Joined: Nov 12, 2008
    Posts: 757
    Many thans to Fred and Campbell.

    However it seems that Campbell forgot to move this to the JDBC forum.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38818
        
      23
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Many-to-one and many-to-many relationships.