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

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!!!
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
MotherIDandFatherIDare NULL-able):If you put
LastNamein bothParentandPlayer(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)…
…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……but I’d rather not complicate and stick with the first model and enforce this at the application level.