I have made a small database for a daycare centre. I have been reading up on identifying and non-identifying relationships and am still a bit confused about the whole thing. I have been using MySQL Workbench to design the database.
Now, the criterias for parents and children are that a Parent can have one or more Children and vice versa – ergo, the relationship between Parents and Children is a many-to-many. The best praktice to solve this (as I understand) is to make a third table – Parets_Children and use that as a “connection” between the other two:
Parents - 1:n - Parents_Children n:1 - Children
Now the issue I have is wether or not to use an identifying relationship here. The logic is that no child can exist without a parent and the parents wouldn’t be members of the daycare unless they had at least one child there. Neither of them would exist “alone”. However, when letting MysQL WB sorting it out for me, it creates a non-identifying relationship (I think).
Very simplified code:
CREATE TABLE 'parents' (
'id_parents' SMALLINT UNSIGNED NOT NULL ,
'name' VARCHAR(48) NOT NULL ,
PRIMARY KEY ('id_parents');
CREATE TABLE 'parents_children' (
'id_parents' SMALLINT UNSIGNED NOT NULL ,
'id_children' SMALLINT UNSIGNED NOT NULL ,
PRIMARY KEY ('id_parents', 'id_children') ,
FOREIGN KEY ('id_parents') REFERENCES 'parents' ('id_parents' )
FOREIGN KEY ('id_children' ) REFERENCES 'children' ('id_children' );
CREATE TABLE 'children' (
'id_children' SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT ,
'name' VARCHAR(48) NOT NULL ,
PRIMARY KEY ('id_children');
Would this output be sufficient to reach the desired function? Have I misunderstood the whole relationship thing? I hope my question isn’t too obsqure.
From the schema you provided I can see that the Foreign Key Constraint exists on table
parents_children, which will ensure that the link between parent and child can only exist if both parent and child exists.This does not however stop you from inserting an entry into
parents, without an accompanying entry inparents_childrenor for that matterchildren.So in short, this schema allows for a parent to exist, without a child.
The problem with your logic here would be that both the parent and child needs to be created before the relationship can be created in
parents_children(due to the foreign key relationships). However, the additional logic that you are looking for (no child no parent and vice versa) would require all a link between parent and child before a parent or child can be created.You see the chicken/egg problem here?