Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

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

Treimin Clark
Ranch Hand
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
Posts: 48917
58
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
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
Posts: 12122
30
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
Bartender
Posts: 12122
30
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
Many thans to Fred and Campbell.

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

Campbell Ritchie
Sheriff
Posts: 48917
58
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.