Identifying relationship - many to many

拟墨画扇 提交于 2019-12-06 09:58:29

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 in parents_children or for that matter children.

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?

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.

To implement this is in a simple way.. have one table MyDayCare where you can have the columns like: RecordID, ChildID, ChildName, ParentID, ParentName
While inserting records you will have to calculate the next ChildID and ParentID. You can have other coulms in your table like DateOfBirth so as to uniquely identify each parent-child pair.

When you delete a child, the parent would also be deleted and vice-versa.
You can even store records of more than one parent for the same child (in different rows). By using simple SELF JOINs you can fetch any data you want for a particular child or parent.

Please note that this is not the best way of designing database tables, but as per your requirement this can also work.

Branko Dimitrijevic

Whether to use identifying relationships or not on the junction (aka. link) table Parets_Children depends on what kind of primary key you wish this table to have.

Identifying relationships will produce a "natural" composite PK {id_parents, id_children}, as you already noted. Non-identifying relationships would allow you to have a "surrogate" PK, in addition to (now alternate) key {id_parents, id_children}.

Unless you have a specific reason for a surrogate key, having just a natural PK on the junction table should be quite enough.

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.

Unfortunately, the presence of at least one child would not be easy to enforce through purely declarative means. In fact (to my knowledge at least) it is impossible to declaratively enforce it on a DBMS such as MySQL that doesn't support deferred constraints, which would be necessary to break chicken-and-egg problem when inserting parent and child together.

If it did (support deferred FKs), the presence of at least one child per parent could be enforced declaratively like this:

The chicken-and-egg problem caused by the circular dependency between parent and parent_child would be resolved by deferring the enforcement of FKs until after all the necessary rows have been inserted.

As it stands, you'll have to either do it in the application code, or make the stored procedures that ensure the correct behavior and then manipulate the data only through them.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!