Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7866737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:29:31+00:00 2026-06-03T00:29:31+00:00

I am having some difficulty in coding a many:many relationship, as shown in the

  • 0

I am having some difficulty in coding a many:many relationship, as shown in the image below:

enter image description here

I am wanting these tables and relationships set up so that:

  • A parent can have more than one child (the child here is the
    “Player”), and a child can have one or more parents (a maximum of
    two).
  • A “family” can consist of one or more parents (maximum of two),
    and also one or more children (unlimited).

I am not sure how to accommodate parents that have different surnames than their “children” and vice versa. I have not been informed if this will be a concern, but this is something I think is worth thinking about. Both Parent and Player classes inherit fields (like first and last names, addresses, age, d.o.b, etc) from a person superclass.

I was about to test some input into the family table when I realised I wasn’t exactly sure how to insert one or more parents with one or more children that they are parents (or caregivers) of. I thought I had this sorted out in theory but upon testing I realised that it wouldn’t work and I have spent over 90mins doing sketches on some ideas but I just got really lost.

==================================================================================

UPDATE: 27-04-2012 @ 22:19PM NZST

I should have gave a visual representation of the results I am looking for – when querying a database with these tables in it. Here is the visual representation:

+-------------------+-----------------+---------------------+
| ParentsFirstName  | ParentsLastName | ChildrenInFamily    |
+-------------------+-----------------+---------------------+
| Gregory           | Peck            | Michael             |
| Laura             | Peck            | Michael             |
| Martha            | Petersen        | Matt, Christopher   |
| Chris             | Michaels        | Richard, Shaun      |
| Nadine            | Michaels        | Richard, Shaun      |
| Barry             | Dackers         | Harry               |
| Kevin             | Mitchell        | Daniel              |
| Rebecca           | Mitchell        | Daniel              |
+-------------------+-----------------+---------------------+

The “children” are in a table called “Player”, and the Parents are in a table called “Parent”. The MySQL code in this post represents the tables concerning this particular problem (you should notice that I am using the Person class as a super class, and the parent/child tables as sub classes). Some other tables are referenced through the use of foreign keys (the “schoolID” field comes from a table called “School”, which has a “schoolName” field).

I am not sure if I have constructed my tables right for what I am wanting to achieve, but doing some research I found a function called GROUP_CONCAT, which at least gave me a thought of what a query may look like – for this particular problem.

Accommodating parents that don’t share the same last name, as well as children that don’t share the same last name as the parents, is another big challenge I can’t even try to wrap my head around (I’d imagine this would be the case for foster families as well). So for the above visualization I am assuming the non-single parents are married and share the same last name, and the children all share the same last name as both of the married parents.

==================================================================================

Here is some code that I have tried to go about creating the part of the database that tries to deal with this part (NOTE: the “Players” are the “children” of the parents):

DROP TABLE IF EXISTS `person` ;
CREATE TABLE `person` (
  `personID` INT(5) NOT NULL AUTO_INCREMENT ,
  `firstName` VARCHAR(50) NOT NULL ,
  `lastName` VARCHAR(50) NOT NULL ,
  `dateOfBirth` DATE NOT NULL ,
  `personType` CHAR(6) NOT NULL, 
  `photo` BLOB NULL DEFAULT NULL ,
  PRIMARY KEY (`personID`))
ENGINE = InnoDB;
SHOW WARNINGS;

DROP TABLE IF EXISTS `parent` ;
CREATE TABLE `parent` (
  `parentID` INT(5) NOT NULL,
  FOREIGN KEY (`parentID`) REFERENCES `person` (`personID`)
  ON DELETE CASCADE 
  ON UPDATE CASCADE)
ENGINE = InnoDB;
SHOW WARNINGS;

DROP TABLE IF EXISTS `player` ;
CREATE TABLE `player` (
  `playerID` INT(5) NOT NULL, 
  `schoolID` INT(5) NOT NULL,
    FOREIGN KEY (`playerID`) 
    REFERENCES `person` (`personID`)
    ON DELETE CASCADE 
    ON UPDATE CASCADE, 
    FOREIGN KEY (`schoolID`)
    REFERENCES `school` (`schoolID`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;
SHOW WARNINGS;

DROP TABLE IF EXISTS `family` ;
CREATE TABLE `family` (
  `parentID` INT(5) NOT NULL ,
  `playerID` INT(5) NOT NULL ,
  PRIMARY KEY (`parentID`, `playerID`), 
    FOREIGN KEY (`playerID` )
    REFERENCES `player` (`playerID`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
    FOREIGN KEY (`parentID`)
    REFERENCES `parent` (`parentID`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;
SHOW WARNINGS;

If someone can help me sort out this problem by giving some guidance, and even some generic examples with explanations, that would be really good. I think this is a many:many relationship that I just can’t drop altogether, as parents can have one or more children and a child can have one or more parents (a family wouldn’t really be a family without children in this context).

Many thanks in advance!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:29:32+00:00Added an answer on June 3, 2026 at 12:29 am

    A child can have no more than 2 parents, both of them have specific roles (mother vs. father) and there can be a situation where one or both parents are unknown.

    So this isn’t a real “many to many” relationship, it actually “many to zero or one or two”, which can be naturally represented like this (both MotherID and FatherID are NULL-able):

    enter image description here

    If you put LastName in both Parent and Player (or a common superclass in your case), this also naturally covers the situation where parents have different last names from their children.

    You can then easily get “children per parent” like this (SQL Fiddle)…

    SELECT
        ParentID,
        Parent.FirstName ParentFirstName,
        Parent.LastName ParentLastName,
        PlayerID,
        Player.FirstName PlayerFirstName,
        Player.LastName PlayerLastName
    FROM
        Parent
        LEFT JOIN Player
            ON Parent.ParentID = Player.MotherID
            OR Parent.ParentID = Player.FatherID
    ORDER BY ParentId
    

    …and pivot the data in your application code if that’s what you wish.


    The above model allows mismatch between Parent‘s gender and its mother/father role. If you want to prevent this you can go overboard and do something like this…

    enter image description here

    …but I’d rather not complicate and stick with the first model and enforce this at the application level.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some difficulty writing some Unit Tests to test a custom ModelBinder that
I'm having some difficulty with my Entity Framework context that is proving very troublesome
I've been having some difficulty in understanding the source of a problem. Below is
I am having some difficulty locating information of comparing C strings. I understand that
I'm having some difficulty embedding an image from the Properties.Resources to a MailMessage, currently
I am having some difficulty writing a function that will search through a directory
I am having some difficulty with these two functions: byteArrayToInt and intToByteArray . The
I am having some difficulty compiling a C++ program that I've written. This program
I'm having some difficulty saving a stream of bytes from an image (in this
Am having some difficulty getting an Image from a url, and then displaying it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.